Asp.net uploads and processes images (generate thumbnails, add text watermarks to images, and generate image watermarks ).
Method class:
Using System;
Using System. Data;
Using System. Configuration;
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;
/// <Summary>
/// Summary of upFileClass
/// </Summary>
Public class upFileClass
{
Public upFileClass ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "originalImagePath"> source image 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 a thumbnail </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 high-width Scaling (possibly deformed)
Break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalImage. Height * width/originalImage. Width;
Break;
Case "H": // specify the height. The width is proportional.
Towidth = originalImage. Width * height/originalImage. Height;
Break;
Case "Cut": // specify the height and width (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 bmp Image
System. Drawing. Image bitmap = new System. Drawing. Bitmap (towidth, toheight );
// Create a canvas
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmap );
// Set a high quality Interpolation Method
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
// Set high quality and smooth Low Speed
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
// Clear the canvas and fill it with a transparent background color
G. Clear (System. Drawing. Color. Transparent );
// Draw the specified part of the original image at the specified position and 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
{
// Save the thumbnail 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 the image
/// </Summary>
/// <Param name = "Path"> original server image Path </param>
/// <Param name = "Path_sy"> generated image path with a text watermark </param>
Public static void AddShuiYinWord (string Path, string Path_sy)
{
String addText = "";
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 an image watermark on the Image
/// </Summary>
/// <Param name = "Path"> original server image Path </param>
/// <Param name = "Path_syp"> path of the image watermark generated </param>
/// <Param name = "Path_sypf"> watermark image path </param>
Public static 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 ();
}
}
Default. aspx code:
<! 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> asp.net: upload an image and process the thumbnail of the watermark _ www.jbxue.com </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: FileUpload ID = "FileUpload1" runat = "server" BorderColor = "Gray" BorderWidth = "1px"/>
<Asp: button ID = "Button1" runat = "server" onClick = "button#click" Text = "Upload" BorderColor = "Gray" BorderWidth = "1px" Width = "70px"/> <br />
<Asp: Label ID = "Label1" runat = "server"> </asp: Label>
</Div>
</Form>
</Body>
</Html>
Default. aspx. cs code:
Using System. Data;
Using System. Configuration;
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;
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#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; // The Name Of The thumbnail file.
String fileName_sy = "sy _" + file. Name; // watermark image file Name (text)
String fileName_syp = "syp _" + file. Name; // watermark image file Name (image)
// The following path can be modified as needed
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); // watermark path (text) on the Server side)
String webFilePath_syp = Server. MapPath ("file/" + fileName_syp); // watermark path (image) on the Server side)
// Prepare a watermark image first for the watermark image path below!
String webFilePath_sypf = Server. MapPath ("file/shuiyin.jpg"); // Server-side watermark image path (image)
If (! File. Exists (webFilePath ))
{
Try
{
FileUpload1.SaveAs (webFilePath); // use the SaveAs method to save the file
UpFileClass. AddShuiYinWord (webFilePath, webFilePath_sy );
UpFileClass. AddShuiYinPic (webFilePath, webFilePath_syp, webFilePath_sypf );
UpFileClass. MakeThumbnail (webFilePath, webFilePath_s, 130,130, "Cut"); // method for generating thumbnails
Label1.Text = "prompt: the file" "+ fileName +" "is uploaded successfully, and a" + fileName_s + "thumbnail is generated. The file type is" + FileUpload1.PostedFile. contentType + ", file size:" + FileUpload1.PostedFile. contentLength + "B ";
}
Catch (Exception ex)
{
Label1.Text = "prompt: File Upload Failed. cause of failure:" + ex. Message;
}
}
Else
{
Label1.Text = "prompt: The file already exists. Please rename it and upload it ";
}
}
Else
{
Label1.Text = "prompt: the file type does not match ";
}
}
}
}