首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误: GDI+中出现一般错误。保存图像时

错误: GDI+中出现一般错误。保存图像时
EN

Stack Overflow用户
提问于 2013-09-03 16:05:23
回答 2查看 19.1K关注 0票数 4

当我尝试保存图像获取此错误A generic error occurred in GDI+时,我搜索了此错误并检查了此文件夹的写入权限,还检查了图像文件未被其他任何内容(包括我的代码)使用,但当我尝试保存图像时仍收到相同的错误,错误在哪里

代码语言:javascript
复制
public partial class AdsMaster : Form
    {
        OpenFileDialog ofd=null;
        public AdsMaster()
        {
            InitializeComponent();
        }

    private void browseButton_Click(object sender, EventArgs e)
    {
        ofd = new OpenFileDialog();
        ofd.Filter = "image files|*.jpg;*.jpeg;*.gif;*.png;*.bmp";
        DialogResult dr = new DialogResult();
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Image img = new Bitmap(ofd.FileName);//create the bitmap
            string imgName = ofd.SafeFileName;
            txtImagePath.Text = imgName;
            pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
            ofd.RestoreDirectory = true;
            img.Dispose();
        }
    }

    private void saveImage_Click(object sender, EventArgs e)
    {
        String str = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        string path = str + "\\Image\\";
        Image img = new Bitmap(ofd.FileName);
        string imgName = ofd.SafeFileName;
        try
        {
               img.Save(path + imgName); //getting error at this line 
               MessageBox.Show("Image is saved");
               img.Dispose();  // dispose of your image                   
        }
        catch(Exception err)
        {
            MessageBox.Show(err.Message.ToString());
        }

      }        
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-03 19:44:09

我认为这里的问题可能是由于在调用

代码语言:javascript
复制
Image img = new Bitmap(ofd.FileName);

下面的代码应该可以解决这个问题

代码语言:javascript
复制
private void saveImage_Click(object sender, EventArgs e)
{
    string str = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    string path = str + "\\Image\\";
    string imgName = ofd.SafeFileName;

    try
    {
        File.Copy(ofd.FileName, path + imgName);

        MessageBox.Show("Image is saved");
    }
    catch (Exception err)
    {
        MessageBox.Show(err.Message.ToString());
    }
}        
票数 4
EN

Stack Overflow用户

发布于 2015-06-04 18:47:23

我通过将文件名和路径更改为存储文件的位置来解决此错误。你需要检查其中的任何一个-

1- FileName应该不同/不应该与您要存储新文件/映像的位置上已经存在的其他文件名匹配。

2-更改操作系统驱动器中新文件的(保存)位置。尽量避免将新文件/映像存储在已安装操作系统的驱动器上。

下面的代码适用于我

代码语言:javascript
复制
         IWebdriver driver;
         driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32"); 
         driver.Navigate().GoToUrl("http://www.gmail.com");
         driver.Manage().Window.Maximize();

         Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile("e:\\pande", System.Drawing.Imaging.ImageFormat.Jpeg);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18587094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档