给图片加水印,然后覆盖原来的图片,为什么会报错误呢,在线等啊!!

generhappy 2012-04-27 10:29:18
下面的代码是我上传图片类,保存的代码

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";
HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
if (file != null)
{
if (!System.IO.Directory.Exists(uploadPath))
{
System.IO.Directory.CreateDirectory(uploadPath);
}
file.SaveAs(uploadPath + file.FileName);//这里是保存在服务器上,我想就在这句之后给图片加水印
string webFilePath = uploadPath + file.FileName;
string webFilePath_sy = uploadPath + file.FileName;

AddWater(webFilePath, webFilePath_sy);

context.Response.Write("1");
}
else
{
context.Response.Write("0");
}
}


public bool IsReusable
{
get
{
return false;
}
}


下面是我增加水印的代码

/// <summary>
/// 在图片上增加文字水印
/// </summary>
/// <param name="Path">原服务器图片路径</param>
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
public static void AddWater(string Path, string Path_sy)
{
string addText = "文字水印";

System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", 60);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);

g.DrawString(addText, f, b, 35, 35);
g.Dispose();
image.Save(Path_sy);
image.Dispose();
}


在上面上传的时候,在image.Save(Path_sy);这句报错,报错GDI一般性错误!
我想原因是保存的时候,覆盖原图片的问题,可能是原图片没有关闭,在线求解决办法啊!!
...全文
520 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
asd6y7tredfghu 2012-04-28
  • 打赏
  • 举报
回复
麻烦一点 A图生成缩略图B图,B图覆盖A图,删除B图,试试吧

File.Copy(B,A,True)

File.Delete(B)

qxyywy 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 的回复:]

老问题了
C# code

System.Drawing.Image image = System.Drawing.Image.FromFile(Path);


C# code

FileStream m_fsSource = File.OpenRead(sourcePicture);
System.Drawing.Image m_imgSource = System.Drawing.……
[/Quote]

没去看它的代码
superliu1122 2012-04-27
  • 打赏
  • 举报
回复
老问题了

System.Drawing.Image image = System.Drawing.Image.FromFile(Path);


FileStream m_fsSource = File.OpenRead(sourcePicture);
System.Drawing.Image m_imgSource = System.Drawing.Image.FromStream(m_fsSource, true);
m_fsSource.Close();

区别在这,System.Drawing.Image.FromFile方法就是有这个问题
qxyywy 2012-04-27
  • 打赏
  • 举报
回复
MakeWordWatemark(webFilePath, webFilePath_sy, "11111", "Arial", 18, "Green", true, 1.0f, 1);
LMAOhuaNL 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

建议 加水印成功后,删除原有图片!不要覆盖!
[/Quote]
对对我也觉得你这样做就ok了
尼古拉特斯拉 2012-04-27
  • 打赏
  • 举报
回复
这个函数没问题滴 我试过了
尼古拉特斯拉 2012-04-27
  • 打赏
  • 举报
回复
姐姐 1.0改成1.0f 人家是float类型的
generhappy 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

方法已经写的很明确了 那些参数是做什么的 实例这个自己去吧
[/Quote]

晕啊,我这样调用为什么报错参数匹配无效呢
MakeWordWatemark(webFilePath, webFilePath_sy, "11111", "Arial", 18, "Green", true, 1.0, 1);
qxyywy 2012-04-27
  • 打赏
  • 举报
回复
方法已经写的很明确了 那些参数是做什么的 实例这个自己去吧
generhappy 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

引用 9 楼 的回复:

引用 4 楼 的回复:

我自己是用的这个
Common.MakeWordWatemark(NewAllName, NewAllName, m_sSystem[6].ToString(), m_sSystem[9].ToString(), Convert.ToInt32(m_sSystem[7]), m_sSystem[8], m_sSystem[10] ……
[/Quote]

不好意思啊,可以给一个调用的实例么?
qxyywy 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 4 楼 的回复:

我自己是用的这个
Common.MakeWordWatemark(NewAllName, NewAllName, m_sSystem[6].ToString(), m_sSystem[9].ToString(), Convert.ToInt32(m_sSystem[7]), m_sSystem[8], m_sSystem[10] == "1" ? true : ……
[/Quote]

上文已经回复
Im_Sorry 2012-04-27
  • 打赏
  • 举报
回复
建议 加水印成功后,删除原有图片!不要覆盖!
generhappy 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

我自己是用的这个
Common.MakeWordWatemark(NewAllName, NewAllName, m_sSystem[6].ToString(), m_sSystem[9].ToString(), Convert.ToInt32(m_sSystem[7]), m_sSystem[8], m_sSystem[10] == "1" ? true : false, Convert.……
[/Quote]

Right() 函数是啥啊
qxyywy 2012-04-27
  • 打赏
  • 举报
回复
……

我上面的Right是通用从右截取 你觉得那个通用性更好呢

上面调用RIGHT函数只是获取一个后缀名 你要用其他的方法也没别的问题

但你上面的方法名和方法作用相同吗???
尼古拉特斯拉 2012-04-27
  • 打赏
  • 举报
回复
public static string Right(string strName)
{
int intExt = strName.LastIndexOf(".");
if (intExt != -1)
return strName.Substring(intExt).ToLower();
else
return "";
}
这样写哪个性能比较好
qxyywy 2012-04-27
  • 打赏
  • 举报
回复
这个很明显可以看出是截取字符串的

/// <summary>
/// 截取右边字符串
/// </summary>
/// <param name="value">截取字符串</param>
/// <param name="length">截取长度</param>
/// <returns>截取右边字符串</returns>
public static string Right(string value, int length)
{
if (length <= 0 || length > value.Length)
{
return value;
}
else
{
return value.Substring(value.Length - length);
}
}
尼古拉特斯拉 2012-04-27
  • 打赏
  • 举报
回复
Right()函数是啥
qxyywy 2012-04-27
  • 打赏
  • 举报
回复
我自己是用的这个
Common.MakeWordWatemark(NewAllName, NewAllName, m_sSystem[6].ToString(), m_sSystem[9].ToString(), Convert.ToInt32(m_sSystem[7]), m_sSystem[8], m_sSystem[10] == "1" ? true : false, Convert.ToSingle(m_sSystem[12]), Convert.ToInt32(m_sSystem[14]));

这是很久以前的代码了 MakeWordWatemark(NewAllName, NewAllName,……)这儿是传入的路径和存储的路径一样的
generhappy 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

C# code

/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="sourcePicture">源图片(绝对路径)</param>
/// <param name="targetPicture">生成图片(绝对路径)</param>
/// <par……
[/Quote]

你这个同样也会出现这个问题吧,你也是m_bmSource.Save(targetPicture, m_imgformatTarget);之后才释放的资源,如果新图片的路径与名称要完全覆盖老图片,也会报同样的错误的啊
qxyywy 2012-04-27
  • 打赏
  • 举报
回复

/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="sourcePicture">源图片(绝对路径)</param>
/// <param name="targetPicture">生成图片(绝对路径)</param>
/// <param name="watermarkWords">水印文字</param>
/// <param name="fontName">水印字体类型</param>
/// <param name="fontSize">水印字体大小</param>
/// <param name="fontColor">水印字体颜色</param>
/// <param name="fontBond">水印文字是否加粗</param>
/// <param name="watermarkAlpha">水印文字透明度(0.1-1.0数值越小透明度越高)</param>
/// <param name="watermarkPosition">水印图片位置(从1-9,1:左上;2:中上;3:右上;4:左中;5:正中;6:右中;7:左下;8:中下;9:右下)</param>
public static void MakeWordWatemark(string sourcePicture, string targetPicture, string watermarkWords, string fontName, int fontSize, string fontColor, Boolean fontBond, float watermarkAlpha, int watermarkPosition)
{
Color m_colorFontColor = new Color();
try
{
m_colorFontColor = ColorTranslator.FromHtml(fontColor);
}
catch
{
return;
}

if (sourcePicture == string.Empty || targetPicture == string.Empty || watermarkWords == string.Empty || watermarkAlpha <= 0.0 || watermarkAlpha > 1.0 || watermarkPosition < 1 || watermarkPosition > 9)
return;

string m_sSourceExtension = Path.GetExtension(sourcePicture).ToLower();
if (File.Exists(sourcePicture) == false || (m_sSourceExtension != ".gif" && m_sSourceExtension != ".jpg" && m_sSourceExtension != ".png" && m_sSourceExtension != ".bmp"))
return;

FileStream m_fsSource = File.OpenRead(sourcePicture);
System.Drawing.Image m_imgSource = System.Drawing.Image.FromStream(m_fsSource, true);
m_fsSource.Close();
int m_iSourceWidth = m_imgSource.Width;
int m_iSourceHeight = m_imgSource.Height;
Bitmap m_bmSource = new Bitmap(m_imgSource, m_iSourceWidth, m_iSourceHeight);
m_imgSource.Dispose();
m_bmSource.SetResolution(1000f, 1000f);
Graphics m_grSource = Graphics.FromImage(m_bmSource);
m_grSource.SmoothingMode = SmoothingMode.AntiAlias;
m_grSource.DrawImage(m_bmSource, new Rectangle(0, 0, m_iSourceWidth, m_iSourceHeight), 0, 0, m_iSourceWidth, m_iSourceHeight, GraphicsUnit.Pixel);

Font m_fontWords = null;
if (fontBond)
m_fontWords = new Font(fontName, (float)fontSize, FontStyle.Bold);
else
m_fontWords = new Font(fontName, (float)fontSize, FontStyle.Regular);

int m_iWordWidth = (int)(Encoding.Default.GetByteCount(watermarkWords) / 2 * fontSize * 1.6 + 6);
int m_iWordHeight = (int)(fontSize * 1.5 + 2);
System.Drawing.Image m_imgWord = new Bitmap(m_iWordWidth, m_iWordHeight);
System.Drawing.Graphics m_grWord = System.Drawing.Graphics.FromImage(m_imgWord);
SizeF m_sfWord = new SizeF();
m_sfWord = m_grWord.MeasureString(watermarkWords, m_fontWords);
m_imgWord.Dispose();
m_grWord.Dispose();

float m_fWatermarkX;
float m_fWatermarkY;
switch (watermarkPosition)
{
case 1:
m_fWatermarkX = 10;
m_fWatermarkY = 10;
break;
case 2:
m_fWatermarkX = (m_iSourceWidth - m_sfWord.Width) / 2;
m_fWatermarkY = 10;
break;
case 3:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = 10;
break;
case 4:
m_fWatermarkX = 10;
m_fWatermarkY = (m_iSourceHeight - m_sfWord.Height) / 2;
break;
case 5:
m_fWatermarkX = (m_iSourceWidth - m_sfWord.Width) / 2;
m_fWatermarkY = (m_iSourceHeight - m_sfWord.Height) / 2;
break;
case 6:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = (m_iSourceHeight - m_sfWord.Height) / 2;
break;
case 7:
m_fWatermarkX = 10;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
case 8:
m_fWatermarkX = (m_iSourceWidth - m_sfWord.Width) / 2;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
case 9:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
default:
m_fWatermarkX = m_iSourceWidth - m_sfWord.Width - 10;
m_fWatermarkY = m_iSourceHeight - m_sfWord.Height - 10;
break;
}
int m_iWatermarkAlpha = Convert.ToInt32(255 * watermarkAlpha);
SolidBrush m_sbTransBrush = new SolidBrush(Color.FromArgb(m_iWatermarkAlpha, m_colorFontColor));
m_imgWord = new Bitmap((int)m_sfWord.Width + 2, (int)m_sfWord.Height + 2);
m_grWord = Graphics.FromImage(m_imgWord);
m_grWord.DrawString(watermarkWords, m_fontWords, m_sbTransBrush, new PointF(0, 0));
ImageAttributes m_imgAttributes = new ImageAttributes();
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // red红色
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green绿色
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue蓝色
new float[] {0.0f, 0.0f, 0.0f, watermarkAlpha, 0.0f}, //透明度
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};//

ColorMatrix m_wmColorMatrix = new ColorMatrix(colorMatrixElements);
m_imgAttributes.SetColorMatrix(m_wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
m_grSource.DrawImage(m_imgWord, new Rectangle((int)m_fWatermarkX, (int)m_fWatermarkY, m_iWordWidth, m_iWordHeight), 0, 0, m_iWordWidth, m_iWordHeight, GraphicsUnit.Pixel);
m_grSource.Dispose();
ImageFormat m_imgformatTarget = ImageFormat.Jpeg;
switch (Right(targetPicture, 4).ToLower())
{
case ".gif":
m_imgformatTarget = ImageFormat.Gif; break;
case ".jpg":
m_imgformatTarget = ImageFormat.Jpeg; break;
case ".png":
m_imgformatTarget = ImageFormat.Png; break;
case ".bmp":
m_imgformatTarget = ImageFormat.Bmp; break;
default:
m_imgformatTarget = ImageFormat.Jpeg; break;
}

m_bmSource.Save(targetPicture, m_imgformatTarget);
m_fontWords.Dispose();
m_sbTransBrush.Dispose();
m_grSource.Dispose();
m_bmSource.Dispose();
}
加载更多回复(3)

62,268

社区成员

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

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

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

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