using System.Drawing; using System.Drawing.Imaging; Bitmap bmp = new Bitmap(100,100); Graphics g = Graphics.FromImage(bmp); // draw something on the graphics object g.DrawLine(Pens.Black, 0, 0, 100, 100); // save the graphics object to a file bmp.Save("image.png", ImageFormat.Png);
using System.Drawing; using System.Drawing.Imaging; using System.IO; Bitmap bmp = new Bitmap(100,100); Graphics g = Graphics.FromImage(bmp); // draw something on the graphics object g.DrawLine(Pens.Black, 0, 0, 100, 100); // save the graphics object to a stream MemoryStream stream = new MemoryStream(); bmp.Save(stream, ImageFormat.Png);In this example, we create a graphics object from a bitmap, draw a line on it, and then save it to a memory stream instead of a file. The package library used in this example is System.Drawing.