.ashx 加水印 为什么有的图片能加成功,有的不成功呀!

sailei1 2010-11-08 01:14:07
<%@ WebHandler Language="C#" Class="WaterHandler" %>

using System;
using System.Web;
using System.Drawing;
using System.IO;
public class WaterHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

string waterPath = "~/images/WaterLogo.jpg";
string defaultPath = "~/images/logo.jpg";
string path = context.Server.MapPath( context.Request.QueryString["pic"].ToString());
Image image = null;
if (File.Exists(path))
{
//try
//{
image = Image.FromFile(path);
Image water = Image.FromFile(context.Server.MapPath(waterPath));
Graphics g = Graphics.FromImage(image);//在这报错
g.DrawImage(water, new Rectangle(image.Width - water.Width, image.Height - water.Height, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
//}
//catch (Exception)
//{

// image = Image.FromFile(context.Server.MapPath(defaultPath));
//}
}
else {
image = Image.FromFile(context.Server.MapPath(defaultPath));
}
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

public bool IsReusable {
get {
return false;
}
}

}
这是改路径的方法

public string water(object o) {
return "~/WaterHandler.ashx?pic=" + o.ToString() ;
}

希望 csdn 上 高手们,指点一下迷津!QQ861072515
...全文
122 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinshouno7 2010-11-09
  • 打赏
  • 举报
回复
web.config中也需要设置吧
<!--针对图片的文件夹进行处理-->
<location path="image">
<system.web>
<httpHandlers>
<!--对jpg文件添加水印-->

<add verb="*" path="*.jpg" type="WaterHandler"/>
<add verb="*" path="*.bmp" type="WaterHandler"/>
</httpHandlers>
</system.web>
</location>
wuhuabucai 2010-11-08
  • 打赏
  • 举报
回复
具体什么错误呢
sailei1 2010-11-08
  • 打赏
  • 举报
回复
路径没错 ,问教员 教员说是图片的错误! 我有点怀疑 所以发帖问问
Teng_s2000 2010-11-08
  • 打赏
  • 举报
回复
catch中获取文件了啊

不用try看是不是报路径的错误 啊
正在加载昵称 2010-11-08
  • 打赏
  • 举报
回复
DiscuzNT开源项目里有加水印代码,建议楼主参考一下
public void AddImageSignPic(Image img, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
{
Graphics g = Graphics.FromImage(img);
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Image watermark = new Bitmap(watermarkFilename);

if (watermark.Height >= img.Height || watermark.Width >= img.Width)
return;

ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();

colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

float transparency = 0.5F;
if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
transparency = (watermarkTransparency / 10.0F);


float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

int xpos = 0;
int ypos = 0;

switch (watermarkStatus)
{
case 1:
xpos = (int)(img.Width * (float).01);
ypos = (int)(img.Height * (float).01);
break;
case 2:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)(img.Height * (float).01);
break;
case 3:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)(img.Height * (float).01);
break;
case 4:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 5:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 6:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
break;
case 7:
xpos = (int)(img.Width * (float).01);
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 8:
xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
case 9:
xpos = (int)((img.Width * (float).99) - (watermark.Width));
ypos = (int)((img.Height * (float).99) - watermark.Height);
break;
}

g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType.IndexOf("jpeg") > -1)
ici = codec;
}
EncoderParameters encoderParams = new EncoderParameters();
long[] qualityParam = new long[1];
if (quality < 0 || quality > 100)
quality = 80;

qualityParam[0] = quality;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
encoderParams.Param[0] = encoderParam;

//if (ici != null)
// img.Save(filename, ici, encoderParams);
//else
// img.Save(filename);
if (ici != null)
img.Save(filename.Substring(0, filename.LastIndexOf('\\') + 1) + "#" + filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - 1 - filename.LastIndexOf('\\')), ici, encoderParams);
else
img.Save(filename.Substring(0, filename.LastIndexOf('\\') + 1) + "#" + filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - 1 - filename.LastIndexOf('\\')));

g.Dispose();
img.Dispose();
watermark.Dispose();
imageAttributes.Dispose();
}
sailei1 2010-11-08
  • 打赏
  • 举报
回复
不用 catch 就报错

62,272

社区成员

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

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

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

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