❔ 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
the squarename variable contains a string with text being something along the lines of "a1.Image" or "b2.Image"
this is where i declare each value of the board array as a string (the "a1.Image" string and so on)
this is the error i keep getting
Well, an image is an image, and a string is a string
Also, what's with all those
global.
s?Isn't Image.FromFile means from stream?
Nope
public static System.Drawing.Image FromFile(string filename);
?
They're showing that the method
FromFile
returns a System.Drawing.Image
but you're trying to assign it to something that requires a stringso i string the path of the file or the name of the file (in this case being nought.png)?
I don't understand the question
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
that line with the error is where you're setting the "name" of the square though?
global.squarename = ...
no i have the name already in squarename
the problem is variable.Image is causing problems
nope
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
Check that line again, you're trying to set the value of
global.squarename
, which is a string
, to an Image
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
ok, but that's not the code you've sent through to us here
it is though because
squarename can be equal to "a1.Image" or "b2.Image" for example
as i set the value here
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 usalso 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
where?
here
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
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?here
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
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
i think this is where the confusion is
what do you mean by assigning a image?
as in
storing the image inside of squarename?
Yes, which is what you're trying to do with the line:
global.squarename = Image.FromFile(.....
you mean
the squarename variable
containing the image
?
that is what the line is "trying" to do yes, as the
Image.FromFile
method returns an Image
object, not a string
but that isnt what im trying to do
so
image command
Then you shouldn't be using the
Image.FromFile
method therethe one to set the image on a button
it worked for named buttons though?
like
a1.Image = ......
Where's that code?
Right, so a1 is the name you've given to a button that you've created in the winforms designer I assume?
yes
wait i have a diagram i made
so a1's image is set to the cross.png on the right
on click
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 thingsnow 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?
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
well i tried just storing
for example
"a1" in squarename
but global.squarename.Image = ...
does not work
neither does (global.squarename + .Image) = ...
that's because
squarename
is just a string
variable, it doesn't have an Image
propertyor anything like that which i have tried
yes i know
ok, so why would you expect it to work?
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
it wouldn't work like that, no
how could i change it to make it work in that way
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
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
tried doing it this way
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
as it starts a1 as a nought
and only produces noughts on click
What are the
value1
and value2
variables for?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
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?if we cant do the string.Image thing i dont think it would make much of a difference
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 themselvesAh I think I understand
Let me try that
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.