asp.net thumbnail and watermark making: Image Generation

Source: Internet
Author: User
Tags eval tostring

asp.net image generation built-in Imageresizetransform class, you can achieve the image sizing function.


can also extend imagetransform to implement its own picture transformation class.


use asp.net image generation to generate picture thumbnails and watermarks using the following code:





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= "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 =, 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, connect  ION);





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.