Dynamic Images on MVC Websites (Stack Exchange Beta)

I looked at the Stack Exchange beta sites and noticed that every site that is in the beta has graphics at the top with the words β€œWeb Applications Beta” or β€œGaming Beta,” for example.

I wondered if these images were created separately or if they were created dynamically? Is there a way to use MVC.NET to create PNG on the fly?

If the answer is too complicated for this forum, can someone point me to any articles that will help?

Thanks in advance

Sniffer

+7
asp.net-mvc png
source share
2 answers

Yes, there is a way to generate and maintain images dynamically using ASP.NET MVC. Here is an example:

public class HomeController : Controller { public ActionResult MyImage() { // Load an existing image using (var img = Image.FromFile(Server.MapPath("~/test.png"))) using (var g = Graphics.FromImage(img)) { // Use the Graphics object to modify it g.DrawLine(new Pen(Color.Red), new Point(0, 0), new Point(50, 50)); g.DrawString("Hello World", new Font(FontFamily.GenericSerif, 20), new Pen(Color.Red, 2).Brush, new PointF(10, 10) ); // Write the resulting image to the response stream using (var stream = new MemoryStream()) { img.Save(stream, ImageFormat.Png); return File(stream.ToArray(), "image/png"); } } } } 

And then just include this image in the view:

 <img src="<%= Url.Action("myimage", "home") %>" alt="my image" /> 
+14
source share

It worked for me.

 using System.Web.Helpers; public class HomeController : Controller { public FileContentResult ImageOutput() { WebImage img = new WebImage("C:\\temp\\blank.jpg")); //Do whatever you want with the image using the WebImage class return new FileContentResult(img.GetBytes(), string.Format("image/{0}",img.ImageFormat)); } } 

To use it, do the same thing that Darin said

 <img src="<%= Url.Action("ImageOutput", "home") %>" alt="my image" /> 
0
source share

All Articles