VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,322 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,017 people online right now. Registration is fast and FREE... Join Now!





Resize Image

Resizes Image to desired size

Submitted By: spimoles
Actions:
Rating:
Views: 26,310

Language: VB.NET

Last Modified: October 22, 2006
Instructions: Use as Function. Resizes image in a Picture Box

Snippet


  1.         'following code resizes picture to fit
  2.  
  3.         Dim bm As New Bitmap(PictureBox1.Image)
  4.         Dim x As Int32 'variable for new width size
  5.         Dim y As Int32 'variable for new height size
  6.  
  7.         Dim width As Integer = Val(x) 'image width.
  8.  
  9.         Dim height As Integer = Val(y) 'image height
  10.  
  11.         Dim thumb As New Bitmap(width, height)
  12.  
  13.         Dim g As Graphics = Graphics.FromImage(thumb)
  14.  
  15.         g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  16.  
  17.         g.DrawImage(bm, New Rectangle(0, 0, width, height), New Rectangle(0, 0, bm.Width, _
  18. bm.Height), GraphicsUnit.Pixel)
  19.  
  20.         g.Dispose()
  21.  
  22.  
  23.       'image path. better to make this dynamic. I am hardcoding a path just for example sake
  24.         thumb.Save("C:\newimage.bmp", _
  25. System.Drawing.Imaging.ImageFormat.Bmp) 'can use any image format
  26.  
  27.         bm.Dispose()
  28.  
  29.         thumb.Dispose()
  30.  
  31.         Me.Close()  'exit app
  32.  

Copy & Paste


Comments


realwish 2008-03-29 03:46:23

not easy to understand for a beginner

csee85 2008-05-04 05:54:05

NOOB

jagatworld 2008-05-16 23:17:16

really good......

jacobjordan 2008-12-31 13:48:16

This is good to demonstrate the concept of how to use Graphics to resize an image, and i have referenced it many times when providing help to people in VB.NET who need to do so. However, it is very unclear and poorly written, and needs much more explaining and commenting on your part. My 2 cents.

CarmenDev 2009-06-15 06:17:23

The code sample is good and works corretly, but when an image has a transparent background, whe it saves the image it saves with a black background (you can see if you open it with paint). Can anyone help me to fix it? Thanks in advnace!

CarmenDev 2009-06-15 06:18:42

The code sample is good and works corretly, but when an image has a transparent background, when it saves the image, it saves with a black background (you can see if you open it with paint app). Can anyone help me to fix it? Thanks in advnace! (sorry about the grammar)

spimoles 2009-10-02 03:37:36

Wow, haven't been on this site in a little under 3years. I'm definitely going to give this snippet a look over and change some things. Looking forward to becoming active on this site.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month