简体   繁体   中英

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

When i click a button and paintDemoMode is true then its showing this rectToDrawOut as rectangle on the pictureBox1

Now i want to save to my hard disk the image show now in the pictureBox1 including the rectangle.

But its aving just the image from the pictureBox1 without the rectangle. I tried also instead pictureBox1.Image.Save i tried bb.Save but again it didnt save the rectangle. whats wrong here ?

In the pictureBox1_Paint event im drawing the rectangle if its needed this is the code:

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

Thanks for helping.

It's because you don't change the Image, you change the output to the screen. You need to create a graphics object from the image with

Graphics g = Graphics.FromImage(pictureBox1.Image);

If you add a rectangle with that graphics object it should work.

Edit:

Replace the line where you save the image with this:

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

You are drawing the rectangle on the screen where the bitmap just have been drawn by the regular Paint event of the control. That doesn't affect the bitmap.

You can use the DrawToBitmap method to render the control to a bitmap. That will include the rectangle, and it will also include any scaling/cropping that the control does when it renders the image:

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

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