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:
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