萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp.net編程 >> asp.net縮略圖和水印制作:Image Generation

asp.net縮略圖和水印制作:Image Generation

asp.net Image Generation內置了ImageResizeTransform類,可以實現圖片大小調整功能。
也可以擴展ImageTransform實現自己的圖片變換類。 
下面使用ASP.NET Image Generation生成圖片縮略圖及水印的代碼: 

數據庫 
CREATE TABLE t_images 

image_id INT, 
image_data IMAGE, 
last_modified_date, DATETIME 

  
Default.aspx 

Code 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NET35Lab.GeneratedImage.Web._Default" %> 

<%@ Register Assembly="Microsoft.Web.GeneratedImage" Namespace="Microsoft.Web" TagPRefix="cc1" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="2" 
            RepeatDirection="Horizontal" CellPadding="4" CellSpacing="4"> 
            <ItemTemplate> 
                <cc1:GeneratedImage ID="GeneratedImage1" runat="server" ImageHandlerUrl="~/ImageHandler1.ashx" 
                    Timestamp=’<%# Eval("last_modified_date") %>’> 
                    <Parameters> 
                        <cc1:ImageParameter Name="imageid" Value=’<%# Eval("image_id") %>’ /> 
                    </Parameters> 
                </cc1:GeneratedImage> 
                <br /> 
            </ItemTemplate> 
        </asp:DataList> 
    </div> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True;" 
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT [image_id], [image_data], [last_modified_date] FROM [t_images]"> 
    </asp:SqlDataSource> 
    </form> 
</body> 
</html> 

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 
 &nb

copyright © 萬盛學電腦網 all rights reserved