Saving a Graphic as BMP (C#)

cheezy

Member
Joined
Apr 11, 2003
Messages
13
Location
Tulsa, Oklahoma
Saving a Graphic as BMP

Hi:

Ive been working on this problem for several days now and Im really confused; what Im trying to do is pop up a form with a place for a person to make his/her signature, and save that to a bmp file on a button click. Heres what Ive got so far:

I have this (blank) pictureBox that Im using as the writing surface, and getting a script-signature I do the following:

C#:
private void pictureBox2_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
  if(ShouldPaint)
  {
    if(x0 !=0 &&y0 != 0 )
    {
      Graphics g =  pictureBox2.CreateGraphics();
      Pen blackPen = new Pen(Color.Black, 2);
      Point startPoint = new Point(x0,y0);
      Point endPoint = new Point(e.X,e.Y);
      g.DrawLine(blackPen,startPoint,endPoint);
      g.Dispose();
      }
				
    x0 = e.X;
    y0 = e.Y;
    }
}

private void pictureBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    ShouldPaint = true;
}

private void pictureBox2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    ShouldPaint = false;
    x0 = 0;
    y0 = 0;
}

This works well enough; I get a signature-looking thing on my form but I havent figured out a way to save this image onto my hard drive. Anybody have help? I researched this on Extreme.net and the closest advice is reasonable (to convert the graphics to a bitmap image and then save the bitmap) but I cannot figure out the code for this.

Thanks!
 
Last edited by a moderator:
Bitmap B=new Bitmap(200, 200);
Graphics G= Graphics.FromImage(B);

now when you draw with G, you actually draw on B. You can put the image in the picture box to see what you are drawing on mouse move events.

And then later, if you want you can save the pretty picture you created using B.Save();
 
PERFECT !!!!

Thank you for your input; it saved my week!

To SUMMARIZE:

I created a private Bitmap= Bee;

and on load event of the form I initialized the bitmap:

Bitmap Bee = new Bitmap(200,200);

and in the snippet of code above I updated to:

Graphics K = Graphics.FromImage(Bee);
Pen blackPen = new Pen(Color.White, 1);
Point startPoint = new Point(x0,y0);
Point endPoint = new Point(e.X,e.Y);
K.DrawLine(blackPen,startPoint,endPoint);
pictureBox2.Image = Bee;
K.Dispose();

I have a Save Button where I have the following code:

pictureBox2.Image.Save(strName,System.Drawing.Imaging.ImageFormat.Bmp);

The strName is declared as following:

private string strName = "D:/testout.bmp";

for example.

THANKS AGAIN!!!
 
Hi,

Does anyone know of a way I can achieve the same affect in .NET Compact Framework? The above code as it is doesnt seem to work (doesnt show me anything on the form). Furthermore, I know that once I have a signature, I wont be able to save it as described above because Bitmaps save function is not supported by CF. Id appreciate any direction...

Thank you,
Bilal
 
Back
Top