C
C#16mo ago
Based

❔ Cannot convert System.Drawing.Image to string

im trying to display a image on a button through a variable as its name in the name.Image = Image.Fromfile(..................) command
66 Replies
Based
BasedOP16mo ago
the squarename variable contains a string with text being something along the lines of "a1.Image" or "b2.Image"
Based
BasedOP16mo ago
this is where i declare each value of the board array as a string (the "a1.Image" string and so on)
Based
BasedOP16mo ago
this is the error i keep getting
Angius
Angius16mo ago
Well, an image is an image, and a string is a string Also, what's with all those global.s?
blueberriesiftheywerecats
Isn't Image.FromFile means from stream?
nukleer bomb
nukleer bomb16mo ago
Nope public static System.Drawing.Image FromFile(string filename);
Based
BasedOP16mo ago
?
SinFluxx
SinFluxx16mo ago
They're showing that the method FromFile returns a System.Drawing.Image but you're trying to assign it to something that requires a string
Based
BasedOP16mo ago
so i string the path of the file or the name of the file (in this case being nought.png)?
SinFluxx
SinFluxx16mo ago
I don't understand the question
Based
BasedOP16mo ago
wait i dont need to save the filename its only 1 file that i need to use im trying to make it so that the image is set to a random button so something like buttonnumber = "a1"; buttonnumber.Image = fromfile(file path yadaayadayada
SinFluxx
SinFluxx16mo ago
that line with the error is where you're setting the "name" of the square though? global.squarename = ...
Based
BasedOP16mo ago
no i have the name already in squarename the problem is variable.Image is causing problems
SinFluxx
SinFluxx16mo ago
nope
Based
BasedOP16mo ago
i also tried to just have the name of the square have the .Image already inside of it but it still doesnt work yeah the squarename is assigned im trying to set the image to that square's image
SinFluxx
SinFluxx16mo ago
Check that line again, you're trying to set the value of global.squarename, which is a string, to an Image
Based
BasedOP16mo ago
if im not mistaken a1.Image = Image.FromFile("C:\Users\saira\Documents\Summer Project 2023\TicTacToe Visual (Medium) Summer Project\TicTacToe Visual (Medium) Summer Project\Resources\Cross.png"); puts a image on the button named a1
SinFluxx
SinFluxx16mo ago
ok, but that's not the code you've sent through to us here
Based
BasedOP16mo ago
it is though because squarename can be equal to "a1.Image" or "b2.Image" for example
Based
BasedOP16mo ago
as i set the value here
SinFluxx
SinFluxx16mo ago
you're setting global.squarename to a string value, you're not setting an image unless there's something going on elsewhere that you haven't shown us
Based
BasedOP16mo ago
also a quick thing i changed about this was i changed the if condition to (global.value1 == 0 && global.value2 == 1) yeah the string being the name of the button a1,a2,a3,b1,b2 and so on +.Image so i can change the (blank) image of the named button to a constant image the problem is that im trying to impliment the variable into the setting image command and it doesnt accept it
SinFluxx
SinFluxx16mo ago
where?
Based
BasedOP16mo ago
here
Based
BasedOP16mo ago
because squarename for example would be "a1.Image" and a1.image written out would work normally but not when its stored in the string variable
SinFluxx
SinFluxx16mo ago
We've just been over this though, global.squarename is not an Image variable, it's a string variable where is this global object set up?
Based
BasedOP16mo ago
here
Based
BasedOP16mo ago
you want me to change the squarename variable to public static image? because wouldnt that store a bitmap of a image or something? i just need the location of the image to change each time
SinFluxx
SinFluxx16mo ago
All I'm saying is, you can't assign an Image to squarename because, as you can see in the image above, squarename is a variable of type string
Based
BasedOP16mo ago
i think this is where the confusion is what do you mean by assigning a image? as in storing the image inside of squarename?
SinFluxx
SinFluxx16mo ago
Yes, which is what you're trying to do with the line: global.squarename = Image.FromFile(.....
Based
BasedOP16mo ago
you mean the squarename variable containing the image ?
SinFluxx
SinFluxx16mo ago
that is what the line is "trying" to do yes, as the Image.FromFile method returns an Image object, not a string
Based
BasedOP16mo ago
but that isnt what im trying to do so image command
SinFluxx
SinFluxx16mo ago
Then you shouldn't be using the Image.FromFile method there
Based
BasedOP16mo ago
the one to set the image on a button it worked for named buttons though? like a1.Image = ......
SinFluxx
SinFluxx16mo ago
Where's that code?
Based
BasedOP16mo ago
SinFluxx
SinFluxx16mo ago
Right, so a1 is the name you've given to a button that you've created in the winforms designer I assume?
Based
BasedOP16mo ago
yes wait i have a diagram i made
Based
BasedOP16mo ago
Based
BasedOP16mo ago
so a1's image is set to the cross.png on the right on click
SinFluxx
SinFluxx16mo ago
OK, so that means a1 is a Button object, and it has an Image property that allows you to set what image will be displayed global is a class you've created yourself, which has a field called squarename that is just a string, so they're completely different things
Based
BasedOP16mo ago
now im trying to make it so that after a1 is clicked, a random unmarked square is set to nought.png a1 is the name of the button yes im trying to make squarename the name of a random button from this grid and then impliment it into the a1(or otherwise).Image = .... if you get what i mean?
SinFluxx
SinFluxx16mo ago
sort of, but that still means you shouldn't be trying to load an Image into the squarename variable, because squarename should be holding the name (a string), not an Image
Based
BasedOP16mo ago
well i tried just storing for example "a1" in squarename but global.squarename.Image = ... does not work neither does (global.squarename + .Image) = ...
SinFluxx
SinFluxx16mo ago
that's because squarename is just a string variable, it doesn't have an Image property
Based
BasedOP16mo ago
or anything like that which i have tried yes i know
SinFluxx
SinFluxx16mo ago
ok, so why would you expect it to work?
Based
BasedOP16mo ago
because id imagine the a1 in the a1.Image command to be a string so if i store say b2 in squarename and use it in place of the constant a1 it would work
SinFluxx
SinFluxx16mo ago
it wouldn't work like that, no
Based
BasedOP16mo ago
how could i change it to make it work in that way
SinFluxx
SinFluxx16mo ago
I think really you wouldn't want to make it work that way by passing around a string to try and then refer to an actual object
Based
BasedOP16mo ago
ok so instead how would i make it so that a random number from the array board is chosen which then pastes the image nought.png onto the assigned(?) button i couldnt think of a better word than assigned sorry about not wording what im saying very well im not very articulate
Based
BasedOP16mo ago
tried doing it this way
Based
BasedOP16mo ago
Based
BasedOP16mo ago
so ignore the squarename image thing entirely and use the randomly generated value1 and value2 to find the corresponding button name problem is that it doesnt work
Based
BasedOP16mo ago
as it starts a1 as a nought
Based
BasedOP16mo ago
and only produces noughts on click
SinFluxx
SinFluxx16mo ago
What are the value1 and value2 variables for?
Based
BasedOP16mo ago
to find a random square which is blank on the grid its used for the board 2d array so after you click to place the cross value1 and value2 randomly generate until it finds a value in the board array which is equal to 0 (blank) and tries to place a nought image there to the corresponding named button
SinFluxx
SinFluxx16mo ago
Instead of having your int[,] board I would have thought it would be easier to have a List<Button> board and then have your 9 buttons in that, then when a button is randomly selected you can just directly access and set its properties from there?
Based
BasedOP16mo ago
if we cant do the string.Image thing i dont think it would make much of a difference
SinFluxx
SinFluxx16mo ago
Well it would because then you can directly set the image for the random item you've picked from that List Because you'd be working on the Button objects themselves
Based
BasedOP16mo ago
Ah I think I understand Let me try that
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server