跪求ASP.NET大神帮我渡过难关~万分感谢!

beautifullysummer 2012-03-28 05:07:08
我想在后台通过图片的绝对路径修改图片大小,然后覆盖原来的图片,但是总是不能实现

public void MakeThumbnail(string imgPath_old, int width, int height)
{
//这种FromFile的打开方式会让文件一直打开,不能覆盖,所以跪求大神提供可以覆盖的方法
System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath_old);
int towidth = width; int toheight = height;
int x = 0; int y = 0; int ow = img.Width;
int oh = img.Height;
// 按值较大的进行等比缩放(不变形)
if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
{
toheight = height;
towidth = img.Width * height / img.Height;
}
else
{
towidth = width;
toheight = img.Height * width / img.Width;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
//以jpg格式保存缩略图(这一块保存的时候要报错,因为文件被打开了,不能保存到原来的位置,也就不能覆盖了
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
g.Dispose();
}
...全文
234 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
我才天生 2012-08-14
  • 打赏
  • 举报
回复
3楼正解,高人呀。
孟子E章 2012-03-28
  • 打赏
  • 举报
回复
System.IO.File.ReadAllBytes是不会占用的
beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]
还是要报错GDI+ 中发生一般性错误。

先确保您的文件没有被占用

你可以换一个文件测试
[/Quote]对了,就是文件被占用了~
beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

测试是没问题的
C# code
public void MakeThumbnail(string imgPath_old, int width, int height)
{
//[color=#FF0000]这种FromFile的打开方式会让文件一直打开,不能覆盖,所以跪
System.Drawing.Image img = System.Drawing.Image.From……
[/Quote]我的报错了,报的是GDI+中一般错误,然后找了资料看不懂 在开发.NET应用中,使用 System.Drawing.Image.Save 方法而导致“GDI+ 中发生一般性错误”的发生,通常有以下三种原因:
1. 相应的帐户没有写权限。
解决方法:赋予 NETWORK SERVICE 帐户以写权限。
2. 指定的物理路径不存在。
解决方法:
在调用 Save 方法之前,先判断目录是否存在,若不存在,则创建。
if (!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
3. 保存的文件已存在并因某种原因被锁定。
解决方法:
重启IIS,解除锁定。并在代码中使用 using 语句,确保释放 Image 对象所使用的所有资源。

我遇到的情况:
在先用openFileDialog打开图片文件,然后用saveFileDialog保存文件时就出现了 “GDI+中发生一般性错误”,我当时就想到是打开的文件还没有释放出来,于是用openFileDialog1.Dispose()来释放,可是没有成功。同样从一个MemorySream 实例打开一个Image后,立即关闭了这个流,结果在Image.Save时也会发生这种错误。我“摆渡”了很久都是遇到和我一样问题的人,CSDN上面的同志也没有给出一个实用的答案。最后终于还是在微软的网站上找到了答案:(以下是官方解决办法)
症状
Bitmap 对象或一个 图像 对象从一个文件, 构造时该文件仍保留锁定对于对象的生存期。 因此, 无法更改图像并将其保存回它产生相同的文件。
孟子E章 2012-03-28
  • 打赏
  • 举报
回复
还是要报错GDI+ 中发生一般性错误。

先确保您的文件没有被占用

你可以换一个文件测试
beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

C# code

bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();

调整下顺序
img.Dispose();
bitmap.Save(imgPath_old, System.Drawing.Im……
[/Quote]这个不行啊~还是要报错GDI+ 中发生一般性错误。
孟子E章 2012-03-28
  • 打赏
  • 举报
回复
测试是没问题的

public void MakeThumbnail(string imgPath_old, int width, int height)
{
//这种FromFile的打开方式会让文件一直打开,不能覆盖,所以跪
System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(imgPath_old)));

int towidth = width; int toheight = height;
int x = 0; int y = 0; int ow = img.Width;
int oh = img.Height;
// 按值较大的进行等比缩放(不变形)
if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)
{
toheight = height;
towidth = img.Width * height / img.Height;
}
else
{
towidth = width;
toheight = img.Height * width / img.Width;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
//以jpg格式保存缩略图([color=#FF0000]这一块保存的时候要报错,因为文件被打开了,不能保存到原来的位置,也就不能覆盖了

bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();
g.Dispose();
}

protected void Page_Load(object sender, EventArgs e)
{

MakeThumbnail(Server.MapPath("~/aaa.jpg"), 100, 100);
}
边城的刀声 2012-03-28
  • 打赏
  • 举报
回复

bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
img.Dispose();

调整下顺序
img.Dispose();
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

C# code
img.Dispose();
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
[/Quote]谢谢~我试试你的~
beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 5 楼 的回复:

不行~我试了

我试了,还真行
img.Dispose();
[/Quote]我用了3楼的方法,很好~你的一会儿我再试试~
beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath_old);
改成
System.Drawing.Image img = System.Drawing.Image.FromStream(newMemoryStream(File.ReadAllBytes(imgPath_old)));
[/Quote]貌似还是报错:GDI+ 中发生一般性错误。
bdmh 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

不行~我试了
[/Quote]
我试了,还真行
img.Dispose();
bidisty 2012-03-28
  • 打赏
  • 举报
回复
img.Dispose();
bitmap.Save(imgPath_old, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();

beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

img用完释放掉呢
[/Quote]不行~我试了
beautifullysummer 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

引用楼主 的回复:
我想在后台通过图片的绝对路径修改图片大小,然后覆盖原来的图片,但是总是不能实现
C# code

public void MakeThumbnail(string imgPath_old, int width, int height)
{
//[color=#FF0000]这种FromFile的打开方式会让文件一直打开,不……
[/Quote]
打DOTA打多了....习惯了~这个你会不会?
孟子E章 2012-03-28
  • 打赏
  • 举报
回复
System.Drawing.Image img = System.Drawing.Image.FromFile(imgPath_old);
改成
System.Drawing.Image img = System.Drawing.Image.FromStream(newMemoryStream(File.ReadAllBytes(imgPath_old)));
bdmh 2012-03-28
  • 打赏
  • 举报
回复
img用完释放掉呢
SomethingJack 2012-03-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
我想在后台通过图片的绝对路径修改图片大小,然后覆盖原来的图片,但是总是不能实现
C# code

public void MakeThumbnail(string imgPath_old, int width, int height)
{
//[color=#FF0000]这种FromFile的打开方式会让文件一直打开,不能覆盖,所以跪求大神提供可以覆盖……
[/Quote]
不是和你说过了 不要用跪求么 汗 技术不是跪求来的 是靠自己学习和领悟的 能帮的一定帮 算不上大神

62,046

社区成员

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

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

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

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