Access Project Resources via Code

mafrosis

Well-known member
Joined
Jun 5, 2006
Messages
88
Location
UK
Programming Experience
5-10
Hey all,

You can add image resources to a project via the Resources tab in the project properties, but is there a way to access these images via code??

Currently im loading some images by path using Image.FromFile() - so it would be neater to use the project resource lib.

Cheers
mafro
 
Are you talking about setting an image as an embedded resource? If so you can access them using reflection.

VB.NET:
Image.FromStream(System.Reflection.Assembly.GetExecutingAssembly
.GetManifestResourceStream(parameters here)
 
If I have added the image resource myimage.jpg in application resources then retrieve it like:
VB.NET:
PictureBox1.Image = My.Resources.myimage
'or
PictureBox1.Image = My.Resources.ResourceManager.GetObject("myimage")
 
Cool thanks guys - Ive a slightly separate question which didnt get answered in another thread but has popped up again here:

Im guessing the My namespace is a VB.NET only thing, so if im coding C# how would I do the same thing? Using the (slightly) more complicated Reflection? If so, im assuming the My namespace is just a wrapper for some reflection code..

Only discovered My a few days ago.. seems odd to have stuff in VB.NET and not C#..
 
While this has nothing to do with vb.net it is related i suppose. You can use the My namespace in c# you just need to add a reference to it, though not all classes are compatible. Reference....

VB.NET:
Microsoft.VisualBasic.dll.
 
Back
Top