Use the FileUpload control to upload pictures and automatically generate thumbnails, automatically generate a watermark with text and pictures
Use the FileUpload control to upload pictures and automatically generate thumbnails, automatically generate a watermark with text and pictures
Author: tecsun Date: 2007-03-18
Font size: Small Medium Large
With the help of the FileUpload control in vs2005, the image file is uploaded and the thumbnail is generated.
Implementation process: Choose the image upload success, get already existing server file generation thumbnail, and judge whether it is a picture type of file, this judgment can be modified in the program, this procedure just judged the "image/bmp", "Image/gif", "Image/pjpeg" three different types.
The code is as follows:
upfile.aspx file
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Upfile.aspx.cs" inherits= "Upfile_upfile"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:fileupload id= "FileUpload1" runat= "Server"/>
<asp:button id= "Button1" runat= "Server" onclick= "button1_click" text= "Upload"/><br/>
<asp:label id= "Label1" runat= "Server" ></asp:Label></div>
</form>
</body>
Upfile.aspx.cs file
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.IO;
public partial class Upfile_upfile:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{ }
protected void Button1_Click (object sender, EventArgs e)
{
if (fileupload1.hasfile)
{
string filecontenttype = FileUpload1.PostedFile.ContentType;
if (Filecontenttype = = "Image/bmp" | | | filecontenttype = "Image/gif" | | filecontenttype = "IMAGE/PJPEG")
{
String name = FileUpload1.PostedFile.FileName; Client file path
FileInfo file = new FileInfo (name);
String fileName = file. Name; File name
String filename_s = "S_" + file. Name; Thumbnail file name
String filename_sy = "Sy_" + file. Name; Watermark image file name (text)
String FILENAME_SYP = "Syp_" + file. Name; Watermark image file name (picture)
String webfilepath = Server.MapPath ("file/" + fileName); Server-side file path
String webfilepath_s = Server.MapPath ("file/" + filename_s); Server-side thumbnail path
String webfilepath_sy = Server.MapPath ("file/" + filename_sy); Server-side watermark path (text)
String WEBFILEPATH_SYP = Server.MapPath ("file/" + FILENAME_SYP); Server-side watermark path (picture)
String WEBFILEPATH_SYPF = Server.MapPath ("file/shuiyin.jpg"); Server-side watermark path (picture)
if (! File.exists (Webfilepath))
{
Try
{
Fileupload1.saveas (Webfilepath); Save a file using the SaveAs method
Addshuiyinword (Webfilepath, Webfilepath_sy);
Addshuiyinpic (Webfilepath, WEBFILEPATH_SYP, WEBFILEPATH_SYPF);
Makethumbnail (Webfilepath, webfilepath_s, 130, 130, "cut"); Generating thumbnail methods
Label1.Text = "Hint: File" "+ FileName +" "successfully uploaded and generated" "+ filename_s +" "thumbnail, File type:" + FileUpload1.PostedFile.ContentType + ", file large Small for: "+ FileUpload1.PostedFile.ContentLength +" B ";
}
catch (Exception ex)
{
Label1.Text = "prompt: File upload failed, failure reason:" + ex. message;
}
}
Else
{
Label1.Text = "Hint: file already exists, please rename and upload";
}
}
Else
{
Label1.Text = "Hint: file type does not match";
}
}
}
/**////<summary>
Generate thumbnails
</summary>
<param name= "Originalimagepath" > Source map Path (physical path) </param>
<param name= "Thumbnailpath" > Thumbnail path (physical path) </param>
<param name= "width" > Thumbnail width </param>
<param name= "height" > thumbnail height </param>
<param name= "mode" > How to Generate Thumbnails </param>
public static void Makethumbnail (String originalimagepath, string thumbnailpath, int width, int height, string mode)
{
System.Drawing.Image originalimage = System.Drawing.Image.FromFile (Originalimagepath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = Originalimage.width;
int oh = originalimage.height;
Switch (mode)
{
Case "HW"://Specify width scaling (possibly distorted)
Break
Case "W"://Specify wide, high proportionally
Toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://specified high, wide proportional
Towidth = Originalimage.width * height/originalimage.height;
Break
Case "cut"://Specify width trim (not deformed)
if (double) originalimage.width/double originalimage.height > (double) towidth/double toheight)
{
Oh = originalimage.height;
ow = Originalimage.height * towidth/toheight;
y = 0;
x = (Originalimage.width-ow)/2;
}
Else
{
ow = Originalimage.width;
Oh = originalimage.width * height/towidth;
x = 0;
y = (Originalimage.height-oh)/2;
}
Break
Default
Break
}
Create a new BMP picture
System.Drawing.Image bitmap = new System.Drawing.Bitmap (towidth, toheight);
Create a new artboard
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Set high quality interpolation method
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smooth degree
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Empty the canvas and fill with a transparent background color
G.clear (System.Drawing.Color.Transparent);
Draws the specified portion of the original picture at the specified location and at the specified size
G.drawimage (Originalimage, New System.Drawing.Rectangle (0, 0, Towidth, toheight),
New System.Drawing.Rectangle (x, Y, Ow, OH),
System.Drawing.GraphicsUnit.Pixel);
Try
{
Save thumbnails in JPG format
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
Throw e;
}
Finally
{
Originalimage.dispose ();
Bitmap. Dispose ();
G.dispose ();
}
}
/**////<summary>
Add a text watermark to a picture
</summary>
<param name= "Path" > Original server picture Path </param>
<param name= "Path_sy" > generated image path with text watermark </param>
protected void Addshuiyinword (String Path, String path_sy)
{
String addtext = "Test watermark";
System.Drawing.Image Image = System.Drawing.Image.FromFile (Path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (image);
G.drawimage (image, 0, 0, image. Width, image. Height);
System.Drawing.Font f = new System.Drawing.Font ("Verdana", 16);
System.Drawing.Brush B = new System.Drawing.SolidBrush (System.Drawing.Color.Blue);
g.DrawString (AddText, F, B, 15, 15);
G.dispose ();
Image. Save (Path_sy);
Image. Dispose ();
}
/**////<summary>
Generate a picture watermark on a picture
</summary>
<param name= "Path" > Original server picture Path </param>
<param name= "PATH_SYP" > Image path with picture watermark </param>
<param name= "PATH_SYPF" > Watermark image Path </param>
protected void Addshuiyinpic (String Path, String Path_syp, String path_sypf)
{
System.Drawing.Image Image = System.Drawing.Image.FromFile (Path);
System.Drawing.Image copyimage = System.Drawing.Image.FromFile (PATH_SYPF);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (image);
G.drawimage (Copyimage, new System.Drawing.Rectangle) (image. Width-copyimage.width, image. Height-copyimage.height, Copyimage.width, Copyimage.height), 0, 0, Copyimage.width,copyimage.height, System.Drawing.GraphicsUnit.Pixel);
G.dispose ();
Image. Save (PATH_SYP);
Image. Dispose ();
}
}