I already have a variable which stores the result of a random object, now I want to use that number to load the image.
The images are labelled1, _2, _3 etc etc
To explain it, I want it something like this:
diceBox1.Image = VirtualGame.Properties.Resources. + diceVal1;
So say the variable diceVal1 has a value of 2, I want it to load image _2.
How can I go about doing this?
Mar 11, 2017 12:56 AM
(This post was last modified: Mar 11, 2017 12:58 AM by ShadowCloudGH.)
(Mar 11, 2017 12:35 AM)VirtualFrost Wrote: I already have a variable which stores the result of a random object, now I want to use that number to load the image.
The images are labelled1, _2, _3 etc etc
To explain it, I want it something like this:
diceBox1.Image = VirtualGame.Properties.Resources. + diceVal1;
So say the variable diceVal1 has a value of 2, I want it to load image _2.
How can I go about doing this?
I'm not sure what you are struggling with?
Back before string interpolation I would do this with string.format("{0}_{1}.{2}", VirtualGame.Properties.Resources, diceVal, imgExtensions )
Where imgExtension is jpg or gif or whatever you are using. It's not clear to me what you are struggling with? Did you want an extensionMethod on some class so you could go
diceVal1.GetImagePath() or did you just want to wrap the above in a method?
This is rather easily achieved with string interpolation, although I'm not sure if the multiline issue has been fixed... I know this compiled to string + string previously for multiline values which ended up causing performance hickups with large amounts of iteration, I am sure it has been fixed though, so feel free to use that instead of string.Format
(Mar 11, 2017 12:56 AM)ShadowCloudGH Wrote: I'm not sure what you are struggling with?
Back before string interpolation I would do this with string.format("{0}_{1}.{2}", VirtualGame.Properties.Resources, diceVal, imgExtensions )
Where imgExtension is jpg or gif or whatever you are using. It's not clear to me what you are struggling with? Did you want an extensionMethod on some class so you could go
diceVal1.GetImagePath() or did you just want to wrap the above in a method?
This is rather easily achieved with string interpolation, although I'm not sure if the multiline issue has been fixed... I know this compiled to string + string previously for multiline values which ended up causing performance hickups with large amounts of iteration, I am sure it has been fixed though, so feel free to use that instead of string.Format
I'm not that great at programming so I'll explain it the best I can without using the wrong terminology
diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1;
^^ This does not work. Basically to put strings and variables together, you'd generally go like "The value is " + randomValue; right? I'm trying to do the same here basically
diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1;
would become
diceBox1.Image = VirtualGame.Properties.Resources._1;
if diceVal1 = 1.
I have a roll button, each time I click it, diceVal1 will change to a random number between 1-10, then I want to use that value so it can load the correct image. Does that help?
(Mar 11, 2017 01:15 AM)VirtualFrost Wrote: I'm not that great at programming so I'll explain it the best I can without using the wrong terminology
diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1;
^^ This does not work. Basically to put strings and variables together, you'd generally go like "The value is " + randomValue; right? I'm trying to do the same here basically
diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1;
would become
diceBox1.Image = VirtualGame.Properties.Resources._1;
if diceVal1 = 1.
I have a roll button, each time I click it, diceVal1 will change to a random number between 1-10, then I want to use that value so it can load the correct image. Does that help?
Can you let me know what you get when you change from
diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1
to
diceBox1.Image = VirtualGame.Properties.Resources + "._" + diceVal1.ToString()
(Mar 11, 2017 01:44 AM)ShadowCloudGH Wrote: Can you let me know what you get when you change from
diceBox1.Image = VirtualGame.Properties.Resources._ + diceVal1
to
diceBox1.Image = VirtualGame.Properties.Resources + "._" + diceVal1.ToString()
http://prntscr.com/eikpj6
I think the way i uses to load images in c# the first semester was using something like
DiceBox1.image = Properties.Resources.ResourceManager.GetObject("_" + diceVal1. ToString()
Not exactly sure anymore tho
Mar 11, 2017 01:58 AM
(This post was last modified: Mar 11, 2017 02:03 AM by ShadowCloudGH.)
(Mar 11, 2017 01:51 AM)VirtualFrost Wrote: http://prntscr.com/eikpj6
Ah, now it makes sense. Currently you are pointing to the class Resources, rather than a specific property in the resources.
You are using
Properties.Resources
You should be using
Properties.Resources.ResourceName
There's a nice example from http://stackoverflow.com/questions/90697...ces-in-net
Code paused = !paused;
if (paused)
notifyIcon.Icon = Properties.Resources.RedIcon;
else
notifyIcon.Icon = Properties.Resources.GreenIcon;
You would use GetObject if the resource was the actual image, GetString for a string with the image path rather than the image object ;)
Mar 11, 2017 02:09 AM
(This post was last modified: Mar 11, 2017 02:10 AM by VirtualFrost.)
(Mar 11, 2017 01:58 AM)ShadowCloudGH Wrote: Ah, now it makes sense. Currently you are pointing to the class Resources, rather than a specific property in the resources.
You are using
Properties.Resources
You should be using
Properties.Resources.ResourceName
There's a nice example from http://stackoverflow.com/questions/90697...ces-in-net
Code paused = !paused;
if (paused)
notifyIcon.Icon = Properties.Resources.RedIcon;
else
notifyIcon.Icon = Properties.Resources.GreenIcon;
You would use GetObject if the resource was the actual image, GetString for a string with the image path rather than the image object ;) The end result I want is
diceBox1.Image = Properties.Resources._1; (If diceVal1 = 1, so yes you're right)
That's what I'm trying to work out though, I can't have that always be static, I need the image to change depending on the result of diceVal1. How can I change 1 to be the result of diceVal1? I'm not asking how to use Properties.Resources, I'm asking how can I change the image using that so it changes each time the value changes.
(Mar 11, 2017 01:58 AM)Domyos Wrote: I think the way i uses to load images in c# the first semester was using something like
DiceBox1.image = Properties.Resources.ResourceManager.GetObject("_" + diceVal1. ToString()
Not exactly sure anymore tho
Error for me, cannot convert object to System.Drawing
(Mar 11, 2017 02:09 AM)VirtualFrost Wrote: The end result I want is
diceBox1.Image = Properties.Resources._1; (If diceVal1 = 1, so yes you're right)
That's what I'm trying to work out though, I can't have that always be static, I need the image to change depending on the result of diceVal1. How can I change 1 to be the result of diceVal1? I'm not asking how to use Properties.Resources, I'm asking how can I change the image using that so it changes each time the value changes.
O, for some reason I was under the impression you have the resource pointing to a base path, like
"/images/dicepicturefor"
And in the actual Images folder you have
dicepicturefor_1.jpg
dicepicturefor_2.jpg
dicepicturefor_3.jpg
And so on and so forth.
From the last message I understand you have different properties in your resources that you would like to access dynamically like this:
Code ResourceManager rm = new ResourceManager(typeof(YourResourceClass));
string someString = rm.GetString("Properties.Resources._" + diceval.ToString());
(Mar 11, 2017 02:09 AM)VirtualFrost Wrote: Error for me, cannot convert object to System.Drawing
Cast it to a Bitmap/Image.
Lotta blood to let, peace to make, give a fuk about a check
(Mar 11, 2017 02:09 AM)VirtualFrost Wrote: The end result I want is
diceBox1.Image = Properties.Resources._1; (If diceVal1 = 1, so yes you're right)
That's what I'm trying to work out though, I can't have that always be static, I need the image to change depending on the result of diceVal1. How can I change 1 to be the result of diceVal1? I'm not asking how to use Properties.Resources, I'm asking how can I change the image using that so it changes each time the value changes.
Error for me, cannot convert object to System.Drawing
Try a cast
Code var r = new Random();
pictureBox1.Image = (Bitmap)Properties.Resources.ResourceManager.GetObject($"_{r.Next(1, 4)}");
(Mar 11, 2017 02:18 AM)Owl Wrote: Try a cast
Code var r = new Random();
pictureBox1.Image = (Bitmap)Properties.Resources.ResourceManager.GetObject($"_{r.Next(1, 4)}");
Brilliant, this is what I needed, works perfectly. Thanks
|