示例#1
0
        public static BitmapSource BitmapSourceFromBitmap(Bitmap bitmap)
        {
            if (bitmap == null)
            {
                return(null);
            }
            using (var bitmapLocker = new BitmapLocker(bitmap))
            {
                int    num      = Math.Abs(bitmapLocker.BitmapData.Stride);
                int    length   = bitmap.Height * num;
                byte[] numArray = new byte[length];
                if (bitmapLocker.BitmapData.Stride > 0)
                {
                    Marshal.Copy(bitmapLocker.BitmapData.Scan0, numArray, 0, length);
                }
                else
                {
                    IntPtr scan0 = bitmapLocker.BitmapData.Scan0;
                    for (int index = 0; index < bitmap.Height; ++index)
                    {
                        Marshal.Copy(scan0, numArray, index * num, num);
                        scan0 -= num;
                    }
                }
                Int32Rect sectionRect = new Int32Rect(0, 0, bitmap.Width, bitmap.Height);
                switch (bitmap.PixelFormat)
                {
                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                    numArray = ExtractPixelSection(numArray, 24, num, sectionRect);
                    break;

                case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                    numArray = ExtractPixelSection(numArray, 32, num, sectionRect);
                    break;
                }
                BitmapSource bitmapSource = BitmapSource.Create(bitmap.Width, bitmap.Height, DpiHelper.Default.LogicalDpiX, DpiHelper.Default.LogicalDpiY, PixelFormats.Bgra32, null, numArray, num);
                bitmapSource.Freeze();
                return(bitmapSource);
            }
        }
示例#2
0
        public static Bitmap BitmapFromBitmapSource(BitmapSource bitmapSource)
        {
            if (bitmapSource == null)
            {
                return(null);
            }
            System.Drawing.Imaging.PixelFormat format;
            if (bitmapSource.Format == PixelFormats.Bgra32)
            {
                format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            }
            else if (bitmapSource.Format == PixelFormats.Bgr32)
            {
                format = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
            }
            else if (bitmapSource.Format == PixelFormats.Pbgra32)
            {
                format = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
            }
            else
            {
                System.Windows.Media.PixelFormat bgra32 = PixelFormats.Bgra32;
                return(BitmapFromBitmapSource(new FormatConvertedBitmap(bitmapSource, bgra32, null, 0.0)));
            }
            int    pixelWidth  = bitmapSource.PixelWidth;
            int    pixelHeight = bitmapSource.PixelHeight;
            int    stride      = pixelWidth * (bitmapSource.Format.BitsPerPixel / 8);
            Bitmap bitmap      = new Bitmap(pixelWidth, pixelHeight, format);

            using (BitmapLocker bitmapLocker = new BitmapLocker(bitmap, ImageLockMode.ReadWrite))
            {
                int[] source = new int[pixelWidth * pixelHeight];
                bitmapSource.CopyPixels(source, stride, 0);
                Marshal.Copy(source, 0, bitmapLocker.BitmapData.Scan0, source.Length);
            }
            return(bitmap);
        }