GDI 图片加字的重新保存的问题

Jaron 2005-02-16 02:23:48
if (System.IO.File.Exists(Server.MapPath(strSourcefile)))
{
System.Drawing.Image OldImage = new Bitmap(System.Drawing.Image.FromFile(Server.MapPath(strSourcefile)));
string strFileExt = strSourcefile.Substring(strSourcefile.LastIndexOf("."));
intImageWidth = OldImage.Width;
intImageHeight = OldImage.Height;

System.Drawing.Brush PhotoBrush = new TextureBrush(OldImage);

System.Drawing.Bitmap Img = new System.Drawing.Bitmap(intImageWidth,intImageHeight);
Graphics g = Graphics.FromImage(Img);
Color Old_Color = ColorTranslator.FromHtml("#FFFFFF");
g.Clear(Old_Color);
g.FillRectangle(PhotoBrush,0,0,intImageWidth,intImageHeight);
Color Advertisement_Color = ColorTranslator.FromHtml("#FFFFFF");
if (boolIsBlod) MyFontStyle_A = (FontStyle)System.Enum.Parse(typeof(FontStyle),"Bold");
else MyFontStyle_A = FontStyle.Regular;
Font AddString_Font = new Font(strAdd_ons_Word_fontname,float.Parse(intAdd_ons_Word_FontSize.ToString()),MyFontStyle_A);

string strFontWidth = g.MeasureString(strCopyRight, AddString_Font).Width.ToString();
string strFontHeight = g.MeasureString(strCopyRight, AddString_Font).Height.ToString();
int intFontWidth = (int)g.MeasureString(strCopyRight, AddString_Font).Width;
int intFontHeight = (int)g.MeasureString(strCopyRight, AddString_Font).Height;
int intCPY_X = (intImageWidth - intFontWidth)/2 + intAdd_ons_Word_X;
int intCPY_Y = (intImageHeight - intFontHeight)/2 + intAdd_ons_Word_Y;
if (intCPY_X <= 0) intCPY_X = 1;
if (intCPY_Y <= 0) intCPY_Y = 1;

Point Advertisement_Point = new Point(intCPY_X,intCPY_Y);
g.DrawString(strCopyRight,AddString_Font,new System.Drawing.SolidBrush(Advertisement_Color),Advertisement_Point);
System.IO.MemoryStream ms=new System.IO.MemoryStream();

Img.Save(Server.MapPath(strSourcefile),System.Drawing.Imaging.ImageFormat.Jpeg); //此行出错

OldImage.Dispose();
PhotoBrush.Dispose();

g.Dispose();
Img.Dispose();
Response.End();
}


GDI+ 中发生一般性错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Runtime.InteropServices.ExternalException: GDI+ 中发生一般性错误。

已经排除路径和权限的问题,更名后可以保存,不知道问题出在哪儿了。。。
...全文
237 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
programmer11 2005-02-19
  • 打赏
  • 举报
回复
up
minghui000 2005-02-19
  • 打赏
  • 举报
回复
up
webserv2 2005-02-19
  • 打赏
  • 举报
回复
if(UploadFile.PostedFile.FileName.Trim()!="")
{
//上传文件
string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
string path = Server.MapPath(".") + "/UploadFile/" + fileName + extension;
UploadFile.PostedFile.SaveAs(path);

//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana", 32);
Brush b = new SolidBrush(Color.White);
string addText = AddText.Value.Trim();
g.DrawString(addText, f, b, 10, 10);
g.Dispose();

//加图片水印
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath(".") + "/Alex.gif");
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();

//保存加水印过后的图片,删除原始图片
string newPath = Server.MapPath(".") + "/UploadFile/" + fileName + "_new" + extension;
image.Save(newPath);
image.Dispose();
if(File.Exists(path))
{
File.Delete(path);
}

Response.Redirect(newPath);
}

Jaron 2005-02-19
  • 打赏
  • 举报
回复
准备结贴,有意见的继续补充。。。
Jaron 2005-02-17
  • 打赏
  • 举报
回复
..
fds2003 2005-02-17
  • 打赏
  • 举报
回复
up
Jaron 2005-02-17
  • 打赏
  • 举报
回复
不知道我的方法为什么不行,用 fromstream 解决了

FileStream fs = new FileStream(sFile,FileMode.Open,FileAccess.Read);
System.Drawing.Image OldImage = new Bitmap(fs);
噯卟釋手 2005-02-17
  • 打赏
  • 举报
回复
up
Jaron 2005-02-17
  • 打赏
  • 举报
回复
string sFile = Server.MapPath(strSourcefile);
if (System.IO.File.Exists(sFile))
{
System.Drawing.Image OldImage = new Bitmap(System.Drawing.Image.FromFile(sFile));
intImageWidth = OldImage.Width;
intImageHeight = OldImage.Height;
OldImage.Dispose();
System.IO.File.Delete(sFile);
}

连这样都不行。。。


该进程无法访问文件“D:\OA\2005-02\images\2005021516044_35164.jpg”,因为该文件正由另一进程使用。
saucer 2005-02-17
  • 打赏
  • 举报
回复
FromFile seems to lock the file, try to use FileStream to open your file, then use FromStream method to create your bitmap, and then specifically close the FileStream
bidisty 2005-02-16
  • 打赏
  • 举报
回复
g.Dispose();
先放掉,
然后保存img.save
amendajing 2005-02-16
  • 打赏
  • 举报
回复
up
Jaron 2005-02-16
  • 打赏
  • 举报
回复
谢谢思归!

System.Drawing.Image OldImage = new Bitmap(System.Drawing.Image.FromFile(Server.MapPath(strSourcefile)));

and

Img.Save(Server.MapPath(strSourcefile),System.Drawing.Imaging.ImageFormat.Jpeg);

文件名是一样的。先读出旧图片,图片加字后,重新保存

OldImage.Dispose();
Img.Save(Server.MapPath(strSourcefile),System.Drawing.Imaging.ImageFormat.Jpeg);

写成这样还是一样的错误。不知道是什么原因。。。。
dsclub 2005-02-16
  • 打赏
  • 举报
回复
把原来的文件先关了!释放掉控制权!GDI就是这个样子,什么资源不用了都要及时释放。
saucer 2005-02-16
  • 打赏
  • 举报
回复
have you tried?

string sFile = Server.MapPath(strSourcefile);

System.Drawing.Image OldImage = new Bitmap(System.Drawing.Image.FromFile(sFile));

...

OldImage.Dispose();
System.IO.File.Delete(sFile);
g.Dispose();
Img.Save(sFile,ImageFormat.Jpeg);
Jaron 2005-02-16
  • 打赏
  • 举报
回复
不知道为何无法关闭。
njuzgj 2005-02-16
  • 打赏
  • 举报
回复
帮顶吧
nga96 2005-02-16
  • 打赏
  • 举报
回复
UP,
liulxmooo 2005-02-16
  • 打赏
  • 举报
回复
up
流梓 2005-02-16
  • 打赏
  • 举报
回复
string strDotName=File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.ToString().Length-4,4);
Random ro = new Random(System.DateTime.Now.GetHashCode());
string strFileName="image"+ro.Next()+strDotName;
File1.PostedFile.SaveAs(this.Server.MapPath("upload/"+strFileName));//图片上传结束,下面生成缩略图
System.Drawing.Image.GetThumbnailImageAbort myCallback =new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
Bitmap myBitmap = new Bitmap(this.Server.MapPath("upload/")+"\\"+strFileName);
Bitmap tomap=new Bitmap(Server.MapPath("images/logo.gif"));
System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(200,150,myCallback,IntPtr.Zero);
Response.ContentType="image/jpeg";
Bitmap newbmp=new Bitmap(myThumbnail);
Graphics g=Graphics.FromImage(newbmp);
g.DrawImageUnscaled(tomap,0,0,200,150);

Random ro1 = new Random(System.DateTime.Now.GetHashCode());
string strName="image"+ro1.Next()+".jpg";

newbmp.Save(this.Server.MapPath("upload/"+strName),System.Drawing.Imaging.ImageFormat.Jpeg);
myBitmap.Dispose();
tomap.Dispose();
newbmp.Dispose();
加载更多回复(4)

62,039

社区成员

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

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

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

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