asp.net generate thumbnail code _ Practical Tips

Source: Internet
Author: User
Copy Code code as follows:

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;
Using System.IO;
Using System.Drawing;
Using System.Drawing.Imaging;

<summary>
Picture Processing class
1. Generate thumbnail images or change the size and quality of pictures proportionally
2. Place the generated thumbnail in the specified directory
</summary>
public class Imageclass
{
Public System.Drawing.Image Resourceimage;
private int imagewidth;
private int imageheight;

public string errmessage;

<summary>
Constructors for classes
</summary>
<param name= "Imagefilename" > Picture file full path name </param>
Public Imageclass (String imagefilename)
{
Resourceimage = System.Drawing.Image.FromFile (imagefilename);
Errmessage = "";
}

public bool Thumbnailcallback ()
{
return false;
}

<summary>
Generate thumbnail overload Method 1, returning the image object of the thumbnail
</summary>
<param name= "width" > thumbnail widths </param>
<param name= "height" > Thumbnail Heights </param>
<returns> thumbnail Image Object </returns>
Public System.Drawing.Image getreducedimage (int Width, int Height)
{
Try
{
System.Drawing.Image Reducedimage;

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort ( Thumbnailcallback);

Reducedimage = Resourceimage.getthumbnailimage (Width, Height, CALLB, IntPtr.Zero);

return reducedimage;
}
catch (Exception e)
{
Errmessage = E.message;
return null;
}
}

<summary>
Generate thumbnail overload Method 2, save the thumbnail file to the specified path
</summary>
<param name= "width" > thumbnail widths </param>
<param name= "height" > Thumbnail Heights </param>
<param name= "Targetfilepath" > Thumbnail saved full-text name, (with path), parameter format: D:images ilename.jpg</param>
<returns> return true successfully, otherwise return false</returns>
public bool Getreducedimage (int Width, int Height, string targetfilepath)
{
Try
{
System.Drawing.Image Reducedimage;

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort ( Thumbnailcallback);

Reducedimage = Resourceimage.getthumbnailimage (Width, Height, CALLB, IntPtr.Zero);
Reducedimage.save (@targetFilePath, imageformat.jpeg);

Reducedimage.dispose ();

return true;
}
catch (Exception e)
{
Errmessage = E.message;
return false;
}
}

<summary>
Generate thumbnail overload Method 3, returning the image object of the thumbnail
</summary>
<param name= "Percent" > thumbnail width of the percentage such as: Required 80, to fill 0.8</param>
<returns> thumbnail Image Object </returns>
Public System.Drawing.Image getreducedimage (double Percent)
{
Try
{
System.Drawing.Image Reducedimage;

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort ( Thumbnailcallback);

ImageWidth = Convert.ToInt32 (Resourceimage.width * Percent);
ImageHeight = Convert.ToInt32 (Resourceimage.width * Percent);

Reducedimage = Resourceimage.getthumbnailimage (ImageWidth, ImageHeight, CALLB, IntPtr.Zero);

return reducedimage;
}
catch (Exception e)
{
Errmessage = E.message;
return null;
}
}

<summary>
Generate thumbnail overload method 4, returning the image object of the thumbnail
</summary>
<param name= "Percent" > thumbnail width of the percentage such as: Required 80, to fill 0.8</param>
<param name= "Targetfilepath" > Thumbnail saved full-text name, (with path), parameter format: D:images ilename.jpg</param>
<returns> return true successfully, otherwise return false</returns>
public bool Getreducedimage (double Percent, String targetfilepath)
{
Try
{
System.Drawing.Image Reducedimage;

System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort ( Thumbnailcallback);

ImageWidth = Convert.ToInt32 (Resourceimage.width * Percent);
ImageHeight = Convert.ToInt32 (Resourceimage.width * Percent);

Reducedimage = Resourceimage.getthumbnailimage (ImageWidth, ImageHeight, CALLB, IntPtr.Zero);

Reducedimage.save (@targetFilePath, imageformat.jpeg);

Reducedimage.dispose ();

return true;
}
catch (Exception e)
{
Errmessage = E.message;
return false;
}
}
}
Related Article

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.