Using the FileUPload control upload images and automatically generate thumbnails, automatically generate watermarks with text an

xiaoxiao2021-04-05  249

Http://www.cnblogs.com/weekzero/archive/2006/05/05/392003.html

This article implements the upload of the picture file by means of the fileupload control in VS2005 and generates a thumbnail. Implementation process: After selecting the image upload success, obtain the file that already exists in the existing server generates thumbnails, and determines whether it is a picture type file, this judgment can be modified in the program, this program is just judging "image / bmp", "image / gif "," image / pjpeg "three 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"

>

<

HTML

XMLNS

= "http://www.w3.org/1999/xhtml"

>

<

HEAD

Runat

= "Server"

>

<

Title

>

Untitled Page

Title

>

HEAD

>

<

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

>

HTML

>

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 diagram file name (text) String filename_syp = "SYP_" File.name; // Watermark diagram file name (picture) string webfilepath = server.mappath ("file /" filename); // server-side file path String WebFilep ATH_S = Server.mAppath ("File /" FileName_s); // Server-side Thumbnail Path String WebFilePath_SY = Server.mAppath ("File /" FileName_SY); // Server Radio Watermark Path (Text) String WebFilePath_syp = Server.mappath ("file /" filename_syp); // server side with watermarking path (picture) string webfilepath_sypf = server.mappath ("file / shuiyin.jpg"); // server side watermark image (picture) IF (! File.exists (WebFilePath) {Try {fileupload1.saveas (webfilepath);

// use the SaveAs method to save the file AddShuiYinWord (webFilePath, webFilePath_sy); AddShuiYinPic (webFilePath, webFilePath_syp, webFilePath_sypf); MakeThumbnail (webFilePath, webFilePath_s, 130, 130, "Cut"); // generate thumbnails method Label1.Text = "prompt : File " FileName " "Successfully Uploaded, and generated" filename_s "thumbnail, file type:" fileupload1.postedfile.contentType ", file size is:" fileupload1.postedfile.contentLength "B";} catch (exception ex) {label1.text = "Tip: File upload failed, failure:" ex.Message;}} else {label1.text = "Tip: The file already exists, please rename it Upload ";}} else {label1.text =" Tip: File type does not match ";}}} / ** ////

/// generated thumbnail /// /// Source Map Path (Physical Path) /// thumbnail path (physical path) /// thumbnail width /// thumbnail height < / param> /// generated thumbnails 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 high width zoom (possible deformation) Break; case "w": // Specify wide, high proportion toHeight = OriginalImage.Height * width / OriginalImage.width; Break; Case "H": / / Specify high, broadly proportional Towidth = OriginalImage.width * Height / OriginalImage.Height; Break; Case "CUT": // Specify the high width cut (non-deformation) IF ((Double) OriginalImage.width / (Double) OriginalImage. Height> (Double) Towidth / (Double) {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;} // Newly built a BMP image System. Drawing.Image bitmap = new System.Drawing.Bitmap (towidth, toheight); // Create a artboards System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (bitmap); // set quality interpolation g.InterpolationMode =

System.drawing.drawing2d.interpolationmode.high; // Set high quality, low speed rendering smoothness g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality; // Empty canvas and populate G. Clear in transparent background (System) .Drawing.color.Transparent; // Draw the specified section of the original picture in 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 {// Saves thumbnails Bitmap.Save (thumbnailpath, system.drawing.Imaging.imageformat.jpeg) in JPG format;} catch System.exception e) {throw e;} finally {OriginalImage.dispose (); bitmap.dispose (); g.dispose ();}} / ** ////

/// Add text on the image Watermark /// /// Original server picture path /// generated picture path with text watermark 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.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 on the picture Water print /// /// Original server picture path <

转载请注明原文地址:https://www.9cbs.com/read-132174.html

New Post(0)