Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Draw and save on a file

Status
Not open for further replies.

swreng

MIS
Jun 22, 2006
90
GR
Dear all,

I am looking for an example code, on drawing simple lines on a form and save all the drawing to any image file.

Here is my code that drawing 3 horizontal lines on a form. My problem is ,how can i save this drawing to any image file?
Many thanks!


System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen,20,100,100,100);

formGraphics.DrawLine(myPen,20,150,100,150);

formGraphics.DrawLine(myPen,20,200,100,200);
myPen.Dispose();
formGraphics.Dispose();
 
You can draw first on an offline buffer and when you're done, draw the final image into the form.

Code:
Bitmatp buffer = new Bitmap(300, 300);
Graphics g = Graphics.FromImage(buffer);
/* draw stuffs */

void form_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.DrawImage(buffer);
}

/* to save... */
buffer.Save("somefile.bmp");

Sorry for the codes if you find errors, I don't have .net on my machine now. There are other solutions but I use this method often.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top