How to use imported images/resources images?

ljpv14

Active member
Joined
Nov 28, 2011
Messages
44
Programming Experience
3-5
Hello guys! I've been coding using vb.net for a while but I've been using imageLocation property to set an image for a picturebox or for the background image of a form. How do I use those images in my Resources Folder in my solution explorer to set an image for a certain picturebox or form. I sorta need a code for it. Thank you biggy in advanced! :D
 
Have you been adding those image files via the Resources page of the project properties? If not then you should. In that case, remove the files from the Resources folder and then re-add them to the project properties.

You can then get an Image object in code using My.Resources.SomeProperty, where SomeProperty is the name of your resource. That Image object can be assigned to the Image property of a PictureBox.

A point to note is that, each time you use a property of My.Resources, a new object is created. As such, if you intend to use an Image multiple times then it would be preferable to assign it to a variable and reuse it from there. Also, just like any other Image, you should Dispose it once you're done with it.
 
I do importing of images when setting form background images or pictureboxes, but not when accessing my resources through code. I'm using imageLocation which wastes my code and code transitions.

Regarding disposing, how do I do that? Is it just a simple Image.dispose()? Or is there a proper way of disposing images. I really need to dispose since my game uses multiple images. I often experience lag in my game when executing.

Thank you for the help! Really appreciated it. :)
 
I do importing of images when setting form background images or pictureboxes
When doing this choose "Project resource file" rather than "Local resource". Importing to file "My Project/Resources.resx" adds it to project resources including generating the My.Resources property that can be used in code.
Image.dispose()? Or is there a proper way of disposing images.
Yes, calling the Dispose method on the object you want to dispose of is the way to do it.
 
A follow up question about dispose. When is the right time to dispose an image? For example, I assigned a image for the hover event, when the mouse leave and I will reassign another image for leave event, should I dispose the image for the hover event? If I'm disposing, what is happening to the image? Will it be deleted from my resources? Or its instance will be the one to be deleted by the dispose function? Thanks guys!!!
 
The right time to dispose an Image is the same as the right time to dispose any disposable object.
Also, just like any other Image, you should Dispose it once you're done with it.
If an object is still being used, e.g. an image is being displayed in a PictureBox, then you're not done with it. If you're finished with the object for now and it may or may not get used again then you have to make a call based on the circumstances.
 
Back
Top