Results 1 to 7 of 7

Thread: Resizing existing bitmap

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Resizing existing bitmap

    I have created a bitmap using:

    Code:
    Dim myimage As New Bitmap(100,100)
    But I now want to resize the bitmap to, let's say 200x50 pixels. How would I do that?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Resizing existing bitmap

    You can't resize the existing Bitmap object. You have to create a new Bitmap of the desired size, call Graphics.FromImage to create a Graphics object and then call DrawImage on that to draw the old Image onto the new one. Don't forget to Dispose the Graphics and the old Bitmap.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Resizing existing bitmap

    Quote Originally Posted by jmcilhinney View Post
    You can't resize the existing Bitmap object. You have to create a new Bitmap of the desired size, call Graphics.FromImage to create a Graphics object and then call DrawImage on that to draw the old Image onto the new one. Don't forget to Dispose the Graphics and the old Bitmap.
    But what if I want to use the same variable, myimage?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Resizing existing bitmap

    Well?

  5. #5
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Resizing existing bitmap

    It would be stupid to use the same variable, but if you must.
    what jmc said:
    vb.net Code:
    1. Dim b As New Bitmap("C:\heart.PNG")
    2.         Dim temp As New Bitmap(b.Width * 2, b.Height * 2)
    3.         Using g As Graphics = Graphics.FromImage(temp)
    4.             g.DrawImage(b, New Rectangle(0, 0, temp.Width, temp.Height))
    5.         End Using
    6.         b = temp
    technically you aren't using the same variable, you are creating a new image, and setting the old variable's reference to the new image. The old image will be garbage collected.
    Last edited by BlindSniper; Dec 26th, 2011 at 01:08 PM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Resizing existing bitmap

    If you don't need the original sized image in your code, just the rescaled one, you can do it in a single line:
    Code:
    Dim b As New Bitmap(Image.FromFile("C:\heart.PNG"), w, h)
    where w and h are the desired width and height of the scaled bitmap.

    Still, the method recommended by JMcIlhinney and BlindSniper has an advantage. It allows you to specify the quality of the scaling, which can matter particularly if you are enlarging the image. For example, you could insert this after line 3 of BlindSniper's code:
    Code:
    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    The default mode blurs the original a bit when you enlarge it, but Bicubic spends about 2.5 times as long to give you a better quality blur. It usually looks best for photos.

    Alternatively, you might want to use InterpolationMode.NearestNeighbor, which converts each pixel to a rectangle of colour. That can be useful in drawing programs, and it's quicker than anything else.

    BB

    EDIT: Sorry, I made a mistake in the "one-line"version. See the current version.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Resizing existing bitmap

    Quote Originally Posted by Davve View Post
    Well?
    Before you start making posts like that, remember that you are not paying for our help, so we are not at your beckon call. This post was completely pointless because anyone who would answer your question would answer the previous post. That previous post was at 2.48 AM my time and this post was at 4.12 AM. Do you think OI should be sitting by my computer in the middle of the night just in case you have a question? We who answer questions are scattered all over the world in all different time zones. As well as that, we are volunteering our time here. If you have a question then post and wait. We will get to it if and when we have the time, expertise and inclination. If you want to be able to make demands then you might think about paying someone for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width