我读了同一个问题的答案,并做了所要求的任何事情。我也给当前用户的文件夹写入权限,如前面的答案之一所述,但仍然出现此错误。所以请任何人都能为我提供具体的答案。 这是我的代码保存图像时发生GDI +的一般错误图片
protected void Page_Load(object sender, EventArgs e)
{
//get all the files in a directory
string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");
//combine them into one image
System.Drawing.Bitmap stitchedImage = Combine(files);
//save the new image
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);//Error:-A generic error occurred in GDI+
}
public static System.Drawing.Bitmap Combine(string[] files)
{
//read all images into memory
List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
System.Drawing.Bitmap finalImage = null;
try
{
int width = 0;
int height = 0;
foreach (string image in files)
{
//create a Bitmap from the file and add it to the list
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
//update the size of the final bitmap
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;
images.Add(bitmap);
}
//create a bitmap to hold the combined image
finalImage = new System.Drawing.Bitmap(width, height);
//get a graphics object from the image so we can draw on it
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
{
//set background color
g.Clear(System.Drawing.Color.Black);
//go through each image and draw it on the final image
int offset = 0;
foreach (System.Drawing.Bitmap image in images)
{
g.DrawImage(image,
new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
offset += image.Width;
}
}
return finalImage;
}
catch (Exception ex)
{
if (finalImage != null)
finalImage.Dispose();
throw ex;
}
finally
{
//clean up memory
foreach (System.Drawing.Bitmap image in images)
{
image.Dispose();
}
}
}
}
有多大图片? –
您正在从'@“D:\ Naved \ BasicDotNet \ Images”加载图片,因此必须是一个目录 - 但是您可以['stitchedImage.Save(@“D:\ Naved \ BasicDotNet \ Images”, System.Drawing.Imaging.ImageFormat.Jpeg);'](https://msdn.microsoft.com/en-us/library/9t4syfhh%28v=vs.110%29.aspx) - 尝试覆盖pre-与*文件*具有相同名称的现有目录。尝试一个不同的名字。 Image.Save的'string'参数是要保存的文件,而不是要保存的目录。 – dbc