views:

313

answers:

3

Hi folks,

I'm trying to read in a hardcoded bitmap image into a bitmap object. I keep getting the same error:

System.ArgumentException: Parameter is not valid.

This is the code i have...

const string fakeByteData = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";

Stream stream = new MemoryStream(Encoding.ASCII.GetBytes(fakeByteData));
var bitmap = new Bitmap(stream);

Yes, i've not used any 'using' statements of disposed off stuff, because I was trying to see why this piece of byte data was erroring. This is some random code i have in a very temporary unit test.

I'm assuming the content of the fakeByteData is legit.

Can anyone shed some light, here?

+3  A: 

You sure fakeByteData isn't Base64-encoded? That's usually the way to store image(binary) data as a string...

The "==" at the end of your string looks like the padding characters used in Base64 encoding as well.

What happens when you try:

Stream stream = new MemoryStream(Convert.FromBase64String(fakeByteData));


Update: I tried decoding fakeByteData using an online decoder/encoder and could not load the image. When viewing the contents, it looks like very fake data:

� !"#$%&'()+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

This certainly shouldn't produce any kind of image I know; it appears to be a sequential array of bytes 00-FF. So I think your problem is in the data.

lc
exactly the same error.
Pure.Krome
What line are you getting the error on?
lc
var bitmap = new Bitmap(stream) line. fails there. both mine AND yours.
Pure.Krome
@lc: It's not just "very fake data" - it's very predictable data: 256 bytes, 00-FF.
Jon Skeet
A: 

Is the bitmap data an actual bitmap? To create a bitmap from a stream the stream must contain the data that is normally contained in a bitmap file. This includes headers and that stuff.

If the byte data is meant to give the actual image and not the entire file you should first create a new bitmap with the size you want and the image format you want. Then you should lock the bitmap using BitMap.LockBits and use Marshal.Copy to copy the byte data into the bitmap data. Alternatively you could use Bitmap.SetPixel and traverse the byte array manually, but that will be much slower.

Rune Grimstad
+3  A: 

That's a base64 string, but it doesn't have an image in it.

If you dump the data to disk after base64 decoding it, you'll find it's just bytes 0x00, 0x01, 0x02 etc right up to 0xff.

EDIT: To address your comment in the question: this is "bad" data in that it doesn't represent an image. You may want to have a unit test for such a case, but you can't write a unit test which expects a real image based on data which doesn't represent a valid image.

How will you be getting your image data in real life? Will it be as a base-64 encoded string? If so, here's some sample valid data for a red spot, taken from the Wikipedia entry for the data URI scheme:

string fakeByteData = 
    "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP"
    + "C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA"
    + "AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J"
    + "REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq"
    + "ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0"
    + "vr4MkhoXe0rZigAAAABJRU5ErkJggg==";
Jon Skeet
so, the error i was getting was basically saying "the base64 data doesn't have an _image_ in it, so boom! error"?
Pure.Krome
Yes. You've asked it to load an image from that data (as a byte array by that point - it doesn't know it came from base64) and the data doesn't contain an image, hence the exception.
Jon Skeet
Thanks heaps Jon. I've learnt heaps from stalking your answers (all in a good way, mind you). You are a credit to the entire prgramming community. /me bows low. thanks again!
Pure.Krome
No problem - glad it helped :)
Jon Skeet