【问题标题】:Im trying to save now the image inside the pictureBox1 to my hard disk but its saving it without the rectangle inside the pictureBox1 why?我现在试图将pictureBox1中的图像保存到我的硬盘上,但是它保存它时没有pictureBox1中的矩形,为什么?
【发布时间】:2011-09-07 05:42:54
【问题描述】:
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 为真时,它会将这个rec​​tToDrawOut 显示为pictureBox1 上的矩形

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

但它只是从没有矩形的图片框1 中获得的图像。我也尝试了 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);
                                    }
            }
        }

感谢您的帮助。

【问题讨论】:

  • 有没有机会修复缩进?

标签: c# image


【解决方案1】:

您正在屏幕上绘制矩形,其中位图刚刚由控件的常规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);
}

【讨论】:

  • 咕噜谢谢。你能告诉我如何在我的代码中使用它吗?试过了,不知道怎么弄。
【解决方案2】:

这是因为您不更改图像,而是将输出更改为屏幕。您需要从图像创建一个图形对象

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

【讨论】:

  • hcb 你能告诉我如何在我的代码中使用这个 g 图形对象吗?试过了,不知道怎么弄。
  • Hcb 我试过你的例子,问题是我现在在pictureBox1 中看到的矩形与之前的位置不同。我不知道为什么。
  • @Daniel Lip:如果图片框与图像的大小完全相同,则在位图上绘图和在 Paint 事件中绘图会产生相同的结果。否则,您需要缩放矩形以适合图像,就像图像被缩放以适合图片框一样,只有相反的方式。
猜你喜欢
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 2012-11-17
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
相关资源
最近更新 更多