bitmap保存问题

冰镇宝贝321 2013-07-04 01:11:04
求大神帮忙,我读取一张图片,想修改大小保存到另个文件夹中,报错
关键代码如下:
    public static void MakeThumNail(string originalImagePath, string thumNailPath, int width, int height, string model)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int thumWidth = width; //缩略图的宽度
int thumHeight = height; //缩略图的高度

int x = 0;
int y = 0;

int originalWidth = originalImage.Width; //原始图片的宽度
int originalHeight = originalImage.Height; //原始图片的高度

switch (model)
{
case "HW": //指定高宽缩放,可能变形
break;
case "W": //指定宽度,高度按照比例缩放
thumHeight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高度,宽度按照等比例缩放
thumWidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut":
if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight)
{
originalHeight = originalImage.Height;
originalWidth = originalImage.Height * thumWidth / thumHeight;
y = 0;
x = (originalImage.Width - originalWidth) / 2;
}
else
{
originalWidth = originalImage.Width;
originalHeight = originalWidth * height / thumWidth;
x = 0;
y = (originalImage.Height - originalHeight) / 2;
}
break;
default:
break;
}

//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth, thumHeight);

//新建一个画板
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);

//设置高质量查值法
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
graphic.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight),
new System.Drawing.Rectangle(x, y, originalWidth, originalHeight),
System.Drawing.GraphicsUnit.Pixel);
try
{
if (!Directory.Exists(thumNailPath))
{
Directory.CreateDirectory(thumNailPath);
}
bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);

}
catch (Exception ex)
{
throw ex;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
graphic.Dispose();
}
}


bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
就是到这一句,就产生错误,GDI+ 中发生一般性错误。

去拜读和骨骼都查了,有的说没权限,有的说没释放,但是我个人还是认为是没释放,求大神帮忙看下
...全文
289 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰镇宝贝321 2013-07-04
  • 打赏
  • 举报
回复
引用 5 楼 uinatlex 的回复:

if (!Directory.Exists(thumNailPath))
            {
                Directory.CreateDirectory(thumNailPath);
            }
            bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
这个是什么意思,判断路径不存在后生成文件夹,你后面又把文件SAVE到这个路径,当然要报错 删掉 或者更改
亲 这个回答是正确的,我也发现了,是路径不对,谢谢了 结贴
零点逸族 2013-07-04
  • 打赏
  • 举报
回复
是不是没有指定图片名称?
uinatlex 2013-07-04
  • 打赏
  • 举报
回复

if (!Directory.Exists(thumNailPath))
            {
                Directory.CreateDirectory(thumNailPath);
            }
            bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
这个是什么意思,判断路径不存在后生成文件夹,你后面又把文件SAVE到这个路径,当然要报错 删掉 或者更改
冰镇宝贝321 2013-07-04
  • 打赏
  • 举报
回复
在线等,求帮忙啊
冰镇宝贝321 2013-07-04
  • 打赏
  • 举报
回复
引用 2 楼 insus 的回复:
Refer this:
http://www.cnblogs.com/insus/articles/2060601.html

亲,您这个是不错,问题用了您的那个保存,还是出错的啊
insus 2013-07-04
  • 打赏
  • 举报
回复
冰镇宝贝321 2013-07-04
  • 打赏
  • 举报
回复
没人帮我

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧