保存图像时 GDI+ 中出现一般性错误
A generic error occurred in GDI+ when saving Image
我阅读了同一问题的答案并按要求做了。如前一个答案中所述,我还为当前用户授予了文件夹的写入权限,但仍然出现此错误。所以请任何人都可以为我提供具体的答案要做什么?
这是我的代码
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();
}
}
}
}
尝试
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\stitchedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
而不是
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);
Image.Save (String, ImageFormat)
的第一个参数是要保存到的文件 name,而不是要保存到的 directory .之前你做过
string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");
所以@"D:\Naved\BasicDotNet\Images"
一定是目录,不是文件。
(顺便说一下,我建议将拼接的图像保存到不同的目录;否则,每次您 运行 您的代码都会将之前的拼接与之前的内容拼接在一起。)
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg); - you need to specify the file name here. I.e edit it to something like this: `stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\myjpeg.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)`
我阅读了同一问题的答案并按要求做了。如前一个答案中所述,我还为当前用户授予了文件夹的写入权限,但仍然出现此错误。所以请任何人都可以为我提供具体的答案要做什么? 这是我的代码
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();
}
}
}
}
尝试
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\stitchedImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
而不是
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg);
Image.Save (String, ImageFormat)
的第一个参数是要保存到的文件 name,而不是要保存到的 directory .之前你做过
string[] files = Directory.GetFiles(@"D:\Naved\BasicDotNet\Images");
所以@"D:\Naved\BasicDotNet\Images"
一定是目录,不是文件。
(顺便说一下,我建议将拼接的图像保存到不同的目录;否则,每次您 运行 您的代码都会将之前的拼接与之前的内容拼接在一起。)
stitchedImage.Save(@"D:\Naved\BasicDotNet\Images", System.Drawing.Imaging.ImageFormat.Jpeg); - you need to specify the file name here. I.e edit it to something like this: `stitchedImage.Save(@"D:\Naved\BasicDotNet\Images\myjpeg.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)`