0

所以,我正在研究我的油漆应用程序。每次我进行更改时,当前屏幕状态都会被复制并作为位图图像保存在我的磁盘上(这样我就可以在我的绘画事件中使用它)。

当我最小化并将窗口恢复到正常状态然后尝试绘制时,就会出现问题。这会触发我的事件对更改做出反应,程序会尝试保存图像---->>> 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);                
        }
4

1 回答 1

0

您正在将图像保存到磁盘,以便在其他活动中使用它?哇。

为什么不只使用类全局变量来存储位图?

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);
    }
}
于 2012-06-12T15:26:43.700 回答