C# 错误:GDI+;中发生一般错误;。在保存图像时

C# 错误:GDI+;中发生一般错误;。在保存图像时,c#,.net,C#,.net,当我试图保存图像时出现此错误 GDI+中发生一般错误。 我搜索了此错误并检查了此文件夹的写入权限,还检查了图像文件是否未被其他任何东西(包括我的代码)使用,但当我试图保存图像时,仍然收到相同的错误,错误在哪里 public partial class AdsMaster : Form { OpenFileDialog ofd=null; public AdsMaster() { InitializeComponent

当我试图保存图像时出现此错误
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());
        }

      }        
} 

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

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

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

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

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

检查此处的解决方案:可能是您试图将图像保存到加载图像的同一文件中(图像文件是否在应用程序目录中)?我尝试从另一个文件夹保存图像,但仍然收到相同的错误谢谢,帮助了我!虽然我所要做的就是从现有的位图中创建一个新的位图。