kramed

Hi there,

What I am trying to do is be able to draw an image (based on defining variables) to a picture box.

Example I have this line of code and it works perfect:

Code Block

handcard_picbox2.Image = new Bitmap(Properties.Resources.cj);


But 'cj.gif' is an image of a jack of clubs face card that is named cj in my .resx. My program randomly deals/shuffles cards but I am trying to figure out how to draw the random card. The problem I am running into now is how do I populate the picture box based on what card I need I can determine what card/suit of the card fine, but how can I pass a varying path to a resource file I tried...

Code Block

string go = "Properties.Resources.cj";
handcard_picbox1.Image = new Bitmap(go);



But that passes the wrong type to the picboxes property aparently. Can someone point me in the right direction I hope I was clear and provided enough information.


Mark


Re: Windows Forms General Passing a random resource image file to Picturebox

Oleh Svintsitskyy

Hi kramed,

You can create mappings when form loads

Code Block

private Dictionary<INT< FONT>, string> m_mapping;

private void Form1_Load(object sender, EventArgs e)

{

m_mapping = new Dictionary<INT< FONT>, string>();

m_mapping.Add(1, Properties.Resources.card1);

m_mapping.Add(2, Properties.Resources.card2);

m_mapping.Add(3, Properties.Resources.card3);

m_mapping.Add(4, Properties.Resources.card4);

}


and use them whenever needed

Code Block

private Image GetRandomImage()

{

Random rnd = new Random();

int nNextCard = rnd.Next(1, m_mapping.Keys.Count);

return m_mapping[nNextCard];

}

private void UpdatepictureBox()

{

handcard_picbox1.Image = GetRandomImage();

}

If You want to use GIF frames instead of separate images You will have to exract each frame(using NGif for example) create mappings and extract random frame when needed.

No need to create new Bitmap each time. It is of Image type. You can check this is Resources.Designer.cs file.

Hope this helps.

Regards,

Oleh.





Re: Windows Forms General Passing a random resource image file to Picturebox

kramed

Thanks for the help, I greatly appreciate it!