62,046
社区成员
发帖
与我相关
我的任务
分享
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
/// <summary>
/// imagehandler1 的摘要说明
/// </summary>
public class imagehandler1 : IHttpHandler
{
public imagehandler1()
{
//
// TODO: 在此处添加构造函数逻辑
//
/// <summary>
}
#region IHttpHandler 成员
public bool IsReusable
{
get { return false; }
}
/// 会产生graphics异常的PixelFormat
/// </summary>
private static System.Drawing.Imaging.PixelFormat[] indexedPixelFormats = { System.Drawing.Imaging.PixelFormat.Undefined, System.Drawing.Imaging.PixelFormat.DontCare,
System.Drawing.Imaging.PixelFormat.Format16bppArgb1555, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, System.Drawing.Imaging.PixelFormat.Format4bppIndexed,
System.Drawing.Imaging.PixelFormat.Format8bppIndexed
};
/// <summary>
/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
/// </summary>
/// <param name="imgPixelFormat">原图片的PixelFormat</param>
/// <returns></returns>
private static bool IsPixelFormatIndexed(System.Drawing.Imaging.PixelFormat imgPixelFormat)
{
foreach (System.Drawing.Imaging.PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
}
return false;
}
public void ProcessRequest(HttpContext context)
{
string fn = context.Request.PhysicalPath;
using (System.Drawing.Image img = System.Drawing.Image.FromFile(fn))
{
//如果原图片是索引像素格式之列的,则需要转换
if (IsPixelFormatIndexed(img.PixelFormat))
{
Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(img, 0, 0);
}
Graphics gif = Graphics.FromImage(bmp);
gif.DrawString("全局添加水印", new Font("宋体", 20), new SolidBrush(Color.Red), new PointF(20.0f, bmp.Height - 80.0f));
gif.Dispose();
context.Response.ContentType = "image/jpeg";
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.End();
}
else //否则直接操作
{
Graphics gif = Graphics.FromImage(img);
gif.DrawString("全局添加水印", new Font("宋体", 20), new SolidBrush(Color.Red), new PointF(20.0f, img.Height - 80.0f));
gif.Dispose();
context.Response.ContentType = "image/jpeg";
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.End();
}
}
#endregion
}
}
public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
{
if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
{
return;
}
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
{
return;
}
Graphics graphics = Graphics.FromImage(originalImage);
int x = originalImage.Width - waterImage.Width - 10;
int y = originalImage.Height - waterImage.Height - 10;
int width = waterImage.Width;
int height = waterImage.Height;
graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
graphics.Dispose();
MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
/// <summary>
/// 对一个指定的图片加上文字水印效果。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterText">水印文字内容</param>
public static void CreateTextWaterMark(string imageFile, string waterText)
{
if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
{
return;
}
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
Graphics graphics = Graphics.FromImage(originalImage);
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);
float x = (float)originalImage.Width - waterTextSize.Width - 10F;
float y = (float)originalImage.Height - waterTextSize.Height - 10F;
graphics.DrawString(waterText, waterTextFont, brush, x, y);
graphics.Dispose();
brush.Dispose();
MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();
System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);
imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}