tags:

views:

1092

answers:

4

It's very easy to mark an image file to become an embedded resource however how does one access the image thereafter. Please can I have some example code?

+1  A: 

Look at the third code snippet at http://msdn.microsoft.com/en-us/library/aa309403(VS.71).aspx

Franci Penov
+1  A: 

The most direct method:

YourProjectsBaseNamespace.Properties.Resources.YourImageResourceName
Will
+2  A: 

1) Adding and Editing Resources (Visual C#)

System.Drawing.Bitmap bitmap1 = myProject.Properties.Resources.Image01;

2) Accessing Embedded Resources using GetManifestResourceStream

Assembly _assembly = Assembly.GetExecutingAssembly();

Stream _imageStream = 
    _assembly.GetManifestResourceStream(
    "ThumbnailPictureViewer.resources.Image1.bmp");
Bitmap theDefaultImage = new Bitmap(_imageStream);
Gulzar
And, of course, you'd put a using statement around that Stream declaration...
bdukes
@bdukes: Not necessarily. Not sure how the `Bitmap` class works, but if it's like the `Image` class you can't dispose the stream until you are done with the image. And then you should dispose the image, not the stream. (At least if I have understood the documentation correctly :)
Svish
A: 
//Get the names of the embedded resource files;

List<string> resources = new List<string>(AssemblyBuilder.GetExecutingAssembly().GetManifestResourceNames());

//Get the stream

StreamReader sr = new StreamReader(
       AssemblyBuilder.GetExecutingAssembly().GetManifestResourceStream(
        resources.Find(target => target.ToLower().Contains("insert name here"))

You can convert from bitmap from the stream. The Bitmap class has a method that does this. LoadFromStream if my memory serves.

Leahn Novash