Asp.net thumbnails and watermark creation: Image Generation

Source: Internet
Author: User

Asp.net Image Generation has the built-in ImageResizeTransform class, which allows you to adjust the Image size.
You can also extend ImageTransform to implement your own image conversion class.
The following code uses ASP. NET Image Generation to generate Image thumbnails and Watermarks:

Database
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 = "PC3" %>
<! 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" performanceid = "sqlperformance1" RepeatColumns = "2"
RepeatDirection = "Horizontal" CellPadding = "4" CellSpacing = "4">
<ItemTemplate>
<PC3: GeneratedImage ID = "GeneratedImage1" runat = "server" ImageHandlerUrl = "~ /ImageHandler1.ashx"
Timestamp = '<% # Eval ("last_modified_date") %>'>
<Parameters>
<PC3: ImageParameter Name = "imageid" Value = '<% # Eval ("image_id") %>'/>
</Parameters>
</PC3: 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
{
Private const int VERTICAL_PADDING = 5;
Private const int HORIZONAL_PADDING = 5;

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

Public CopyrightTransform ()
{
// Assign Defaults
FontColor = Color. FromArgb (128,255, 0, 0 );
Font = new Font ("Courier", 13 );
}


Public override System. Drawing. Image ProcessImage (System. Drawing. Image img)
{
Graphics gra = Graphics. FromImage (img );
SizeF sz = gra. MeasureString (Text, Font );
Gra. DrawString (Text, Font, new SolidBrush (FontColor), img. Width-sz. Width-HORIZONAL_PADDING, img. Height-sz. Height-VERTICAL_PADDING );
Return img;
}

Public override string UniqueString
{
Get
{
Return base. UniqueString + Text + FontColor. ToString () + Font. ToString ();
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.