0

i have a trouble converting byte array to an image by common methods for example:

using (var ms = new MemoryStream(byteArrayIn))
{
    return Image.FromStream(ms); ->exception
}

and

System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
Image img = (Image)converter.ConvertFrom(ImgInBytes); -> exception 

The exception is Parameter is not valid

moreover, i used a 4 byte array length which was initiated by zero value.

it was supposed to show a black image but it didn't

14
  • you think you can make a image from 4 bytes? That would be just enough data for one pixel. But not actually wich color depth to use or any of the thousand other important inforamtions. You should propably not try to create a image from scratch. Create it from a existing soruce until you got the hang of the basics. Commented Nov 11, 2018 at 0:17
  • 1
    Show the byteArrayIn variable. If you just want to create an empty black image, what stops you from using a new Bitmap? Moreover, from where does your information come that it is supposed to be a black image? How would you decide on width & height of a picture? Commented Nov 11, 2018 at 0:18
  • @Christopher why there is not any limitation on byte[] size? Commented Nov 11, 2018 at 0:18
  • 1
    @Mr.AF that doesn't mean that any arbitrary content is valid; what format are you trying to represent (streams are broadly comparable to files: it isn't just raw bitmap data); the header for most image formats is more than 4 bytes Commented Nov 11, 2018 at 0:21
  • 4
    @Mr.AF it isn't just about size: the contents matter; the input needs to be a valid image format - 0 0 0 0 is not a valid image format. Most images consist of an identification token (png always starts 137 80 78 71 13 10 26 10, for example) - then format-specific encoded metadata - dimensions, color depth, possibly a color palette, maybe date/location/tool metadata; then finally some pixel data, which could use a range of encodings... and that's assuming it is pixed-based (vs vector-based) Commented Nov 11, 2018 at 0:29

1 Answer 1

2

i used a 4 byte array length which was initiated by zero value.

The API expects a valid image stream; 4 bytes with value zero is not a valid image stream. The method is going to inspect the stream, trying to identify the image format (streams are broadly comparable to files, except without any concept of a filename) - it isn't just looking for pixel data. That means it is going to be looking for an image header that it recognizes (for example, png always starts with the byte values 137 80 78 71 13 10 26 10); once it has identified the format, it'll want to decode the image header (dimensions, color depth, possibly a palette, etc), and then finally there might be some pixel data - or there might not, if it isn't a pixel format (it could be a vector image format). So; there's a lot more to consider than just some pixel data.

If you want a black image: perhaps start with Bitmap - maybe see this answer

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

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.