Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = "1.JPEG"
Label1.Text = "what text"
End Sub
when i clck this button, the text "what text" will appear. but the picture stil can;t be display.
the error showed is " value of type string cannot be converted to 'System.Drawing.Image'"
May i know what happen>

May i know what happen here??
Nick Gabello
cgraus,
Visual Basic allow calling static methods on instances. You can tell the compiler to generate a warning if you do so, however.
gameprix1,
Since the method is static (shared) as cgraus pointed out, it doesn't modify the current instance - it returns the loaded image. If you haven't changed your compiler settings, you'll see that the VB compiler will warn you if you use the function in this way.
Best regards,
Johan Stenberg
Big-O
Here's the answer to your question....
To load an image into a picturebox from a file, this is what you need to do in VB 2005. It's probably the same in VB 2003, but I can't remember off the top of my head right now if it's changed...
PictureBox1.Image.FromFile("C:\image.jpg")
There is no need for the rest of the code, but there IS a need to directly point to the image file you plan to load. If you have any other problems, let us know!
fudicator
ssloka
Hmm... given that FromFile is a static method on the Image class, does VB allow calling static methods on a class instance If so, then the method is there because it's a static method on the object, and it returns an Image, which does not get passed to the class instance, so the class instance is never initialised. Just a guess....
SEH
PictureBox1.Image = System.Drawing.Image.FromFile("C:\Image.jpg")
Best regards,
Johan Stenberg
Neeti
THanks you so much,MSFT Johan Stenberg
FrankEvans
Check out the last paragraph in http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcn7/html/vaconSharedMembers.asp.
This is exactly the confusion they are talking about
The following code snippet:
Dim img as New Image
img.FromFile("myImage.jpg") ' This will *not* touch img - it will return a new image...
will call exactly the same static method as this:
System.Windows.Forms.Image.FromFile("myImage.jpg")
img is *not* modified - the method is static and has no concept of a "current instance" that it can modify.
It will, however, return a new image, but since you are not storing the return value in a variable it is lost.
You could have written something like this:
Dim pb as New System.Windows.Forms.PictureBox
Dim loadedImg as Image = pb.Image.FromFile("myImage.jpg") ' Confusing, but works...
or even
Dim pb as New System.Windows.Forms.PictureBox
pb.Image = pb.Image.FromFile("myImage.jpg") ' Even more confusing, but it still works...
and you would have stored the value, but I agree with the VB compiler when it gives me the following warning:
warning BC42025: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
and think that this kind of use of static methods is *not* to recommend.In my opinion, the "correct" and non-confusing way to use the method is:
Dim pb as New System.Windows.Forms.PictureBox
pb.Image = System.Windows.Forms.Image.FromFile("myImage.jpg")
Best regards,
Johan Stenberg
ef
Hmmmm... Right you are... My code did not display the image, but your code did... But, the question is... Why doesn't PICTUREBOX.Image.FromFile actually LOAD and DISPLAY the image I mean, what is it there for in the first place if it doesn't do what it's suppossed to do This MUST be a bug of some sort in the picturebox control. If you search for "FromFile" in the Help file you will find it does NOT relate to the PictureBox Control. It's not even marked obsolete! If you know the answer to this question, please let all of us know. Thanks.
Lastly, I apologize for assuming the code I provided would work instead of checking it first. I hate it when others assume things and hate it even more when I do it myself...
ImpureEvil
Thanks - I whipped up a demo app and found this to be the case. To be honest, although I use C# and not VB.NET, I agree with vB.NET here. C++ does the same thing - a static method is scoped at a higher level than an instance method and I believe it should be visible in both scopes.
DeepakGH
and my application save at my document.
then i type in PictureBox1.Image.FromFile("C:\image.jpg")
when i build it, there are no error, but when run the .exe there are nothing display.
casgo
Assuming that 1.jpeg is a file that you're trying to load:
Dim img as Image = Image.FromFile("1.jpg")
something like that ( I don't do VB ). Then
PictureBox1.Image = img
is what you'll need. Of course, this assumes that 1.jpg is in a folder that's directly visible to the exe.
R_oo_T
i get.
lcdmendes
I just feel dumb for not thinking it through and realising that no intermediate variable was needed. Although I have to admit, I find the PictureBox a pointless control, I never use them.
vitty
I understand why it's happening, but what I don't get is why it doesn't render the image once it's loaded... For example, I can declare an image variable that loads an image from a file and THEN pass it off to the PictureBox Control to render it. What step is missing if I do the same thing through the PictureBox instead The image is still loaded into the PictureBox's property, so why doesn't it render it the same way Why do I have to go through the extra step or create an instance of an image first in order to load and render it into a PictureBox Control I could be over-analyzing this but it just doesn't seem to make any logical sense to me...
Thanksll