|
-
Feb 3rd, 2008, 07:51 PM
#1
Thread Starter
Member
[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.
-
Feb 3rd, 2008, 07:54 PM
#2
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.
-
Feb 3rd, 2008, 08:08 PM
#3
Thread Starter
Member
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.
-
Feb 3rd, 2008, 08:48 PM
#4
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.
-
Feb 4th, 2008, 05:13 AM
#5
Thread Starter
Member
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?
-
Feb 4th, 2008, 05:34 AM
#6
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:
Using g As Graphics = Graphics.FromImage(newBitmap) d.DrawImage(<see documentation for appropriate parameters>) End Using
-
Feb 4th, 2008, 06:26 AM
#7
Thread Starter
Member
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).
-
Feb 4th, 2008, 07:28 AM
#8
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|