BitmapSource

Posted redsky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BitmapSource相关的知识,希望对你有一定的参考价值。

来自:https://stackoverflow.com/questions/8311805/bitmapsource-copypixels-byte-bitmapsource-how-to-do-this-simple

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

            bitmapSource.CopyPixels(pixels, stride, 0);

            return pixels;
        }

        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 + 7) / 8), 0);

            return bitmap;
        }

 

以上是关于BitmapSource的主要内容,如果未能解决你的问题,请参考以下文章