Reputation: 701
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?
Upvotes: 0
Views: 2201
Reputation: 7882
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();
Upvotes: 0
Reputation: 5818
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);
}
Upvotes: 2