5

I added image to my c# project from Project settings -> Resources
How can i get this image at runtime?
I trying this:

public byte[] GetResource(string ResourceName)
{
    System.Reflection.Assembly asm = Assembly.GetEntryAssembly();

    // list all resources in assembly - for test
    string[] names = asm.GetManifestResourceNames(); //even here my TestImg.png is not presented

    System.IO.Stream stream = asm.GetManifestResourceStream(ResourceName); //this return null of course

    byte[] data = new byte[stream.Length];

    stream.Read(data, 0, (int)stream.Length);

    return data;
}

I call this function this way:

byte[] data = GetResource("TestImg.png");

But I see my image in Resources folder in project explorer.
enter image description here

Could anyone tell what's wrong there?
enter image description here

3
  • Why do you cast stream.Length to an int? It already is an int. .LongLength returns a long. Commented Apr 1, 2013 at 13:07
  • I don't know how, but for me Stream.Length is long Commented Apr 1, 2013 at 13:10
  • It's worked for me with use Assembly.GetExecutingAssembly() Commented Jun 29, 2021 at 4:46

4 Answers 4

9

You need to set the file TestImg.png as an "Embedded Resource." The resource name would then be Resources/TestImg.png.

Sign up to request clarification or add additional context in comments.

5 Comments

TestImg.png is not even presented in asm.GetManifestResourceNames(); What the difference then if changing name of resource I search for? And anyway, i tried, no luck
Anyways, can you tell us what asm.GetManifestResourceNames returns? That would help.
Where I can set TestImg.png as embedded resource? I don't see this in Project settings -> Resources
Thank you, finally in can be seen in names. To sad, the name is kinda weird - VLab_1_new.Resources.TestImg.png
3

This works:

    var info = Application.GetResourceStream(uri);
    var memoryStream = new MemoryStream();
    info.Stream.CopyTo(memoryStream);
    return memoryStream.ToArray();

In additional, if you want to save the image to your drive:

    var b =
    new Bitmap(namespace.Properties.Resources.image_resouce_name);
    b.Save("FILE LOCATION");

Comments

2

You can access the image with Properties.Resources.TestImg.

4 Comments

wow, this is so easy and really works :O thank you. Too sad it not at runtime
What I want is to show this image in WebBrowser by using data protocol msdn.microsoft.com/en-us/library/cc848897(v=vs.85).aspx
This is ok, but it at design time only... The web page may require any other name of image
Visual Studio by default causes the image resource to be returned as Bitmap rather than as a byte array. See my response.
2

You can edit the Resources.resx file and change:

  <data name="ResourceKey" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..\Resources\Image.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
  </data>

to (replace the type and assembly definition after the file name):

  <data name="ResourceKey" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..\Resources\Image.jpg;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </data>

Regenerate the .Designer.cs file, such as by opening the resources (.resx file) in the designer and changing its access type to internal and then back to public (or vice versa). Now you can access the image as byte array using Properties.Resources.ResourceKey. I don't know if this can be done without manually editing the resource file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.