asp.net instance source code for generating thumbnails

Source: Internet
Author: User
Tags bool

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;

///

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

///

public class Imageclass

{

Public System.Drawing.Image Resourceimage;

private int imagewidth;

private int imageheight;

public string errmessage;

///

Constructors for classes

///

Full path name of picture file

Public Imageclass (String imagefilename)

{

Resourceimage = System.Drawing.Image.FromFile (imagefilename);

Errmessage = "";

}

public bool Thumbnailcallback ()

{

return false;

}

///

Generate thumbnail overload Method 1, returning the image object of the thumbnail

///

width of thumbnails

The height of the thumbnail

Image object for thumbnail image

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;

}

}

///

Generate thumbnail overload Method 2, save the thumbnail file to the specified path

///

width of thumbnails

The height of the thumbnail

Thumbnail saved full-text name, (with path), parameter format: D:images ilename.jpg

Returns true successfully, otherwise returns false

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;

}

}

///

Generate thumbnail overload Method 3, returning the image object of the thumbnail

///

Percentage of the width of the thumbnail: 80 is required, 0.8.

Image object for thumbnail image

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;

}

}

///

Generate thumbnail overload method 4, returning the image object of the thumbnail

///

Percentage of the width of the thumbnail: 80 is required, 0.8.

Thumbnail saved full-text name, (with path), parameter format: D:images ilename.jpg

Returns true successfully, otherwise returns false

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.