Asp.net generates high-quality thumbnails for general functions (c # code

Source: Internet
Author: User

String strOutPath = "";
ArrFilePath = myCommonMethod. UploadPic (Files, "/UpLoads/UpPicture /");
This. GetThumbnailImage (120,150,100, System. IO. Path. GetFileName (arrFilePath [0]. ToString (), out strOutPath );

// Create a thumbnail Function
/// <Summary>
///
/// </Summary>
/// <Param name = "width"> thumbnail width </param>
/// <Param name = "height"> thumbnail height </param>
/// <Param name = "level"> scale down </param>
/// <Param name = "file_id"> file name </param>
Public void GetThumbnailImage (int width, int height, int level, string file_id, out string strOutParam)
{
String newfile = Request. PhysicalApplicationPath + "/UpLoads/UpPicture/" + "top _" + file_id;

System. Drawing. Image oldimage = System. Drawing. Image. FromFile (Request. PhysicalApplicationPath + "/UpLoads/UpPicture/" + file_id );

System. Drawing. Image thumbnailImage = oldimage. GetThumbnailImage (width, height, new System. Drawing. Image. GetThumbnailImageAbort (ThumbnailCallback), IntPtr. Zero );

Bitmap output = new Bitmap (thumbnailImage );

// Processing JPG Quality Functions
ImageCodecInfo [] codecs = ImageCodecInfo. GetImageEncoders ();
ImageCodecInfo ici = null;
Foreach (ImageCodecInfo codec in codecs)
{
If (codec. MimeType = "image/jpeg ")
{
Ici = codec;
}
}
EncoderParameters ep = new EncoderParameters ();
Ep. Param [0] = new EncoderParameter (Encoder. Quality, (long) level );
Output. Save (newfile, ici, ep );
// Output parameters
StrOutParam = newfile. Substring (newfile. LastIndexOf ("/") + 1 );
// Release all objects used
Ep. Dispose ();
Output. Dispose ();
Oldimage. Dispose ();
ThumbnailImage. Dispose ();

/// Delete the source Image
// String file_path = "/UpLoads/UpPicture/" + "top _" + file_id;
// UsageIO. DeletePath (Request. PhysicalApplicationPath + "images/article_images/" + file_id );
//// Response. write ("<script> parent. form1.A _ Simg. value = '"+ file_path +"'; location. replace ('upload _ Img. aspx ') </script> ");
}

Bool ThumbnailCallback ()
{
Return false;
}

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.