A method of generating thumbnail and watermark of asp.net image

Source: Internet
Author: User
Tags eval net return string tostring
asp.net image generation built-in Imageresizetransform class, you can achieve the image sizing function. You can also extend imagetransform to implement your own picture transformation class.

The following uses ASP.net image generation to generate a picture thumbnail and watermark code.

Database

The following are the referenced contents:

CREATE TABLE T_images
(
image_id INT,
Image_data IMAGE,
Last_modified_date, DATETIME
)

Default.aspx

The following are the referenced contents:

<%@ 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 ">
<title></title>
<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=datadirectorydatabase.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>

Imagehandler1.ashx

The following are the referenced contents:

<%@ WebHandler Language="C#" CodeBehind="ImageHandler1.ashx.cs" Class="NET35Lab.GeneratedImage.Web.ImageHandler1" %>


ImageHandler1.ashx.cs

The following are the referenced contents:

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 =, 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=datadirectorydatabase.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 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 ();
}
}
}
}

  



Related Article

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.