C # generate thumbnail code

Source: Internet
Author: User

/**////<summary>
Generate thumbnail images
</summary>
<param name= "Originalimagepath" > Source map 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 Thumbnails </param>
public static void Makethumbnail (String originalimagepath, string thumbnailpath, int width, int height, string mode)
{
Image originalimage = 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 aspect scaling (possibly deformed)
Break
Case "W"://Specify width, high proportionally
Toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://Specify high, wide proportionally
Towidth = Originalimage.width * height/originalimage.height;
Break
Case "Cut"://Designation Aspect cut (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 new BMP image
Image bitmap = new System.Drawing.Bitmap (towidth,toheight);

Create a new artboard
Graphics g = System.Drawing.Graphics.FromImage (bitmap);

Setting high-quality interpolation methods
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;

Set high quality, low speed rendering smoothness
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

Empty the canvas and fill it with a transparent background color
G.clear (color.transparent);

Draws the specified portion of the original picture at the specified position and at the specified size
G.drawimage (Originalimage, New Rectangle (0, 0, Towidth, toheight),
New Rectangle (x, Y, Ow,oh),
GraphicsUnit.Pixel);

Try
{
Save thumbnails in JPG format
Bitmap. Save (Thumbnailpath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
Throw e;
}
Finally
{
Originalimage.dispose ();
Bitmap. Dispose ();
G.dispose ();
}
}

The second Kind

4 overloaded methods, have directly returned image object, have generated thumbnails, and saved to the specified directory!

Using System.IO;
Using System.Drawing;
Using System.Drawing.Imaging;

<summary>
Picture Processing class
1. Create thumbnail images or proportionally change the size and quality of pictures
2. Place the generated thumbnail in the specified directory
</summary>
public class Imageclass
{
Public 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=image.fromfile (Imagefilename);
Errmessage= "";
}

public bool Thumbnailcallback ()
{
return false;
}

<summary>
Generate thumbnail overload Method 1, return the thumbnail image object
</summary>
<param name= "width" > thumbnail widths </param>
<param name= "height" > Thumbnail Heights </param>
<returns> image objects for thumbnails </returns>
Public Image getreducedimage (int width,int Height)
{
Try
{
Image Reducedimage;

Image.getthumbnailimageabort callb=new Image.getthumbnailimageabort (thumbnailcallback);

Reducedimage=resourceimage.getthumbnailimage (Width,height,callb,intptr.zero);

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

<summary>
Generate thumbnail Reload 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\filename.jpg</param>
<returns> successfully returns TRUE, otherwise returns false</returns>
public bool Getreducedimage (int width,int height,string targetfilepath)
{
Try
{
Image Reducedimage;

Image.getthumbnailimageabort callb=new 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, return the thumbnail image object
</summary>
<param name= "Percent" > thumbnail width percentage such as: 80 required, fill in the 0.8</param>
<returns> image objects for thumbnails </returns>
Public Image getreducedimage (double Percent)
{
Try
{
Image Reducedimage;

Image.getthumbnailimageabort callb=new 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, return the thumbnail image object
</summary>
<param name= "Percent" > thumbnail width percentage such as: 80 required, fill in the 0.8</param>
<param name= "Targetfilepath" > Thumbnail saved full-text name, (with path), parameter format:d:\images\filename.jpg</param>
<returns> successfully returns TRUE, otherwise returns false</returns>
public bool Getreducedimage (double percent,string targetfilepath)
{
Try
{
Image Reducedimage;

Image.getthumbnailimageabort callb=new 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;
}
}


}

C # generate thumbnail code

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.