0

I have a need to create an Image at run time and save it on the server. For the sake of example, lets say I'm creating just a basic rectangle. This rectangle image will be a .png file. How would I do this with C# code in an ASP.NET MVC 4 app?

I'm trying to learn how to draw basic images in C# however, I'm not sure where to start. Can someone point me to a basic sample of drawing a a rectangle?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Bill Jones
  • 701
  • 4
  • 16
  • 24

2 Answers2

2

Here is the sample for you to start.

Source: How to generate a PNG file with C#?

MSDN: Bitmap Class, Graphics Class

using (Bitmap b = new Bitmap(50, 50)) {
  using (Graphics g = Graphics.FromImage(b)) {
    g.Clear(Color.Green);
 }
  b.Save(@"C:\green.png", ImageFormat.Png);
}
Community
  • 1
  • 1
Hary
  • 5,690
  • 7
  • 42
  • 79
0

Hope this help:

Bitmap img = new Bitmap(300, 50);
Graphics g = Graphics.FromImage(img);
g.FillRectangle(Brushes.White, 1, 1, 298, 48);
// render the image to output stream
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//clean up
g.Dispose();
img.Dispose();
phnkha
  • 7,782
  • 2
  • 24
  • 31