Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / VB / VB.net - String type to Image type conversion?

Author
Message
xplosys
19
Years of Service
User Offline
Joined: 5th Jan 2006
Playing: FPSC Multiplayer Games
Posted: 23rd Jan 2012 04:34
I have a VB form called "Deck" which holds 52 picture boxes, namely a deck of playing cards. Another form is called "Table". From the Table I can fill a picture box with any card I wish by assigning it the same image of the corresponding picture box from the Deck using it's picture property. Simple enough.

Now I've coded a random card picker. The card picker picks the suit and number of a card and checks to see if it's already been picked. It assigns the card a name such as...

D13 for the Kind of Diamonds or
H6 for the 6 of Hearts, and so on.

These names match the images in the picture boxes on the Deck form. How can I assign this new selected card to a picture box on the Table using it's name? D13 or H6, etc? I can't find a way to convert this name (string) to an acceptable image format. Can anyone enlighten me or point me in another direction?

I don't want to go through 52 case selects each time a new card is dealt.

Brian.

!retupmoc eht ni deppart m'I !pleH

budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 23rd Jan 2012 15:44
I'm a bit confused. Do the picture boxes and the images have corresponding names? Are the images the ones with the specific names, and are being assigned to random picture boxes? Are the picture boxes the ones with the names.

Not sure if it will help, there's the Bitmap class, you can create a bitmap in memory of the specified file, then use that in the picture box:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx


"I am a game making juggalo so ha"
xplosys
19
Years of Service
User Offline
Joined: 5th Jan 2006
Playing: FPSC Multiplayer Games
Posted: 23rd Jan 2012 16:51
@budokaiman

Thanks for the response.

Let me break it down a little further. I have a picturebox named picC7. The background picture already assigned to this picturebox is a picture of the 7 of Clubs. (C7)

I can assign that picture to another picture box like this...

pic1.BackgroundImage = picC7.BackgroundImage

However, that means I need to select the image manually. I have some code that selects an image at random and assigns "picC7.BackgroundImage" to a variable name "Card"

What I want to do is assign the variable "Card", which contains the string "picC7.BackgroundImage" to pic1 as a background image, but VB won't accept a "string" as an "image" type. How can I convert the string to an image type?

I hope that is a little more clear. The question is, how can I convert a string type to an image type. Using "CType" doesn't work.

Brian.

!retupmoc eht ni deppart m'I !pleH

budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 23rd Jan 2012 22:47
Okay, I see what you mean now. What you're trying to do isn't very efficient though, this is how I'd do it.

1. Add and embed a resource of all the images to your solution:
Project > add existing item > select the images
click on the images in your solution explorer in VS, then go to the properties window and change "build action" to "embed resource"

2. Add an assembly handler to your project:


3. Create an image stream that will use the resource by name.


Then just change your random code to generate the card letter and number and add that between the solution and file format.

Hopefully that helps.


"I am a game making juggalo so ha"
xplosys
19
Years of Service
User Offline
Joined: 5th Jan 2006
Playing: FPSC Multiplayer Games
Posted: 23rd Jan 2012 23:29
Thanks again, I've already added all the images to the project, but I wanted to try it the other way. Still, I've found no way to do it so I'll stick with what works. I don't know what you mean by not efficient though. The images are either on a form or in a folder. The code would have been shorter if I could have made it work my way.

Thanks,

Brian.

!retupmoc eht ni deppart m'I !pleH

budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 24th Jan 2012 00:19
Quote: " I don't know what you mean by not efficient though."

I may be wrong, but what it looked like was you've got picture boxes for all 52 cards, then you have more picture boxes which will be assigned the image from the other picture box. If you use the image stream, you just need the images inside the project's resource file (which they should be anyway if you added the images in the visual editor).

Quote: "The code would have been shorter if I could have made it work my way."

There really wouldn't be much extra work, it would probably only be about 4 lines longer.




"I am a game making juggalo so ha"
xplosys
19
Years of Service
User Offline
Joined: 5th Jan 2006
Playing: FPSC Multiplayer Games
Posted: 24th Jan 2012 00:49
Thanks again. Not exactly what I did, but your code is cleaner. I appreciate that.

Brian.

!retupmoc eht ni deppart m'I !pleH

budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 24th Jan 2012 14:17
Quote: "Thanks again. Not exactly what I did, but your code is cleaner. I appreciate that."

You're welcome, I'm happy to help.


"I am a game making juggalo so ha"

Login to post a reply

Server time is: 2025-05-22 03:29:52
Your offset time is: 2025-05-22 03:29:52