theDeSilva.com Andrew de Silva

Resize image using VB.NET

January 21st, 2010 · No Comments · .NET

Here’s a quick function to resize image using VB.NET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Sub ResizeImage(ByVal dir As String, ByVal fileName As String, ByVal percentResize As Double)
        'following code resizes picture to fit
 
        Dim bm As New Bitmap("C:\" & dir & "\" & fileName)
 
        Dim width As Integer = bm.Width - (bm.Width * percentResize) 'image width. 
 
        Dim height As Integer = bm.Height - (bm.Height * percentResize)  'image height
 
        Dim thumb As New Bitmap(width, height)
 
        Dim g As Graphics = Graphics.FromImage(thumb)
 
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
 
        g.DrawImage(bm, New Rectangle(0, 0, width, height), New Rectangle(0, 0, bm.Width, _
bm.Height), GraphicsUnit.Pixel)
 
        g.Dispose()
 
        bm.Dispose()
 
        'image path.
        thumb.Save("C:\" & dir & "\" & fileName, _
System.Drawing.Imaging.ImageFormat.Jpeg) 'can use any image format 
 
        thumb.Dispose()
 
    End Sub

To use the code all you have to do is to pass in the directory of the image , the file name and the percentage of increase / decrease that you would like.

Share and Enjoy:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
  • Technorati
  • Facebook
  • Google Bookmarks
  • LinkedIn

Tags: ····