获取 System.Runtime.InteropServices.ExternalException!
我正在尝试使用鼠标单击事件绘制图像并保存它。我添加了一个按钮来撤消上次的绘制操作。我通过鼠标单击事件加载以前保存的图像来执行此操作。我这里有一个代码...我将展示在代码中的注释中出现异常的部分:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
rect.Width = 0;
rect.Height = 0;
pictureBox1.Invalidate();
pictureBox1.Image.Save(String.Format("{0}.Bmp",textBox2.Text )); //getting exception here!!
int radius = 10; //Set the number of pixel you want to use here
//Calculate the numbers based on radius
int x0 = Math.Max(e.X - (radius / 2), 0),
y0 = Math.Max(e.Y - (radius / 2), 0),
x1 = Math.Min(e.X + (radius / 2), pictureBox1.Width),
y1 = Math.Min(e.Y + (radius / 2), pictureBox1.Height);
Bitmap bm = pictureBox1.Image as Bitmap; //Get the bitmap (assuming it is stored that way)
for (int ix = x0; ix < x1; ix++)
{
for (int iy = y0; iy < y1; iy++)
{
bm.SetPixel(ix, iy, Color.Black); //Change the pixel color, maybe should be relative to bitmap
}
}
pictureBox1.Refresh(); //Force refresh
}
按钮下的代码是:
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Load(string.Format("{0}.Bmp",textBox2.Text));
}
在我的程序中,我尝试先保存图像,然后绘制它。当我单击该按钮时,它正在工作&加载图像,但是当我再次尝试绘制它时,出现异常。请帮助我需要更改代码的地方。
I am trying to paint the image and save it by using the mouse click event. I added a button to undo the last paint operation. I am performing this operation by loading the previously saved image by mouse click event. I have a code here... I'll show the part where i get the exception in comments in the code:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
rect.Width = 0;
rect.Height = 0;
pictureBox1.Invalidate();
pictureBox1.Image.Save(String.Format("{0}.Bmp",textBox2.Text )); //getting exception here!!
int radius = 10; //Set the number of pixel you want to use here
//Calculate the numbers based on radius
int x0 = Math.Max(e.X - (radius / 2), 0),
y0 = Math.Max(e.Y - (radius / 2), 0),
x1 = Math.Min(e.X + (radius / 2), pictureBox1.Width),
y1 = Math.Min(e.Y + (radius / 2), pictureBox1.Height);
Bitmap bm = pictureBox1.Image as Bitmap; //Get the bitmap (assuming it is stored that way)
for (int ix = x0; ix < x1; ix++)
{
for (int iy = y0; iy < y1; iy++)
{
bm.SetPixel(ix, iy, Color.Black); //Change the pixel color, maybe should be relative to bitmap
}
}
pictureBox1.Refresh(); //Force refresh
}
the code under button is:
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Load(string.Format("{0}.Bmp",textBox2.Text));
}
In my program I tried to save the image first and then painted it. When I click the button it is working & loading the image, but when I again tried to paint it, there I am getting the exception. Please help where I need to change the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是当
pictureBox1.Image
对象正在使用该文件时,您正尝试将图像保存到文件中。模拟问题:
您可以声明一个私有
Image
字段并在加载时将图像加载到其中,然后将图像保存到其中,而不是使用相同的图像文件来保留图像先前的状态以进行撤消将其保存到您的pictureBox1.Image
中以进行撤消。然而,要实现强大的多重撤消/重做,< strong>这里是一个适合您情况的好例子。
The problem is that you are trying to save the image to a file while the file is in use by the
pictureBox1.Image
object.To simulate the problem:
Instead of using the same image file to keep the image previous state for undo, you can declare a private
Image
field and load image to it whenever loading, and save image to it instead of saving it to yourpictureBox1.Image
for undo.However to implement a powerful multi-Undo/Redo, here is a good example that fits your case.