BitmapSource.CopyPixels-> byte [] & # 8594; BitmapSource, how to do it simply?

How to make efficient BitmapSource for byte [] and vice versa in C #?

+4
source share
1 answer

BitmapSource to byte []:

private byte[] BitmapSourceToArray(BitmapSource bitmapSource) { // Stride = (width) x (bytes per pixel) int stride = (int)bitmapSource.PixelWidth * (bitmapSource.Format.BitsPerPixel / 8); byte[] pixels = new byte[(int)bitmapSource.PixelHeight * stride]; bitmapSource.CopyPixels(pixels, stride, 0); return pixels; } 

byte [] in BitmapSource:

 private BitmapSource BitmapSourceFromArray(byte[] pixels, int width, int height) { WriteableBitmap bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null); bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * (bitmap.Format.BitsPerPixel / 8), 0); return bitmap; } 
+8
source

Source: https://habr.com/ru/post/1383644/


All Articles