C# - Convert WPF Image.source to a System.Drawing.Bitmap

前端 未结 4 1954
醉梦人生
醉梦人生 2020-11-29 09:35

I\'ve found loads of people converting a BitmapSource to a Bitmap, but what about ImageSource to Bitmap? I am making an i

4条回答
  •  青春惊慌失措
    2020-11-29 09:56

    That example worked for me:

        public static Bitmap ConvertToBitmap(BitmapSource bitmapSource)
        {
            var width = bitmapSource.PixelWidth;
            var height = bitmapSource.PixelHeight;
            var stride = width * ((bitmapSource.Format.BitsPerPixel + 7) / 8);
            var memoryBlockPointer = Marshal.AllocHGlobal(height * stride);
            bitmapSource.CopyPixels(new Int32Rect(0, 0, width, height), memoryBlockPointer, height * stride, stride);
            var bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppPArgb, memoryBlockPointer);
            return bitmap;
        }
    

提交回复
热议问题