简体   繁体   中英

How do I save pictureBox1.Image with the current drawings inside?

In form1 I have pictureBox1 Paint event:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
  CloudEnteringAlert.Paint(e.Graphics, factor, distance);
  pictureboximagestosavecount++;
  pictureBox1.Image.Save(@"c:\temp\pboximages\" + 
      pictureboximagestosavecount.ToString("D6") + 
      "pbimg.gif", System.Drawing.Imaging.ImageFormat.Gif);
  anglecounter += 1;
  DrawLine(e.Graphics, anglecounter);
  if (null != mImage)
  {
     e.Graphics.DrawImage(mImage, mRect);
  }
  DrawRectangle(e.Graphics);
}

When I'm running the program I see the DrawLine I see cloudEnteringAlert and the image I see everything on the pictureBox1.

I added now the saving line:

pictureBox1.Image.Save(@"c:\temp\pboximages\" + 
                         pictureboximagestosavecount.ToString("D6") + "pbimg.gif", 
                         System.Drawing.Imaging.ImageFormat.Gif);

I'm getting many images gif's on my drive but they are the same I don't see the CloudEnteringAlert and not the DrawLine only the image itself. I guess I'm not saving the changes only the Image. Then how can I save it all the time including the drawings of the CloudEnteringAlert and the DrawLine ?

EDIT:

This is what i did now:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            anglecounter += 1;
            DrawLine(e.Graphics, anglecounter);
            if (null != mImage)
            {
                e.Graphics.DrawImage(mImage, mRect);
            }
            DrawRectangle(e.Graphics);

            pictureboximagestosavecount++;
            savePictureBox(pictureBox1, @"c:\temp\pboximages\" + pictureboximagestosavecount.ToString("D6") + "pbimg.gif");
        }

        void savePictureBox(PictureBox PB, string fileName)
        {
            using (Bitmap bmp = new Bitmap(PB.ClientSize.Width, PB.ClientSize.Height))
            {
                PB.DrawToBitmap(bmp, PB.ClientRectangle);
                bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
            }
        }

I have timer3 tick event that make pictureBox1.Invalidate(); each 10ms I need it 10ms interval since i want the drawings in the paint event to be smooth as possible and also fast.

And since i call savePictureBox in the paint event each 10ms it make everything very slow.

This is timer3 tick event. Timer3 interval is set to 10ms since i want that the drawing in the method DrawLine to be fast.

private void timer3_Tick(object sender, EventArgs e)
        {
            pictureBox1.Invalidate();
        }

Here is the code you probably are looking for:

void savePictureBox(PictureBox PB, string fileName)
{
    using (Bitmap bmp = new Bitmap(PB.ClientSize.Width, PB.ClientSize.Height))
    {
        PB.DrawToBitmap(bmp, PB.ClientRectangle);
        bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
    }
}

It saves the PictureBox complete with Image , BackgroundImage (if there is one) and everything drawn onto it in the Paint event.

If instead you actually want to modify the Image say so..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM