converting text to image in .net core

Anjali Agarwal 1,426 Reputation points
2023-03-26T22:09:36.8366667+00:00

hello All,

How can I convert text to image in .net core. I dont want to use any third party tool.

Any help will be appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,564 questions
{count} votes

Accepted answer
  1. Qing Guo - MSFT 896 Reputation points Microsoft Vendor
    2023-03-27T08:56:35.4866667+00:00

    Hi @Anjali Agarwal,

    <input box will be passed to the controller. I want to change that text to an image.

    I have a work demo like below, you can refer to it.

    In Controller:

    
    
    public class WordController : Controller
        {      
            public IActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public IActionResult Index(string? word)
            {
                return RedirectToAction("GenerateImage", new {text=word});
            }
            public FileResult GenerateImage(string text)
            {
                MemoryStream ms = new MemoryStream();            
                System.Drawing.Image img = new Bitmap(200, 30, System.Drawing.Imaging.PixelFormat.Format64bppArgb);
                Graphics drawing = Graphics.FromImage(img);
                Font font = new Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
    
                SizeF textSize = drawing.MeasureString(text, font);
                img.Dispose();
                drawing.Dispose();
                //create a new image of the right size
                img = new Bitmap((int)textSize.Width + 30, (int)textSize.Height + 40);
                drawing = Graphics.FromImage(img);
                Color backColor = Color.White;
                Color textColor = Color.FromArgb(1, 119, 245);
    
                //textColor. = "#0177F5";
                //paint the background
                drawing.Clear(backColor);
                //create a brush for the text
                Brush textBrush = new SolidBrush(textColor);
                drawing.DrawString(text, font, textBrush, 40, 20);
                drawing.Save();
                font.Dispose();
                textBrush.Dispose();
                drawing.Dispose();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                img.Dispose();
                return File(ms.ToArray(), "image/png");
            }
        }
    

    Index view:

    <form  method="post">
        <input name="word"  type="text"  />
        <button type="submit">
            submit
        </button>    
    </form>
    

    result:1


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Qing Guo

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.