4

当我尝试保存图像时遇到此错误 A generic error occurred in GDI+ ,我搜索了此错误并检查了此文件夹的写入权限,并检查了图像文件没有被其他任何东西(包括我的代码)使用但是当我尝试保存图像时得到同样的错误,错误在哪里

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());
        }

      }        
} 
4

2 回答 2

4

我认为这里的问题可能是由于您调用时在原始图像上放置了锁

Image img = new Bitmap(ofd.FileName);

以下应该可以解决问题

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());
    }
}        
于 2013-09-03T11:44:09.087 回答
0

我通过更改文件名和文件存储路径来解决此错误。您需要检查其中之一-

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

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

下面的代码对我有用

         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);
于 2015-06-04T10:47:22.737 回答