我试图将pictureBox1中的图像保存到我的硬盘但是它保存它而没有pictureBox1中的矩形为什么?

时间:2011-09-07 05:42:55

标签: c# image

if (ImagesComparion1.ImageComparison(File1, file2, image_scan_text_rect) == true)
                                            {
                                               /* Logger.Write("File1 is >>>> " + combinedTemp);
                                                // Logger.Write("File2 is >>>> " + fi.FullName);
                                                Logger.Write("Last File is >>>> " + last_file);
                                                Logger.Write("image_scan_text_rect values are >>>>> " + image_scan_text_rect.ToString());
                                                Logger.Write("ImagesComparion1.ImageComparison(File1, file2, image_scan_text_rect) is now true");*/
                                                if (pictureBox1.Image != null)
                                                {
                                                    pictureBox1.Image.Dispose();
                                                    pictureBox1.Image = null;
                                                }
                                                pictureBox1.Load(last_file);
                                                File1.Dispose();
                                                Properties.Resources.RadarImageClose.Dispose();
                                                label18.Text = "The Radar Is Not Active Now";
                                                label18.Visible = true;

                                                if (paintDemoMode == true)
                                                {
                                                    Bitmap bmp = new Bitmap(combinedTemp);
                                                    Bitmap bb = new Bitmap(bmp);
                                                    bmp.Dispose();
                                                    bmp = null;


                                                    if (pictureBox1.Image != null)
                                                    {
                                                        pictureBox1.Image.Dispose();
                                                        pictureBox1.Image = null;
                                                    }

                                                    pictureBox1.Image = bb;
                                                    image_scan_text_rect = new Rectangle(25, 240, 341, 39);
                                                    float x = ((float)image_scan_text_rect.X / bb.Width) * (float)this.pictureBox1.Width;
                                                    //float y = ((float)Rect.Y / this.pictureBox1.ClientSize.Height) * (float)FirstImage.Height;
                                                    float y = ((float)image_scan_text_rect.Y / bb.Height) * (float)this.pictureBox1.Height;
                                                    //float newRight = ((float)Rect.Right / (float)pictureBox1.ClientSize.Width) * ((float)FirstImage.Width); 
                                                    float newRight = ((float)image_scan_text_rect.Right / bb.Width) * (float)pictureBox1.Width;
                                                    //float newBottom = ((float)Rect.Bottom / (float)pictureBox1.ClientSize.Height) * ((float)FirstImage.Height);
                                                    float newBottom = ((float)image_scan_text_rect.Bottom / bb.Height) * (float)pictureBox1.Height;
                                                    rectToDrawOut = new RectangleF(x, y, newRight - x, newBottom - y);
                                                    pictureBox1.Image.Save(@"d:\testit.png", System.Drawing.Imaging.ImageFormat.Png);
                                                }
                                                return;
                                            }

当我单击一个按钮并且paintDemoMode为true时,它在pictureBox1上将此rectToDrawOut显示为矩形

现在我想保存到我的硬盘中,图片显示现在包含矩形的pictureBox1。

但它只是来自pictureBox1的图像而没有矩形。我也尝试了pictureBox1.Image.Save我尝试了bb.Save但又没有保存矩形。什么错了?

在pictureBox1_Paint事件中,如果需要,则绘制矩形,这是代码:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            using (Pen pen = new Pen(Color.Red, 2))
            {
                if (paintDemoButtonSwitch == true)
                {
                                        e.Graphics.DrawRectangle(pen, rectToDrawOut.X, rectToDrawOut.Y, rectToDrawOut.Width, rectToDrawOut.Height);
                                    }
            }
        }

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这是因为您没有更改图像,而是将输出更改为屏幕。您需要使用

从图像创建图形对象
Graphics g = Graphics.FromImage(pictureBox1.Image);

如果添加一个带有该图形对象的矩形,它应该可以工作。

编辑:

用以下内容替换保存图像的行:

   Graphics g = Graphics.FromImage(pictureBox1.Image);
   using (Pen pen = new Pen(Color.Red, 2))
      {
         if (paintDemoButtonSwitch == true)
         {
            g.DrawRectangle(pen, rectToDrawOut.X, rectToDrawOut.Y, rectToDrawOut.Width, rectToDrawOut.Height);
         }
   }
   pictureBox1.Image.Save(@"d:\testit.png", System.Drawing.Imaging.ImageFormat.Png);

答案 1 :(得分:0)

您正在屏幕上绘制矩形,其中位图刚刚由控件的常规Paint事件绘制。这不会影响位图。

您可以使用DrawToBitmap方法将控件呈现为位图。这将包括矩形,它还将包括控件在渲染图像时执行的任何缩放/裁剪:

using (Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height)) {
  pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
  b.Save(@"d:\testit.png", System.Drawing.Imaging.ImageFormat.Png);
}