ASP.NET Image Generation内置了ImageResizeTransform类,可以实现图片大小调整功能。
也可以扩展ImageTransform实现自己的图片变换类。
下面使用ASP.NET Image Generation水印的代码:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingMicrosoft.Web;
- usingSystem.Collections.Specialized;
- usingSystem.Data.SqlClient;
- usingSystem.Drawing;
- namespaceNET35Lab.GeneratedImage.Web
- {
- publicclassImageHandler1:ImageHandler
- {
- publicImageHandler1()
- {
- this.ImageTransforms.Add(newImageResizeTransform
{Width=320,Mode=ImageResizeMode.Fit});- this.ImageTransforms.Add(newCopyrightTransform{Text="Guushuuse.NET"});
- this.EnableClientCache=true;
- this.EnableServerCache=true;
- }
- publicoverrideImageInfoGenerateImage(NameValueCollectionparameters)
- {
- intintimageID=int.Parse(parameters["imageid"]);
- SqlConnectionconnection=newSqlConnection(
- @"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.
mdf;IntegratedSecurity=True;UserInstance=True;");- connection.Open();
- SqlCommandcommand=newSqlCommand
("selectimage_datafromt_imageswhereimage_id="+imageID,connection);- SqlDataReaderdr=command.ExecuteReader();
- dr.Read();
- returnnewImageInfo((Byte[])dr[0]);
- }
- }
- publicclassCopyrightTransform:ImageTransform
- {
- privateconstintVERTICAL_PADDING=5;
- privateconstintHORIZONAL_PADDING=5;
- publicstringText{get;set;}
- publicFontFont{get;set;}
- publicColorFontColor{get;set;}
- publicCopyrightTransform()
- {
- //AssignDefaults
- FontColor=Color.FromArgb(128,255,0,0);
- Font=newFont("Courier",13);
- }
- publicoverrideSystem.Drawing.ImageProcessImage(System.Drawing.Imageimg)
- {
- GraphicsGraphicsgra=Graphics.FromImage(img);
- SizeFsz=gra.MeasureString(Text,Font);
- gra.DrawString(Text,Font,newSolidBrush(FontColor),
img.Width-sz.Width-HORIZONAL_PADDING,img.Height-sz.Height-VERTICAL_PADDING);- returnimg;
- }
- publicoverridestringUniqueString
- {
- get
- {
- returnbase.UniqueString+Text+FontColor.ToString()+Font.ToString();
- }
- }
- }
- }
以上是介绍ASP.NET Image Generation
【编辑推荐】