aspnetImageGeneration内置了ImageResizeTransform类可以实现图片大小调整功能
也可以扩展ImageTransform实现自己的图片变换类
下面使用ASPNETImageGeneration生成图片缩略图及水印的代码
数据库
CREATETABLEt_images
(
image_idINT
image_dataIMAGE
last_modified_dateDATETIME
)
Defaultaspx
Code
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Defaultaspxcs"Inherits="NETLabGeneratedImageWeb_Default"%>
<%@RegisterAssembly="MicrosoftWebGeneratedImage"Namespace="MicrosoftWeb"TagPRefix="cc"%>
<!DOCTYPEhtmlPUBLIC"//WC//DTDXHTMLTransitional//EN""
<htmlxmlns="
<headrunat="server">
<title></title>
</head>
<body>
<formid="form"runat="server">
<div>
<asp:DataListID="DataList"runat="server"DataSourceID="SqlDataSource"RepeatColumns=""
RepeatDirection="Horizontal"CellPadding=""CellSpacing="">
<ItemTemplate>
<cc:GeneratedImageID="GeneratedImage"runat="server"ImageHandlerUrl="~/ImageHandlerashx"
Timestamp=’<%#Eval("last_modified_date")%>’>
<Parameters>
<cc:ImageParameterName="imageid"Value=’<%#Eval("image_id")%>’/>
</Parameters>
</cc:GeneratedImage>
<br/>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSourceID="SqlDataSource"runat="server"ConnectionString="DataSource=SQLEXPRESS;AttachDbFilename=|DataDirectory|Databasemdf;IntegratedSecurity=True;UserInstance=True;"
ProviderName="SystemDataSqlClient"SelectCommand="SELECT[image_id][image_data][last_modified_date]FROM[t_images]">
</asp:SqlDataSource>
</form>
</body>
</html>
ImageHandlerashx
<%@WebHandlerLanguage="C#"CodeBehind="ImageHandlerashxcs"Class="NETLabGeneratedImageWebImageHandler"%>
ImageHandlerashxcs
Code
usingSystem;
usingSystemCollectionsGeneric;
usingSystemLinq;
usingSystemWeb;
usingMicrosoftWeb;
usingSystemCollectionsSpecialized;
usingSystemDataSqlClient;
usingSystemDrawing;
namespaceNETLabGeneratedImageWeb
{
publicclassImageHandler:ImageHandler
{
publicImageHandler()
{
thisImageTransformsAdd(newImageResizeTransform{Width=Mode=ImageResizeModeFit});
thisImageTransformsAdd(newCopyrightTransform{Text="GuushuuseNET"});
thisEnableClientCache=true;
thisEnableServerCache=true;
}
publicoverrideImageInfoGenerateImage(NameValueCollectionparameters)
{
intimageID=intParse(parameters["imageid"]);
SqlConnectionconnection=newSqlConnection(
@"DataSource=SQLEXPRESS;AttachDbFilename=|DataDirectory|Databasemdf;IntegratedSecurity=True;UserInstance=True;");
connectionOpen();
SqlCommandcommand=newSqlCommand("selectimage_datafromt_imageswhereimage_id="+imageIDconnection);
SqlDataReaderdr=commandExecuteReader();
drRead();
returnnewImageInfo((Byte[])dr[]);
}
}
publicclassCopyrightTransform:ImageTransform
{
privateconstintVERTICAL_PADDING=;
privateconstintHORIZONAL_PADDING=;
publicstringText{get;set;}
publicFontFont{get;set;}
publicColorFontColor{get;set;}
publicCopyrightTransform()
{
//AssignDefaults
FontColor=ColorFromArgb();
Font=newFont("Courier");
}
publicoverrideSystemDrawingImageProcessImage(SystemDrawingImageimg)
{
Graphicsgra=GraphicsFromImage(img);
SizeFsz=graMeasureString(TextFont);
graDrawString(TextFontnewSolidBrush(FontColor)imgWidthszWidthHORIZONAL_PADDINGimgHeightszHeightVERTICAL_PADDING);
returnimg;
}
publicoverridestringUniqueString
{
get
{
returnbaseUniqueString+Text+FontColorToString()+FontToString();
}
}
}
}