c# - Image.FromFile("image_path") 抛出一些 JPEG 文件的内存不足异常
问题描述
从外部 URL 下载图像后,我正在生成缩略图。url 包含 JPEG 图像的路径。当我为 jpeg 图像创建缩略图时,已经为某些 JPEG 图像生成了缩略图,而没有为某些 JPEG 图像生成缩略图。我无法找到为什么它不适用于某些 JPEG 图像的根本原因。下面是相同的代码片段。
try
{
Image image = Image.FromFile("image_Path");
int srcWidth = image.Width;
int srcHeight = image.Height;
int thumbWidth = width;
int thumbHeight = 0;
Bitmap bmp = new Bitmap(72, 72);
if (srcHeight > srcWidth)
{
thumbHeight = (srcHeight / srcWidth) * thumbWidth;
bmp = new Bitmap(thumbWidth, thumbHeight);
}
else
{
thumbHeight = thumbWidth;
thumbWidth = (srcWidth / srcHeight) * thumbHeight;
bmp = new Bitmap(thumbWidth, thumbHeight);
}
Graphics gr = Graphics.FromImage(bmp);
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.InterpolationMode = InterpolationMode.High;
Rectangle rectDestination = new Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
bmp.Save(destinationfile);
bmp.Dispose();
image.Dispose();
}
catch (OutOfMemoryException ex)
{
}
finally
{
//bmp.Dispose();
//image.Dispose();
}
解决方案
推荐阅读
- angular - 找出在 Karma 测试期间哪个 Angular 组件发出错误
- python - 带有条件计算的汇总数据框。熊猫
- mysql - MYSQL 中缺少数据库记录
- reactjs - React Ag-grid 加载覆盖道具
- fastapi - FastAPI如何只允许特定IP的端点访问?
- performance - 为什么 Ext JS 网格面板中组的展开/折叠有一个小的延迟?
- node.js - Heroku 使用 .json 文件作为数据库
- python-3.x - 如何在多线程硒线chromedriver python中设置具有身份验证的代理?
- docusignapi - 如何通过 REST 或 SOAP 传输草稿信封
- django - Django:“用户”和“用户”的实例之间不支持“<”