asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

asp.net缩略图和水印制作:Image Generation


发布日期:2021年10月13日
 
asp.net缩略图和水印制作:Image Generation

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();
}
}
}
}

               

上一篇:ASP.NET动态生成静态页面的实例代码

下一篇:asp.net显示下载提示的下载网页程序