Sign in to follow this  
Michael Mossman

Drawing Image onto PictureBox

Recommended Posts

I think I am going down the same road already travelled by LordRhys in his posting in October 2005. This has helped me to overcome my initial errors of reading an imbedded resource into a bitmap. Now, when I try to display the bitmap I get an exception that the object is already in use. I have created a Graphics object from the PictureBox as follows :- Dim srcPic as Graphics = PicBox.CreateGraphics. I have created a bitmap called cardImage as per the discussion in October 2005. N.B. Because of the problems with creating an array of bitmaps, I am starting off with only the one ! My attempt to display is as follows :- srcPic.DrawImage(cardImage,intNextSuit,intNextCard,35,50). This line throws the exception "Object already in use somewhere else" Could LordRhys ( or any other kind guru out there ) please help me to progress. Thanks, Mike.

Share this post


Link to post
Share on other sites
Thanks ApochPiQ.

The context of this problem is trying to use a gif file of a playing card and then turning it into a bitmap in order to display it inside a PictureBox.

The relevant code snippet is shown below :-

--------

Public Sub ShowHand()

Dim intNextSuit As Integer
Dim intNextCard As Integer

'Get a Graphics Object from the form
Dim srcPic As Graphics = _display.CreateGraphics

'create Bitmap of card
Dim execAssem As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim cardImage = New Bitmap(execAssem.GetManifestResourceStream("BridgeTestBed.1.gif"))

'set the x and y co-ordinates for display
intNextSuit = 2
intNextCard = 0

srcPic.DrawImage(cardImage, intNextSuit, intNextCard, 35, 50)

-------

It is when trying to execute the final command that a memory violation exception is thrown.

N.B. I'm fairly certain that the conversion from gif to bitmap is OK as this was the subject of a lengthy thread last year.

Does this help you to point to my error ?

Thanks, Mike.

Share this post


Link to post
Share on other sites
One thing - if you're loading from the resource table, you have to make sure the permissions are set up. Right click on the .GIF file in the Solution Explorer, click Properties, and ensure that Build Action is set to Embedded Resource.

With that set up, your code should work without errors.

Share this post


Link to post
Share on other sites
Argh, going to MSDN nearly crashed my computer!

That said, this code should work. Does it reliably crash on this line every time the function is called, or is it intermittent (i.e. it sometimes paints successfully)? The error indicated that the graphics object is in use already; there's two reasons for that I can see:
(a) you're calling this method on a background thread while the UI thread is painting the window
(b) you aren't Dispose()ing the graphics objects you create and after a few goes it won't let you use CreateGraphics() any more.
Neither of those really explains a persistent crash though =| so I am probably not at the answer!

(Btw, props for doing a bridge game. Let's play my AI against yours one time ;D)

Share this post


Link to post
Share on other sites
Thank you, folks for the help and suggestions.

First of all, an apology. The exception is not a memory violation but "Object already in use". I did say that on my first post but had a small mental aberration when showing my code fragment.

Secondly, I have changed the build action to embedded resource.

Finally, this does happen all the time, even on the first attempt.

One thing that does occur to me, bearing in mind my total inexperience with .net, is that I may be completely misunderstanding the concept of graphics display.

In my ignorance, I am trying to put a bitmap onto a PictureBox. In the code fragment,cardImage is the bitmap and srcPic is the PictureBox.

Is my DrawImage statement actually legal and correct in this concept ?

P.S. Bob, I would love to take up your challenge but I'm afraid my project is just not yet ready for this experience !

Share this post


Link to post
Share on other sites
Perhaps it is something to do with loading the picture from a resource – perhaps it is the resource that is already in use? Try replacing the line where you get the picture from the resource with
cardImage = new Bitmap(100,200);

(and possibly some drawing code so you can see if it works) and see if that runs successfully.

Share this post


Link to post
Share on other sites
Hey Bob, that really did something !

First of all, I did what you said but came up with the same error, so I dug a little bit deeper and found out that all of the underlying attributes of srcPic were marked as "Already in Use" in the debug window when I clicked on the '+' sign.

This means it is the srcPic which is already in use and then I found the following line of code (which was a hangover from a previous attempt). :-

'get the IntPtr of the graphics
Dim HDC1 As IntPtr = srcPic.GetHdc

I removed the offending line and, voila, up came my display in the PictureBox in the right place.

Thanks for your patience and your help. That nicely signs off this thread but, at the current rate of progress, I have a suspicion that vb2005 will definitely send me back here with other problems.

Share this post


Link to post
Share on other sites
As it turns out, the PictureBox control has an Image property, which you can set to the image you want to be displayed. The functionality is equivalent to what you're doing but it's simply a more concise way of doing the same thing. It might have even worked given the outstanding device context, but I don't know for sure.

Share this post


Link to post
Share on other sites
Zipster: that property won't let you position the subimage yourself, nor let you draw mutliple subimages to the picture box (and I assume multiple cards will be drawn at some stage), so I think the OP's solution is correct here.

Michael: glad I could help :). It's a good strategy when confronted with a crash: if there are two or more things that could be the cause, remove all but one of them and test each in turn.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this