GDI+ 中出现一般性错误 - 将位图保存到桌面
A generic error occured in GDI+ - saving bitmap to desktop
我创建了一个分形绘图程序,并尝试在其中加入一个按钮来保存当前位图。我使用 mandelbrot() 方法,但按钮和代码似乎不起作用,它只是抛出错误。这是绘制和保存位图的代码:
我以为是文件夹权限的问题,所以加了代码保存到当前用户的桌面。但是还是一样的错误。
绘制位图
private Bitmap picture;
private Graphics g1;
private void Form1_Paint(object sender, PaintEventArgs e)
{
//put the bitmap on the window
Graphics windowG = e.Graphics;
windowG.DrawImageUnscaled(picture, 0, 0);
}
保存位图
private void savefractal_Click(object sender, EventArgs e)
{
try
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
picture.Save(path);
savefractal.Text = "Saved file.";
}
catch (Exception)
{
MessageBox.Show("There was a problem saving the file." +
" Check the file permissions.");
}
}
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
picture.Save(path)
这不是文件名,您传递的是目录路径,这是行不通的。
使用Path.Combine
:
String fileName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MyBitmap.bmp" );
picture.Save( fileName );
我创建了一个分形绘图程序,并尝试在其中加入一个按钮来保存当前位图。我使用 mandelbrot() 方法,但按钮和代码似乎不起作用,它只是抛出错误。这是绘制和保存位图的代码:
我以为是文件夹权限的问题,所以加了代码保存到当前用户的桌面。但是还是一样的错误。
绘制位图
private Bitmap picture;
private Graphics g1;
private void Form1_Paint(object sender, PaintEventArgs e)
{
//put the bitmap on the window
Graphics windowG = e.Graphics;
windowG.DrawImageUnscaled(picture, 0, 0);
}
保存位图
private void savefractal_Click(object sender, EventArgs e)
{
try
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
picture.Save(path);
savefractal.Text = "Saved file.";
}
catch (Exception)
{
MessageBox.Show("There was a problem saving the file." +
" Check the file permissions.");
}
}
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
picture.Save(path)
这不是文件名,您传递的是目录路径,这是行不通的。
使用Path.Combine
:
String fileName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MyBitmap.bmp" );
picture.Save( fileName );