所以,我正在研究我的油漆应用程序。每次我进行更改时,当前屏幕状态都会被复制并保存为磁盘上的位图图像(因此我可以在绘制事件中使用它)。
当我最小化窗口并将其返回到正常状态,然后尝试绘制时,会出现此问题。这会触发我的事件对更改做出反应,程序尝试将图像保存---->>> kabooom。
它说"GDI+中发生一般错误"。所以,我一直在浏览各种论坛寻找答案,但没有一个给我真正的答案,他们都提到了错误的路径等,但我很确定这不是问题。我是否必须释放位图或对流执行某些操作?
int width = pictureBox1.Size.Width;
int height = pictureBox1.Size.Height;
Point labelOrigin = new Point(0, 0); // this is referencing the control
Point screenOrigin = pictureBox1.PointToScreen(labelOrigin);
int x = screenOrigin.X;
int y = screenOrigin.Y;
Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(x, y), Point.Empty, bounds.Size);
}
bitmap.Save(_brojFormi + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
您要将映像保存到磁盘,以便可以在其他事件中使用它?哇。
为什么不只使用类全局变量来存储位图?
class MyForm
{
Bitmap currentImage = null;
Graphics gfx = null;
private void btnLoad_Click(object sender, EventArgs e)
{
// ...
currentImage = new Bitmap(fileName);
gfx = Graphics.FromImage(currentImage);
}
private void pbEditor_Paint(object sender, PaintEventArgs e)
{
if (currentImage != null && gfx != null)
{
lock(currentImage) e.Graphics.DrawImage(currentImage, ...);
}
}
private void pbEditor_Click(object sender, MouseEventArgs e)
{
// quick example to show bitmap drawing
if (e.Button == MouseButtons.Left)
lock(currentImage) currentImage.SetPixel(e.Location.X, e.Location.Y, Colors.Black);
}
}