繁体   English   中英

如何保存带有当前图形的pictureBox1.Image?

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

在form1中,我有pictureBox1 Paint事件:

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);
}

当我运行该程序时,我看到了DrawLine,我看到了cloudEnteringAlert,并且图像上看到了pictureBox1上的一切。

我现在添加了保存行:

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

我的驱动器上有很多图像gif,但它们是相同的,我看不到CloudEnteringAlert,而DrawLine仅显示图像本身。 我想我不是仅保存图像的更改。 然后,如何一直保存它,包括CloudEnteringAlert和DrawLine的图形?

编辑:

这就是我现在所做的:

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);
            }
        }

我有timer3 tick事件,使pictureBox1.Invalidate(); 每10毫秒,我需要10毫秒的间隔,因为我希望绘画事件中的图形尽可能平滑并且也要快。

而且由于我在绘画事件中每隔10ms调用一次savePictureBox,因此一切都很缓慢。

这是timer3滴答事件。 Timer3间隔设置为10ms,因为我希望DrawLine方法中的绘制速度更快。

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

这是您可能正在寻找的代码:

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);
    }
}

它将保存带有ImageBackgroundImage (如果有)的PictureBox以及Paint事件中绘制到其上的所有内容。

相反,如果您实际上要修改图像,请这样说。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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