retrieve binary image data from resx file to byte Array for display?

R

Rich P

This is just for exercise purpose, but I want to read the binary image
data contained in an resx file and display this in a Picturebox. I have
been searching the net and have some routines which read binary data to
a byte array - execpt it is mostly from a binary datafield in a DB.

How can I read the data like the following into a byte array and display
in a picture box? This data is a picture of a green forward arrow.


<data name="tsbNavForward.Image" type="System.Drawing.Bitmap,
System.Drawing"
mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1B
AACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAA
ARNJREFUOE9jYBgFJIWA/3TX3aH1oWwkaUJW7Nnv+N2pw/o2hgElxzP+g3Dh4dT/ufsS/2fs
iP2ftDHif8zK4P9hC33/+890/+850fF/0uKo/zFzQ/+blOtfRDEEpHndraX/V99Y9H/5tXn/
F1+Z9X/+pWn/Z52f/H/qmb7/E051/u8+3vq//Wjj/6ZDtf8DJnr918hUPgk3BGTzyusL/i+9
Ouf/wssz/s+9MOX/jHMT/k8+3f2/72T7/85jTf9bDtf9bzhY9b9mX9l/rx7X/yqJCkfgBuBz
NtDP/60bTP+7dtj/j1sY+d+z2+W/UpLieZIDUrdQw0IzU/W7SqLcTZI1wzSoJCnu0ArVIj8a
ybZ5cGsEANSBlPv0ZW/CAAAAAElFTkSuQmCC

</value>
</data>


How can I read this into a byte array? Do I just read the string
between the <value></value> tags into a string object and then loop
through this string a char at a time and manuary fill a byte array?
What is the correct way to achieve my desired goal?

Thanks,


Rich
 
P

Peter Duniho

Rich said:
[...]
How can I read the data like the following into a byte array and display
in a picture box? This data is a picture of a green forward arrow.

<data name="tsbNavForward.Image" type="System.Drawing.Bitmap,
System.Drawing"
mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>

iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1B
AACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAA
ARNJREFUOE9jYBgFJIWA/3TX3aH1oWwkaUJW7Nnv+N2pw/o2hgElxzP+g3Dh4dT/ufsS/2fs
iP2ftDHif8zK4P9hC33/+890/+850fF/0uKo/zFzQ/+blOtfRDEEpHndraX/V99Y9H/5tXn/
F1+Z9X/+pWn/Z52f/H/qmb7/E051/u8+3vq//Wjj/6ZDtf8DJnr918hUPgk3BGTzyusL/i+9
Ouf/wssz/s+9MOX/jHMT/k8+3f2/72T7/85jTf9bDtf9bzhY9b9mX9l/rx7X/yqJCkfgBuBz
NtDP/60bTP+7dtj/j1sY+d+z2+W/UpLieZIDUrdQw0IzU/W7SqLcTZI1wzSoJCnu0ArVIj8a
ybZ5cGsEANSBlPv0ZW/CAAAAAElFTkSuQmCC

</value>
</data>

As the "mimetype" attribute suggests, the data has been encoded using
Base64 encoding. So to decode it, you need to decode it as Base64. The
System.Convert class has methods to convert byte arrays to Base64
strings and back again.

Pete
 
R

Rich P

As the "mimetype" attribute suggests, the data has been encoded using
Base64 encoding. So to decode it, you need to decode it as Base64. The
System.Convert class has methods to convert byte arrays to Base64
strings and back again.

Pete
<<


Thank you for the reply.

If I say

string s1 =
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1
BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAA
AARNJREFUOE9jYBgFJIWA/3TX3aH1oWwkaUJW7Nnv+N2pw/o2hgElxzP+g3Dh4dT/ufsS/2f
siP2ftDHif8zK4P9hC33/+890/+850fF/0uKo/zFzQ/+blOtfRDEEpHndraX/V99Y9H/5tXn
/F1+Z9X/+pWn/Z52f/H/qmb7/E051/u8+3vq//Wjj/6ZDtf8DJnr918hUPgk3BGTzyusL/i+
9Ouf/wssz/s+9MOX/jHMT/k8+3f2/72T7/85jTf9bDtf9bzhY9b9mX9l/rx7X/yqJCkfgBuB
zNtDP/60bTP+7dtj/j1sY+d+z2+W/UpLieZIDUrdQw0IzU/W7SqLcTZI1wzSoJCnu0ArVIj8
aybZ5cGsEANSBlPv0ZW/CAAAAAElFTkSuQmCC"

how would I decode/convert this data to a byte array or to a form that
could be viewed in a picturebox?

I tried:
str1ng s1 = "..."
//byte[] b1 = System.Convert.ToBase64String( it wants a byte array here.
How do I proceed?

Thanks again for help.




Rich
 
K

kndg

[...]
//byte[] b1 = System.Convert.ToBase64String( it wants a byte array here.
How do I proceed?

Hi Rich,

I think you had already noticed by now, but just in case...

You're using the wrong method. System.Convert.ToBase64String() converts
the string to Base64. What you need here is the reverse:
System.Convert.FromBase64String().

ex:

pictureBox1.Image = new Bitmap(new
MemoryStream(Convert.FromBase64String(s1)));

But, when dealing with resource file, I prefer to use the build-in class
in the framework. You may check the System.Resources namespace for the
available tools.

ex:

private void LoadResourceImage()
{
Image image;

using (var resource = new ResXResourceSet("sample.resx"))
{
image = (Bitmap)resource.GetObject("tsbNavForward.Image");
}

pictureBox1.Image = image;
}

Regards.
 
P

Peter Duniho

Rich said:
[...]
I tried:
str1ng s1 = "..."
//byte[] b1 = System.Convert.ToBase64String( it wants a byte array here.
How do I proceed?

Read the docs. They do explain that that method is for _encoding_ a
Base64 string (as one might expect from a method that takes as its input
binary data). And the correct _decoding_ method is clearly listed in
the methods list for the Convert class.

The correct method returns a byte[], which you can use to create a
MemoryStream, and then use that MemoryStream in the Image.FromStream()
method to get an Image instance.

This all, of course, depends on the data actually being valid data for
some image format that .NET supports.

Pete
 
R

Rich P

Thank you all for your replies. I did try these two suggested methods
pictureBox1.Image = new Bitmap(new
MemoryStream(Convert.FromBase64String(s1)));

//---------------------------------

private void LoadResourceImage()
{
Image image;

using (var resource = new ResXResourceSet("sample.resx"))
{
image = (Bitmap)resource.GetObject("tsbNavForward.Image");
}

pictureBox1.Image = image;
}
<<

and they both worked as advertised. The whole purpose of this effort is
to get more involved with C# as this particular exercise is teaching me
about resources and how to deal with them (and also about the using
System.Resources directive) and more on using byte arrays with images.

But I would like to take this one step further: so now I have a byte
array image that I can see in the PictureBox. How can I save this down
to an image file like "test.gif" or "test.bmp", "test.jpg", "test.png",
"test.ico" ?

Thank you all again for the help.


Rich
 
P

Peter Duniho

Rich said:
[...]
But I would like to take this one step further: so now I have a byte
array image that I can see in the PictureBox. How can I save this down
to an image file like "test.gif" or "test.bmp", "test.jpg", "test.png",
"test.ico" ?

See System.Drawing.Image.Save().
 
R

Rich P

See System.Drawing.Image.Save().

Thank you for this suggestion. I will try this out as soon as I get my
VS2008 machine back from IT -- and report back the results.

Many thanks.

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top