第一步,添加一个一般处理程序(Handler),本例是ImageHandler
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mime;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
/// <summary>
/// Summary description for ImageHandler
/// </summary>
public class ImageHandler : IHttpHandler
{
public ImageHandler()
{
}
public string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return String.Empty;
}
public ImageFormat GetImageFormat(String path)
{
switch (Path.GetExtension(path).ToLower())
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default: return null;
}
}
protected byte[] WatermarkImage(HttpContext context)
{
byte[] imageBytes = null;
if (File.Exists(context.Request.PhysicalPath))
{
// Normally you'd put this in a config file somewhere.
string watermark = "世复检测";
Image image = Image.FromFile(context.Request.PhysicalPath);
Graphics graphic;
if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
{
// Graphic is not a Indexed (GIF) image
graphic = Graphics.FromImage(image);
}
else
{
/* Cannot create a graphics object from an indexed (GIF) image.
* So we're going to copy the image into a new bitmap so
* we can work with it. */
Bitmap indexedImage = new Bitmap(image);
graphic = Graphics.FromImage(indexedImage);
// Draw the contents of the original bitmap onto the new bitmap.
graphic.DrawImage(image, 0, 0, image.Width, image.Height);
image = indexedImage;
}
graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;
Font myFont = new Font("Arial", 15);
SolidBrush brush = new SolidBrush(Color.FromArgb(255, Color.Red));
/* This gets the size of the graphic so we can determine
* the loop counts and placement of the watermarked text. */
SizeF textSize = graphic.MeasureString(watermark, myFont);
//// Write the text across the image.
//for (int y = 0; y < image.Height; y++)
//{
// for (int x = 0; x < image.Width; x++)
// {
// PointF pointF = new PointF(x, y);
// graphic.DrawString(watermark, myFont, brush, pointF);
// x += Convert.ToInt32(textSize.Width);
// }
// y += Convert.ToInt32(textSize.Height);
//}
// Write the text at the right bottom of the image.
for (int y = image.Height-25; y < image.Height; y++)
{
for (int x = image.Width-100; x < image.Width; x++)
{
PointF pointF = new PointF(x, y);
graphic.DrawString(watermark, myFont, brush, pointF);
x += Convert.ToInt32(textSize.Width);
}
y += Convert.ToInt32(textSize.Height);
}
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, GetImageFormat(context.Request.PhysicalPath));
imageBytes = memoryStream.ToArray();
}
}
return imageBytes;
}
#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.PhysicalPath);
byte[] imageBytes = WatermarkImage(context);
if (imageBytes != null)
{
context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
}
else
{
// No bytes = no image which equals NO FILE.
// Therefore send a 404 - not found response.
context.Response.StatusCode = 404;
}
context.Response.End();
}
#endregion
}
第二步,在web.config里添加如下代码:
复制代码 代码如下:
<httpHandlers>
<!--<add verb="GET" type="ImageHandler" path="*.jpg,*.png,*.gif,*.bmp"/>-->
<add verb="GET" type="ImageHandler" path="Uploads/*/*.jpg"/>
</httpHandlers>
asp.net,图片水印
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 林俊吉.2012-将你惜命命【美华】【WAV+CUE】
- 晓雅《分享》DTS-WAV
- 黑鸭子2008-飞歌[首版][WAV+CUE]
- 黄乙玲1989-水泼落地难收回[日本天龙版][WAV+CUE]
- 周深《反深代词》[先听版][FLAC/分轨][310.97MB]
- 姜育恒1984《什么时候·串起又散落》台湾复刻版[WAV+CUE][1G]
- 那英《如今》引进版[WAV+CUE][1G]
- 蔡幸娟.1991-真的让我爱你吗【飞碟】【WAV+CUE】
- 群星.2024-好团圆电视剧原声带【TME】【FLAC分轨】
- 陈思安.1990-国语钢琴酒吧5CD【欣代唱片】【WAV+CUE】
- 莫文蔚《莫后年代20周年世纪典藏》3CD[WAV+CUE][2G]
- 张惠妹《我要快乐》华纳[WAV+CUE][1G]
- 罗大佑1982《之乎者也》无法盗版的青春套装版 [WAV+CUE][1G]
- 曾庆瑜1989-款款柔情[日本东芝版][WAV+CUE]
- Scelsi-IntegraledesquatuorsacordesetTrioacordes-QuatuorMolinari(2024)[24bit-WAV]