Generate a thumbnail [extracted from the CSDN Forum] for myself and friends in the garden

Source: Internet
Author: User
// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "fileName"> original file </param>
/// <Param name = "width"> width </param>
/// <Param name = "height"> height </param>
Private void CreateThumbnailImage (string fileName, string smallPath, int width, int height)
{
// Copyright information
String strAdd = "www.wandre.com ";
// The Name Of The generated thumbnail = year, month, and day + file size (to prevent identical file names)
String newFileName = smallPath;

System. Drawing. Image image, newImage;
// Load the original image
Image = System. Drawing. Image. FromFile (Server. MapPath (fileName ));
// Callback
System. Drawing. Image. GetThumbnailImageAbort callb = new System. Drawing. Image. GetThumbnailImageAbort (callBack );
// Generate a thumbnail
NewImage = image. GetThumbnailImage (width, height, callb, new System. IntPtr (); AddInfo (newImage, strAdd, newFileName, 16 );

Image. Dispose ();
NewImage. Dispose ();

Image1.ImageUrl = newFileName;
}

/// <Summary>
/// Add copyright information
/// </Summary>
/// <Param name = "image"> </param>
/// <Param name = "strAdd"> </param>
/// <Param name = "newFileName"> </param>
/// <Param name = "fontSize"> </param>
Private void AddInfo (System. Drawing. Image image, string strAdd, string newFileName, int fontSize)
{
Response. Clear ();
Bitmap B = new Bitmap (image );
Graphics g = Graphics. FromImage (B );
G. DrawString (strAdd, new Font ("", fontSize), new SolidBrush (Color. Red), image. Width/2-80, image. Height-30 );
B. Save (Server. MapPath (newFileName), System. Drawing. Imaging. ImageFormat. Gif );
}

Private bool callBack ()
{
Return true;
}

Protected void button#click (object sender, EventArgs e)
{
If (loadFile. PostedFile! = Null)
{
// Determine whether it is an image file
If (loadFile. PostedFile. ContentType. ToLower (). IndexOf ("image") <0)
{
// File type error
}
Else
{
String ext = loadFile. PostedFile. FileName. Substring (loadFile. PostedFile. FileName. LastIndexOf ("."));
// Generate a new original file name year month day + file size + Extension
String path = "Uploads \" + System. DateTime. Now. Date. to1_datestring () + loadFile. PostedFile. ContentLength. ToString () + ext;
// File with copyright information added
String newPath = "Uploads \" + System. DateTime. Now. Date. tow.datestring () + loadFile. PostedFile. ContentLength. ToString () + "new" + ext;
// Thumbnail file name
String newFileName = "uploads \" + System. DateTime. Now. Date. tow.datestring () + loadFile. PostedFile. ContentLength. ToString () + "small.gif ";
// Upload the source Image
LoadFile. PostedFile. SaveAs (Server. MapPath (path ));
// Generate a thumbnail
CreateThumbnailImage (path, newFileName, 200,300 );

// Add copyright information to the source Image
System. Drawing. Image image = System. Drawing. Image. FromFile (Server. MapPath (path ));
AddInfo (image, "www.wandre.com", newPath, 20 );
Image. Dispose ();
}
}

}

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.