How to convert image to byte array

前端 未结 12 1594
悲哀的现实
悲哀的现实 2020-11-22 09:52

Can anybody suggest how I can convert an image to a byte array and vice versa?

I\'m developing a WPF application and using a stream reader.

12条回答
  •  执念已碎
    2020-11-22 10:29

    Do you only want the pixels or the whole image (including headers) as an byte array?

    For pixels: Use the CopyPixels method on Bitmap. Something like:

    var bitmap = new BitmapImage(uri);
    
    //Pixel array
    byte[] pixels = new byte[width * height * 4]; //account for stride if necessary and whether the image is 32 bit, 16 bit etc.
    
    bitmap.CopyPixels(..size, pixels, fullStride, 0); 
    

提交回复
热议问题