How to get resource as byte array in c#?

10,452

Solution 1

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

Solution 2

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

Solution 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");

Solution 4

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>

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.

Share:
10,452
Kosmo零
Author by

Kosmo零

Updated on June 28, 2022

Comments

  • Kosmo零
    Kosmo零 almost 2 years

    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

  • Kosmo零
    Kosmo零 about 11 years
    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
  • Cole Tobin
    Cole Tobin about 11 years
    Anyways, can you tell us what asm.GetManifestResourceNames returns? That would help.
  • Kosmo零
    Kosmo零 about 11 years
    wow, this is so easy and really works :O thank you. Too sad it not at runtime
  • Kosmo零
    Kosmo零 about 11 years
    Where I can set TestImg.png as embedded resource? I don't see this in Project settings -> Resources
  • Kosmo零
    Kosmo零 about 11 years
    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
  • Kosmo零
    Kosmo零 about 11 years
    This is ok, but it at design time only... The web page may require any other name of image
  • Dustin Kingen
    Dustin Kingen about 11 years
  • Kosmo零
    Kosmo零 about 11 years
    Thank you, finally in can be seen in names. To sad, the name is kinda weird - VLab_1_new.Resources.TestImg.png
  • Mike Rosoft
    Mike Rosoft over 3 years
    Visual Studio by default causes the image resource to be returned as Bitmap rather than as a byte array. See my response.