zey*_*zey 5 c# asp.net resize image
我得到一个图像文件,重新调整大小,然后使用不同的名称保存在同一个文件夹中(文件名+" - 调整大小"),但是我收到此错误
A generic error occurred in GDI+
Run Code Online (Sandbox Code Playgroud)
这是我的调整大小方法的代码,
private string resizeImageAndSave(string imagePath)
{
System.Drawing.Image fullSizeImg
= System.Drawing.Image.FromFile(Server.MapPath(imagePath));
var thumbnailImg = new Bitmap(565, 290);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, 565, 290);
thumbGraph.DrawImage(fullSizeImg, imageRectangle);
string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "-resize");
thumbnailImg.Save(targetPath, ImageFormat.Jpeg); //(A generic error occurred in GDI+) Error occur here !
thumbnailImg.Dispose();
return targetPath;
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何解决它?
正如其他人所说,它可能是权限问题,或者目录可能不存在.但是,您可以尝试在保存之前克隆图像.如果以上不是问题,这可以解决问题.
private static string resizeImageAndSave(string imagePath)
{
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imagePath));
var thumbnailImg = new Bitmap(565, 290);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, 565, 290);
thumbGraph.DrawImage(fullSizeImg, imageRectangle);
fullSizeImg.Dispose(); //Dispose of the original image
string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "-resize");
Bitmap temp = thumbnailImg.Clone() as Bitmap; //Cloning
thumbnailImg.Dispose();
temp.Save(targetPath, ImageFormat.Jpeg);
temp.Dispose();
return targetPath;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2162 次 |
| 最近记录: |