我现在试图保存图像在pictureBox1到我的硬盘,但它保存它没有矩形在pictureBox1为什么
本文关键字:pictureBox1 保存 为什么 我的 图像 硬盘 | 更新日期: 2023-09-27 18:08:02
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。保存,我试过bb。保存,但还是没有保存矩形。怎么了?
在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);
}
}
}
您正在屏幕上绘制矩形,其中位图刚刚由控件的常规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);
}
这是因为您没有更改图像,而是更改了屏幕的输出。您需要使用
从图像创建一个图形对象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);