当我尝试保存图像获取此错误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());
}
}
} 发布于 2013-09-03 19:44:09
我认为这里的问题可能是由于在调用
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());
}
} 发布于 2015-06-04 18:47:23
我通过将文件名和路径更改为存储文件的位置来解决此错误。你需要检查其中的任何一个-
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);https://stackoverflow.com/questions/18587094
复制相似问题