Upload a picture in asp.net and generate a thumbnail with copyright information

Source: Internet
Author: User
Tags date datetime file size tostring
asp.net| upload | upload picture | thumbnail

Front desk:
<HTML>
<HEAD>
<title>WebForm3</title>
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "Server" >
<input id= "loadfile" type= "file" runat= "Server" >
<asp:button id= "Button1" runat= "Server" text= "button" ></asp:Button><BR>
<asp:image id= "Image1" runat= "Server" ></asp:Image></form>
</body>
</HTML>

Background CS Code:
1/**////<summary>
2///Generate thumbnails
3///</summary>
4///<param name= "FileName" > Original file </param>
5///<param name= "width" > Breadth </param>
6///<param name= "height" > Altitude </param>
7 private void Createthumbnailimage (string filename,string smallpath,int width,int height)
8 {
9//Copyright information
A string stradd = "www.wander.com";
11//generated thumbnail name = Month Day + File size (prevent the same file name)
A string newfilename = Smallpath;
13
System.Drawing.Image Image,newimage;
15//Load the original image
Image = System.Drawing.Image.FromFile (Server.MapPath (fileName));
17//Callback
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort (Callbac k);
19//Generate thumbnails
NewImage = image. Getthumbnailimage (Width,height,callb,new system.intptr ());
21st
Addinfo (newimage,stradd,newfilename,16);
23
Image. Dispose ();
Newimage.dispose ();
26
Image1.imageurl = NewFileName;
28}
29
/**////<summary>
31///Add copyright information
///</summary>
///<param name= "image" ></param>
///<param name= "Stradd" ></param>
///<param name= "NewFileName" ></param>
///<param name= "FontSize" ></param>
Notoginseng private void Addinfo (System.Drawing.Image image,string stradd,string newfilename,int fontsize)
38 {
Response.Clear ();
Bitmap B = new Bitmap (image);
Graphics g = graphics.fromimage (b);
g.DrawString (stradd,new Font ("XXFarEastFont-Arial", FontSize), New SolidBrush (color.red), image. width/2-80, image. HEIGHT-30);
B.save (Server.MapPath (NewFileName), System.Drawing.Imaging.ImageFormat.Gif);
44}
45
The private bool CallBack ()
47 {
return true;
49}
50
Wuyi private void Button1_Click (object sender, System.EventArgs e)
52 {
if (loadfile.postedfile!= null)
54 {
55//Judgment is not image file
if (loadFile.PostedFile.ContentType.ToLower). IndexOf ("image") < 0)
57 {
58//File type error
59}
Or else
61 {
The string ext = loadFile.PostedFile.FileName.Substring (LoadFile.PostedFile.FileName.LastIndexOf (".") );
63//Generate new Original file name Date + file size + extension
$ string Path = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString () + LoadFile.PostedFile.Conte Ntlength.tostring () + ext;
65//Plus the copyright information file
$ string NewPath = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString () + LoadFile.PostedFile.Co Ntentlength.tostring () + "new" + ext;
67//Thumbnail file name
The string newfilename = "Uploads\\" + System.DateTime.Now.Date.ToShortDateString () + Loadfile.postedfil E.contentlength.tostring () + "small.gif";
69//Upload Artwork
LoadFile.PostedFile.SaveAs (Server.MapPath (path));
71//Generate thumbnails
Createthumbnailimage (path,newfilename,200,300);
73
74//to the original artwork plus copyright information
System.Drawing.Image Image = System.Drawing.Image.FromFile (Server.MapPath (path));
Addinfo (Image, "www.wander.com", newpath,20);
The image. Dispose ();
78}
79}
80}
In fact, to change the copyright information to the watermark is also very simple, only need to modify the Addinfo method can



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.