繁体   English   中英

我正在尝试将pictureBox1内的图像保存到我的硬盘上,但是将其保存而没有pictureBox1内的矩形,这是为什么呢?

[英]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?

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事件中,im绘制矩形(如果需要),这是代码:

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

感谢您的帮助。

这是因为您不更改图像,而是将输出更改为屏幕。 您需要使用以下命令从图像创建图形对象

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

您正在屏幕上绘制矩形,在该矩形上控件的常规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);
}

暂无
暂无

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

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