Looseleaf

Hello

A trivial issue for you guys.

I have written an application that builds an image address string depending on what the users does.

i have for instance

dim where as string

where = "head_prog.my.resources.brain1100"

picturebox1.image = where

This address works in that if i simply type it in the image appears.

ie picturebox1.image = head_prog.my.resources.brain1100

Visual basic says this is a string and not a system drawing image and wont show image.

Thank you for any help.

Loosleaf




Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

JohnWein

picturebox1.image = Image.FromFile(where)





Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

Looseleaf

I should add ...

I have tried this......

dim where as string

dim brain as image

where = "head_prog.my.resources.brain1100"

brain = str(where)

picturebox1.image = brain

Cant't convert sting to image...

Help

Looseleaf






Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

Looseleaf

Hello

Thank you for your input.

Yes tried that.

Does not work.

String cannot be converted to image is error.

Looseleaf






Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

JohnWein

Try this: Copy and Paste a file you know is a picture to "C:\Test.bmp" then in your program load the picture into your picturebox using PictureBox1.Image = Image.FromFile("C:\Test.bmp")





Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

Looseleaf

Yes, I am sure that will work.

The problem is I want to include all images in my application when I compile. I don't want to have to tell user "copy directory so and so from cd to hard disk..

For this reason i have stuck the images in the resource file. So the application which runs ok using image.fromfile and a local directory is all in one chunk.

I can recover the images by

PictureBox1.Image = ct_head.my.resources.brain110

but not when I use string variable.

Thanks for your interest.






Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

nobugz

Do it like this:

Dim where As String = "brain1100"
PictureBox1.Image = CType(My.Resources.ResourceManager.GetObject(where), Image)






Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

Jason Payne

I think that explains the error message you're getting; .FromFile is looking for a path to a physical file, not a string reference to a resource.

It doesn't appear as though you can programmatically iterate through the non-literal contents of My.Resources, as there are no collection-esque members in that object.

You could always declare an imageList or something and eihter programmatically or design-time load your images into that, then reference its contents with either indexes or meaningful-description text keys, whose use could mirror My.Resources.ImageName, especially if you declare an enumeration to correspond to them. Images loaded into an imageList control as design time I believe are part of the compiled program, meaning that you wouldn't need to have users copy loads of image files into a directory or anything (though it will of course balloon the .EXE's file size).

That's how I do things like this; not sure if it works with what you want to do.




Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

Looseleaf

Good Feedback!

That is what I thought.

Jason: I looked at image lists.... they seem to work ok for controls like buttons but I could not get it to work for picture boxes. Maybe the answer is to use a panel rather that a picture box... that has helped in the past with other issues.

nobugz: Never heard of the CType method (or forgotten it). I'll give your solution a go first. Won't know until tommorrow, looks like the right solution for me if it works.

Once again, thank you for you support.

Cheers.






Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

Tall Dude

NoBugz has the right answer, but be sure

to note that the object string is case sensitive!






Re: Visual Basic General vb2005 string to get an image. wrong type apparently.

Looseleaf

Yes it works.

If you ever see a CT scan tutorial cd look in the credits for Nobugz.

Cheers.