Asp.net generates high-quality thumbnail functions (vb code)

Source: Internet
Author: User
Tags bmp image

I searched for it on the Internet using C #. For more information, see. convert it to VB. NET.


' <Param name = "originalimagepath"> source image 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 a thumbnail </param>
Public   Shared   Sub makethumbnail () 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
Originalimage = 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 "   ' Specify high-width Scaling (may be deformed)
Case   " W "   ' Specify width and height in proportion
Toheight = Originalimage. Height * Width / Originalimage. Width
Case   " H "   ' Specify height, width by proportion
Towidth = Originalimage. Width * Height / Originalimage. Height
Case   " Cut "   ' Specify height and width reduction (not deformed)
If   Cdbl (Originalimage. width) /   Cdbl (Originalimage. Height) >   Cdbl (Towidth) /   Cdbl (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 System. Drawing. Bitmap (towidth, toheight)

' Create a canvas
Dim G As Graphics = System. Drawing. Graphics. fromimage (Bitmap)

' Set a high quality Interpolation Method
G. interpolationmode = System. Drawing. drawing2d. interpolationmode. High

' Set high quality, low speed rendering Smoothness
G. smoothingmode = System. Drawing. drawing2d. 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 thumbnails in JPG format
Bitmap. Save (thumbnailpath)
Catch Ex As Exception
Throw Ex
Finally
Originalimage. Dispose ()
Bitmap. Dispose ()
G. Dispose ()
End   Try
End sub

It is too bad to directly generate a thumbnail after using the original upload ..

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.