ASP. NET generate high-quality thumbnails common functions (VB. NET, C # code)

Source: Internet
Author: User
Tags bmp image

Generating thumbnails during website development is a very common and practical function. previously, ASP can only be implemented using COM components. net can be easily implemented using the powerful class libraries of the framework. I posted the complete code (with detailed comments) in the following post. I have read some articles on the Internet and the content related to the Asp.net SDK. I have sorted out C # and VB. net.

Let's take a look at the following code:

''Shrink the image and save it.

Private sub savesmallpic (byval sfilename as string)

Dim image as system. Drawing. Image, newimage as system. Drawing. Image

Dim callb as system. Drawing. image. getthumbnailimageabort

Image = system. Drawing. image. fromstream (Me. file1.postedfile. inputstream)

If image. width <image. Height then

Newimage = image. getthumbnailimage (480,640, callb, new system. intptr (0 ))

Else

Newimage = image. getthumbnailimage (640,480, callb, new system. intptr (0 ))

End if

Image. Dispose ()

''To reduce the size of the new image and save it to the temporary path.

Newimage. Save (sfilename, system. Drawing. imaging. imageformat. JPEG)

Newimage. Dispose ()

End sub

Let's look at the following code to generate high-quality thumbnails (VB and C #)

VB. Ne code:

''' To generate a thumbnail

''''

'''' Source image path (physical path)

''' Thumbnail path (physical path)

''' Thumbnail width

''' Thumbnail height

''' How to generate a thumbnail

''''

Public shared sub makethumbnail (byval originalimagepath as string, byval thumbnailpath as string, byval width as integer, byval height as integer, byval mode as string)

Dim originalimage as system. Drawing. Image = system. Drawing. image. fromfile (originalimagepath)

Dim towidth as integer = width

Dim toheight as integer = height

Dim X as integer = 0

Dim y as integer = 0

Dim ow as integer = originalimage. Width

Dim Oh as integer = originalimage. Height

Select case Mode

Case "HW" ''specifies high-width Scaling (may be deformed)

Case "W" ''specifies the width, and the height is proportional.

Toheight = originalimage. Height * width/originalimage. Width

Case "H" ''specifies the height, width is proportional

Towidth = originalimage. Width * Height/originalimage. Height

Case "cut" ''specifies high width cut (not deformed)

If system. convert. todouble (originalimage. width)/system. convert. todouble (originalimage. height)> system. convert. todouble (towidth)/system. convert. todouble (toheight) then

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

End if

Case else

End select

''Create a BMP Image

Dim bitmap = new drawing. Bitmap (towidth, toheight)

''Create a canvas

Dim G as drawing. Graphics = drawing. Graphics. fromimage (Bitmap)

''Set a high quality Interpolation Method

G. interpolationmode = system. Drawing. drawing2d. interpolationmode. High

''Sets high quality and shows smoothness at low speeds.

G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality

''Clear the canvas and fill it with a transparent background color

G. Clear (drawing. color. Transparent)

''At the specified position and draw the specified part of the original image according to the specified size

G. drawimage (originalimage, new drawing. rectangle (0, 0, towidth, toheight), new drawing. rectangle (X, Y, ow, oh), system. drawing. graphicsunit. pixel)

Try

''Save the thumbnail in JPG format

Bitmap. Save (thumbnailpath, system. Drawing. imaging. imageformat. JPEG)

Catch e as exception

Throw E

Finally

Originalimage. Dispose ()

Bitmap. Dispose ()

G. Dispose ()

End try

End sub

C # code:

////

/// Generate a thumbnail

///

/// Source image path (physical path)

/// Thumbnail path (physical path)

/// Thumbnail width

/// Thumbnail height

/// How to generate a thumbnail

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 high-width Scaling (possibly deformed)

Break;

Case "W": // specify the width, and the height is proportional.

Toheight = originalimage. Height * width/originalimage. width;

Break;

Case "H": // specify the height. The width is proportional.

Towidth = originalimage. Width * Height/originalimage. height;

Break;

Case "cut": // specify the height and width (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 BMP Image

Image bitmap = new Bitmap (towidth, toheight );

// Create a canvas

Graphics G = graphics. fromimage (Bitmap );

// Set a high quality Interpolation Method

G. interpolationmode = interpolationmode. High;

// Set high quality and smooth Low Speed

G. smoothingmode = smoothingmode. highquality;

// Clear the canvas and fill it with a transparent background color

G. Clear (color. Transparent );

// Draw the specified part of the original image at the specified position and in the specified size

G. drawimage (originalimage, new rectangle (0, 0, towidth, toheight ),

New rectangle (X, Y, ow, oh ),

Graphicsunit. pixel );

Try

{

// Save the thumbnail in JPG format

Bitmap. Save (thumbnailpath, imageformat. JPEG );

}

Catch (exception E)

{

Throw E;

}

Finally

{

Originalimage. Dispose ();

Bitmap. Dispose ();

G. Dispose ();

}

}

It is clearly displayed that the quality of the thumbnails generated by the first code is very poor, and the thumbnails generated by the following code are clear. Please try.

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.