簡體   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