How to access raw image data in resource file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BeemerBiker
    New Member
    • Jul 2008
    • 87

    How to access raw image data in resource file?

    I have a .png file that my console app reads and puts into a structure that is saved as a file. The png is always the same so I thought I could just add it as a resource image to a resource file so it would be included in my program and not as a separate file. When I did that, the png shows up as a "SystemDrawingB itmap". If I examine this resource in a watch I see "Base" and "Static" members. Expanding those properties does not give me anything useful such as a pointer to the raw png data and its length in bytes.

    If worse comes to worse, I can always make a hex dump of the png contents and then put the hex code into a CS file using static initialization.

    Surely there is some way I can access the raw data internally and read the bytes into a byte array using C#

    Thanks for looking!



    sorry again - I made a better google search and got the answer I needed: "MemoryStre am" and image.Save.
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    I'm not completely understanding your question, can you expand on what you're trying to accomplish please. Also, in case someone does understand what you're asking could you post the code you're using to read the image into your structure and saves the file, that would help a lot :)

    EDIT: Also show how you're reading the data back into your console application
    Last edited by PsychoCoder; Nov 15 '12, 12:38 AM.

    Comment

    • BeemerBiker
      New Member
      • Jul 2008
      • 87

      #3
      Yea - I found the solution and didnt bother posting it exactly. I noticed this post seem to have a lot of views and this was what I found that worked:
      Code:
       public static byte[] ConvertImage(System.Drawing.Image img)
          {
              Byte[] data;
              using(MemoryStream ms = new MemoryStream())
              {
                  img.Save(ms, img.RawFormat);
                  data = new byte[ms.Length];
                  data = ms.ToArray();
              }
              return data;
          }
      ---
      ---
              public byte[] PngArray = ConvertImage(PngResource.jys33);
      // PngResource was the resource file that I dropped jys33.png into

      Comment

      Working...