Collection Asp. NET generates high-quality thumbnail common functions (vb.net, C # code)

Source: Internet
Author: User
Guide:
When developing a Web site, generating thumbnails is a very common and useful feature. Previously used only with COM components in the ASP, it is now easy to implement in. NET the powerful class library of the framework. Here is a complete code (with detailed comments), referring to some articles on the web and the. NET SDK related content, I sorted out C # and vb.net two kinds of code.
Let's look at the following code:
' Save the picture after it's shrunk
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 (640, CALLB, New system.intptr (0))
Else
NewImage = image. Getthumbnailimage (640, CALLB, New system.intptr (0))
End If
Image. Dispose ()
' Make new pictures and pictures smaller and save them in a temporary path
NewImage. Save (sFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
NewImage. Dispose ()
End Sub
Look at the following to generate high-quality thumbnail code (VB and C #)
Vb. NE code:
"' Generate thumbnails
''
' Source diagram path (physical path)
' Thumbnail path (physical path)
' Thumbnail width
' Thumbnail height
"How to generate thumbnails
''
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 width scaling (possible variants)
Case "W" ' specifies a wide, high proportional
Toheight = Originalimage.height * width/originalimage.width
Case "H" ' specifies high, wide proportional
Towidth = Originalimage.width * height/originalimage.height
Case "Cut" ' specifies width reduction (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 new BMP picture
Dim bitmap = New drawing.bitmap (towidth, Toheight)
' Create a new artboard
Dim g as Drawing.graphics = Drawing.Graphics.FromImage (bitmap)
' Set high quality interpolation method '
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High
' Set high quality, low speed rendering smoothing degree
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
' Empty the canvas and fill with a transparent background color
G.clear (Drawing.Color.Transparent)
' Draws the specified portion of the original picture at the specified location and at the specified size
G.drawimage (Originalimage, New Drawing.rectangle (0, 0, Towidth, toheight), New Drawing.rectangle (x, Y, Ow, OH), system.dr Awing. GraphicsUnit.Pixel)
Try
' Save thumbnails 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 thumbnails
///
Source diagram path (physical path)
Thumbnail path (physical path)
Thumbnail width
Thumbnail height
How thumbnails are generated
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 width scaling (possible variants)
Break
Case "W"://Specify wide, high proportionally
Toheight = Originalimage.height * width/originalimage.width;
Break
Case "H"://Specify high, wide proportional
Towidth = Originalimage.width * height/originalimage.height;
Break
Case "cut"://Specify width trim (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 picture
Image bitmap = new Bitmap (towidth, toheight);
Create a new artboard
Graphics g = graphics.fromimage (bitmap);
Set high quality interpolation method
G.interpolationmode = Interpolationmode.high;
Set high quality, low speed rendering smooth degree
G.smoothingmode = smoothingmode.highquality;
Empty the canvas and fill with a transparent background color
G.clear (color.transparent);
Draws the specified portion of the original picture at the specified location 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, imageformat.jpeg);
}
catch (Exception e)
{
Throw e;
}
Finally
{
Originalimage.dispose ();
Bitmap. Dispose ();
G.dispose ();
}
}
Very clearly, the first code generated thumbnail quality is very poor, and the following code generated thumbnail is clearer, we try it.

This article turns from
Http://www.zhouguoqing.com.cn/zBlog/post/30.html
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.