Byte array to image exception









up vote
0
down vote

favorite












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










share|improve this question





















  • 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.
    – Christopher
    Nov 11 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?
    – Icepickle
    Nov 11 at 0:18











  • @Christopher why there is not any limitation on byte size?
    – Mr.AF
    Nov 11 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
    – Marc Gravell
    Nov 11 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)
    – Marc Gravell
    Nov 11 at 0:29















up vote
0
down vote

favorite












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










share|improve this question





















  • 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.
    – Christopher
    Nov 11 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?
    – Icepickle
    Nov 11 at 0:18











  • @Christopher why there is not any limitation on byte size?
    – Mr.AF
    Nov 11 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
    – Marc Gravell
    Nov 11 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)
    – Marc Gravell
    Nov 11 at 0:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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










share|improve this question













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







c# image






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 0:14









Mr.AF

631816




631816











  • 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.
    – Christopher
    Nov 11 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?
    – Icepickle
    Nov 11 at 0:18











  • @Christopher why there is not any limitation on byte size?
    – Mr.AF
    Nov 11 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
    – Marc Gravell
    Nov 11 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)
    – Marc Gravell
    Nov 11 at 0:29

















  • 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.
    – Christopher
    Nov 11 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?
    – Icepickle
    Nov 11 at 0:18











  • @Christopher why there is not any limitation on byte size?
    – Mr.AF
    Nov 11 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
    – Marc Gravell
    Nov 11 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)
    – Marc Gravell
    Nov 11 at 0:29
















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.
– Christopher
Nov 11 at 0:17





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.
– Christopher
Nov 11 at 0:17





1




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?
– Icepickle
Nov 11 at 0:18





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?
– Icepickle
Nov 11 at 0:18













@Christopher why there is not any limitation on byte size?
– Mr.AF
Nov 11 at 0:18




@Christopher why there is not any limitation on byte size?
– Mr.AF
Nov 11 at 0:18




1




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
– Marc Gravell
Nov 11 at 0:21





@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
– Marc Gravell
Nov 11 at 0:21





4




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)
– Marc Gravell
Nov 11 at 0:29





@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)
– Marc Gravell
Nov 11 at 0:29













1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted











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






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244685%2fbyte-array-to-image-exception%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted











    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






    share|improve this answer
























      up vote
      2
      down vote



      accepted











      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






      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 0:39









        Marc Gravell

        775k19021242538




        775k19021242538



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244685%2fbyte-array-to-image-exception%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20