ImageHandler1.ashx 
<%@ WebHandler Language="C#" CodeBehind="ImageHandler1.ashx.cs" Class="NET35Lab.GeneratedImage.Web.ImageHandler1" %> 
ImageHandler1.ashx.cs 
Code 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Microsoft.Web; 
using System.Collections.Specialized; 
using System.Data.SqlClient; 
using System.Drawing; 
namespace NET35Lab.GeneratedImage.Web 
{ 
 public class ImageHandler1 : ImageHandler 
 { 
 public ImageHandler1() 
 { 
 this.ImageTransforms.Add(new ImageResizeTransform { Width = 320, Mode = ImageResizeMode.Fit }); 
 this.ImageTransforms.Add(new CopyrightTransform { Text = "Guushuuse .NET" }); 
 this.EnableClientCache = true; 
 this.EnableServerCache = true; 
 } 
 public override ImageInfo GenerateImage(NameValueCollection parameters) 
 { 
 int imageID = int.Parse(parameters["imageid"]); 
 SqlConnection connection = new SqlConnection( 
 @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;"); 
 connection.Open(); 
 SqlCommand command = new SqlCommand("select image_data from t_images where image_id=" + imageID, connection); 
 SqlDataReader dr = command.ExecuteReader(); 
 dr.Read(); 
 return new ImageInfo((Byte[])dr[0]); 
 } 
 } 
 public class CopyrightTransform : ImageTransform 
 { 
 private const int VERTICAL_PADDING = 5; 
 private const int HORIZONAL_PADDING = 5; 
 public string Text { get; set; } 
 public Font Font { get; set; } 
 public Color FontColor { get; set; } 
 public CopyrightTransform() 
 { 
 //Assign Defaults 
 FontColor = Color.FromArgb(128, 255, 0, 0); 
 Font = new Font("Courier", 13); 
 } 
 public override System.Drawing.Image ProcessImage(System.Drawing.Image img) 
 { 
 Graphics gra = Graphics.FromImage(img); 
 SizeF sz = gra.MeasureString(Text, Font); 
 gra.DrawString(Text, Font, new SolidBrush(FontColor), img.Width - sz.Width - HORIZONAL_PADDING, img.Height - sz.Height - VERTICAL_PADDING); 
 return img; 
 } 
 public override string UniqueString 
 { 
 get 
 { 
 return base.UniqueString + Text + FontColor.ToString() + Font.ToString(); 
 } 
 } 
 } 
}
赞
If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)


QQ:154298438
QQ:417480759