【问题标题】:A generic error occured in GDI+ - saving bitmap to desktopGDI+ 中出现一般错误 - 将位图保存到桌面
【发布时间】:2015-11-05 00:39:12
【问题描述】:

我创建了一个分形绘图程序,并尝试在其中加入一个按钮来保存当前位图。我使用 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.");
            }
        }

【问题讨论】:

    标签: c# image bitmap save


    【解决方案1】:
    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 );
    

    【讨论】:

    • 我会接受这个作为答案 - 说我必须等待 7 分钟
    猜你喜欢
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多