熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

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

數據庫 
CREATE TABLE t_images 

image_id INT 
image_data IMAGE 
last_modified_date DATETIME 

  
Defaultaspx 

Code 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Defaultaspxcs" Inherits="NETLabGeneratedImageWeb_Default" %> 

<%@ Register Assembly="MicrosoftWebGeneratedImage" Namespace="MicrosoftWeb" TagPRefix="cc" %> 
<!DOCTYPE html PUBLIC "//WC//DTD XHTML  Transitional//EN" "
<html xmlns="
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form" runat="server"> 
    <div> 
        <asp:DataList ID="DataList" runat="server" DataSourceID="SqlDataSource" RepeatColumns="
            RepeatDirection="Horizontal" CellPadding="" CellSpacing=""> 
            <ItemTemplate> 
                <cc:GeneratedImage ID="GeneratedImage" runat="server" ImageHandlerUrl="~/ImageHandlerashx" 
                    Timestamp=’<%# Eval("last_modified_date") %>’> 
                    <Parameters> 
                        <cc:ImageParameter Name="imageid" Value=’<%# Eval("image_id") %>’ /> 
                    </Parameters> 
                </cc:GeneratedImage> 
                <br /> 
            </ItemTemplate> 
        </asp:DataList> 
    </div> 
    <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="Data Source=SQLEXPRESS;AttachDbFilename=|DataDirectory|Databasemdf;Integrated Security=True;User Instance=True;" 
        ProviderName="SystemDataSqlClient" SelectCommand="SELECT [image_id] [image_data] [last_modified_date] FROM [t_images]"> 
    </asp:SqlDataSource> 
    </form> 
</body> 
</html> 

ImageHandlerashx 

<%@ WebHandler Language="C#" CodeBehind="ImageHandlerashxcs" Class="NETLabGeneratedImageWebImageHandler" %> 

ImageHandlerashxcs 

Code 
using System; 
using SystemCollectionsGeneric; 
using SystemLinq; 
using SystemWeb; 
using MicrosoftWeb; 
using SystemCollectionsSpecialized; 
using SystemDataSqlClient; 
using SystemDrawing; 

namespace NETLabGeneratedImageWeb 


    public class ImageHandler : ImageHandler 
    { 

        public ImageHandler() 
        { 
            thisImageTransformsAdd(new ImageResizeTransform { Width =  Mode = ImageResizeModeFit }); 
            thisImageTransformsAdd(new CopyrightTransform { Text = "Guushuuse NET" }); 
            thisEnableClientCache = true; 
            thisEnableServerCache = true; 

        } 

        public override ImageInfo GenerateImage(NameValueCollection parameters) 
        { 

            int imageID = intParse(parameters["imageid"]); 

            SqlConnection connection = new SqlConnection( 
                @"Data Source=SQLEXPRESS;AttachDbFilename=|DataDirectory|Databasemdf;Integrated Security=True;User Instance=True;"); 

            connectionOpen(); 

            SqlCommand command = new SqlCommand("select image_data from t_images where image_id=" + imageID connection); 

            SqlDataReader dr = commandExecuteReader(); 

            drRead(); 

            return new ImageInfo((Byte[])dr[]); 

        } 
    } 

    public class CopyrightTransform : ImageTransform 
    { 
        private const int VERTICAL_PADDING = 
        private const int HORIZONAL_PADDING = 

        public string Text { get; set; } 
        public Font Font { get; set; } 
        public Color FontColor { get; set; } 

        public CopyrightTransform() 
        { 
            //Assign Defaults 
            FontColor = ColorFromArgb(   ); 
            Font = new Font("Courier" ); 
        } 


        public override SystemDrawingImage ProcessImage(SystemDrawingImage img) 
        { 
            Graphics gra = GraphicsFromImage(img); 
            SizeF sz = graMeasureString(Text Font); 
            graDrawString(Text Font new SolidBrush(FontColor) imgWidth  szWidth  HORIZONAL_PADDING imgHeight  szHeight  VERTICAL_PADDING); 
            return img; 
        } 

        public override string UniqueString 
        { 
            get 
            { 
                return baseUniqueString + Text + FontColorToString() + FontToString(); 
            } 
        } 
    } 
}


From:http://tw.wingwit.com/Article/program/net/201311/14282.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.