Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Resize Drawing.Bitmap on runtime

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    48

    Resolved [RESOLVED] [2005] Resize Drawing.Bitmap on runtime

    Hello,

    I have a certain algorithm which fills a Drawing.Bitmap according to a file on the computer. This bitmap is set as the background image of a picturebox. The user can select a different file, at which point I need to update the contents of the bitmap.

    This is quite easy (doing a pcb.Invalidate() after the updating), but the problem is that the bitmap can vary in size. I basically need to resize the bitmap every time the user selects a different file.

    The bitmap is a public variable. What I tried was disposing the bitmap and then do a "myBMP = New Bitmap(newX, newY)" to update the size. I get a "ArgumentException was unhandled" error, but for some reason it does not tell me which line it happened on.

    So, I basically need to know how to resize a bitmap on runtime. Also, the algorithm takes too long to add it to the Paint event of the picturebox. I really need to set it as an image so I have to draw it only once.

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

    Re: [2005] Resize Drawing.Bitmap on runtime

    Your terminology is a bit all over the place so it's a bit hard to determine exactly what's supposed to be going on. Please post your current code and tell us what you expect to happen at each stage.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    48

    Re: [2005] Resize Drawing.Bitmap on runtime

    Ok. I have declared a public Bitmap variable in a module like so;

    Code:
    Public myBMP As Bitmap
    Then, in the Load event of the application, I do the following;

    Code:
    myBMP = New Bitmap(1, 1)
    myPCB.BackgroundImage = myBMP
    The first line is because otherwise the second line throws me an error. When the user opens a certain file, a sub is called which does the following;

    Code:
    ' resize the bitmap
    myPCB.Dispose()
    myPCB = New Bitmap(newX, newY)
    
    ' algorithm which takes about 2 seconds
    
    ' redraw
    MDIParent1.myPCB.Invalidate()
    (The algorithm is reading a 'tileset' which contains a number of tiles (can range from 1 to, theoretically, infinite, but practically up to a thousand or so). What it does it read every tile it contains (which is 32x32 pixels, so numberOfTilesx32x32 loops) and places all pixels on the Bitmap.)

    So, the bitmap needs to be resized every time a different tileset is loaded. I was hoping it would work like I tried to, but it gives me the error as I described in my initial post.

    I hope I explained it better this way.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Resize Drawing.Bitmap on runtime

    That doesn't make much sense. myPCB must be of a type derived from Control if it has a BackgroundImage property, yet you're assigning a Bitmap object to it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    48

    Re: [2005] Resize Drawing.Bitmap on runtime

    What? myPCB is simply a Picturebox on the form, which has a BackgroundImage property. Perhaps I should just ask this without all the fuzz around it...

    How can I resize a Drawing.Bitmap object on runtime?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Resize Drawing.Bitmap on runtime

    Well, you can't actually resize a Bitmap. You can create a new Bitmap of a specific size and then draw the original Bitmap onto it.
    vb.net Code:
    1. Using g As Graphics = Graphics.FromImage(newBitmap)
    2.     d.DrawImage(<see documentation for appropriate parameters>)
    3. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    48

    Re: [2005] Resize Drawing.Bitmap on runtime

    In fact, I figured it out. I'll explain what I did for those who want to know.

    I still have the bitmap as a public variable. When the user selects a new file, I first set the Picturebox.BackgroundImage property to Nothing. Then I dispose the Bitmap file, after which I make it a new Bitmap with the correct width and height. I then apply the algorithm, after which I update the height of the Picturebox (width is constant) to the height of the Bitmap. Last I set the Picturebox.BackgroundImage property to the Bitmap, and then I invalidate it to show the bitmap.

    Here's the code;

    Code:
    ' first prepare the background image
    MDIParent1.pcbTileSet.BackgroundImage = Nothing
    gTileSetViewBitmap.Dispose()
    gTileSetViewBitmap = New Bitmap(224, CInt(Math.Ceiling(tTileInfo.sTileCount / 7) * 32))
    
    ' algorithm
    
    ' redraw
    MDIParent1.pcbTileSet.Height = gTileSetViewBitmap.Height
    MDIParent1.pcbTileSet.BackgroundImage = gTileSetViewBitmap
    MDIParent1.pcbTileSet.Invalidate()
    Thanks for the help, jmcilhinney. I really appreciate the time you're taking to look at my (and everyone's) problems

    PS: In the Load event of the application, I first do this;
    Code:
    gTileSetViewBitmap = New Bitmap(1, 1)
    Otherwise, it throws an error when I dispose the Bitmap (which is logical, of course).

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] Resize Drawing.Bitmap on runtime

    Rather than creating a Bitmap that you never use other than to dispose, how about testing whether the variable is a null reference before disposing it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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