none
Width and height of image from byte array C# RRS feed

  • Question

  • Hi,

    I have server-client application where server is converting image as byte array and sending it over TCP network. Once client receives byte array, it suppose to convert as Image and show in UI.

    How do I get width and height from the byte array to convert as a image in client side?

    Best regards

    SBB

    Thursday, October 24, 2019 7:51 PM

Answers

  • Best way to get width and height is convert bytes first to image and read from that. For example following code creates Stream from bytes and then Bitmap from the stream and get size.

    byte[] imageData = ReadImageData();
    MemoryStream imageStream = new MemoryStream(imageData);
    Bitmap image = new Bitmap(imageStream);
    int width = image.Width;
    int height = image.Height;

    Friday, October 25, 2019 4:16 AM
  • The answer to your question depends entirely on HOW you are converting the image to a byte array.  If you are just getting a stream of pixels, then there is no way to figure the size, much less the format.  You have to send the width and height as part of the data stream.

    There are several ways to do that.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, October 25, 2019 6:25 AM

All replies

  • Best way to get width and height is convert bytes first to image and read from that. For example following code creates Stream from bytes and then Bitmap from the stream and get size.

    byte[] imageData = ReadImageData();
    MemoryStream imageStream = new MemoryStream(imageData);
    Bitmap image = new Bitmap(imageStream);
    int width = image.Width;
    int height = image.Height;

    Friday, October 25, 2019 4:16 AM
  • Hi Suresh,

    Thank you for posting here.

    You could look at the following link to know the width and height of image from byte array.

    how to get the width and height from array of bytes of image by C#

    Hope this could help you.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 25, 2019 6:07 AM
    Moderator
  • The answer to your question depends entirely on HOW you are converting the image to a byte array.  If you are just getting a stream of pixels, then there is no way to figure the size, much less the format.  You have to send the width and height as part of the data stream.

    There are several ways to do that.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, October 25, 2019 6:25 AM
  • Hi,

    Finally we have handled to share width and height from the wrapper (c++) which receives data from network.

    Best regards

    Suresh

    Saturday, November 9, 2019 10:26 AM
  • Hi Suresh,

    It seems that your question has the correspond solution. Therefore, I suggest that you could post your answer to here so that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 12, 2019 2:30 AM
    Moderator