首页 > 其他 > 详细

BitmapSource ConvertTo Bitmap

时间:2014-11-07 18:31:26      阅读:338      评论:0      收藏:0      [点我收藏+]

  偶遇需要把 BitmapSource 转成 Bitmap。 。。

 

bubuko.com,布布扣
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Jisons
{
    public static class BitmapSourceHelper
    {

        public static Bitmap ConvertToBitmap(this BitmapSource bs)
        {
            return ConvertToBitmap(bs, 0, 0, bs.PixelWidth, bs.PixelHeight);
        }

        public static Bitmap ConvertToBitmap(this BitmapSource bs, int x, int y, int width, int height)
        {
            var bmp = new Bitmap(width, height, PixelFormat.Format32bppPArgb);
            var bmpdata = bmp.LockBits(new Rectangle(System.Drawing.Point.Empty, bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb);
            bs.CopyPixels(new Int32Rect(x, y, width, height), bmpdata.Scan0, bmpdata.Height * bmpdata.Stride, bmpdata.Stride);
            bmp.UnlockBits(bmpdata);
            return bmp;
        }

        public static byte[] ConvertToBytes(this BitmapSource bs)
        {
            return ConvertToBytes(bs, 0, 0, (int)bs.Width, (int)bs.Height);
        }

        public static byte[] ConvertToBytes(this BitmapSource bs, int x, int y, int width, int height)
        {
            var rect = new Int32Rect(x, y, width, height);
            var stride = bs.Format.BitsPerPixel * rect.Width / 8;
            byte[] data = new byte[rect.Height * stride];
            bs.CopyPixels(rect, data, stride, 0);
            return data;
        }

        public static BitmapSource ClipBitmapSource(this BitmapSource bs, int x, int y, int width, int height)
        {
            var rect = new Int32Rect(x, y, width, height);
            var stride = bs.Format.BitsPerPixel * rect.Width / 8;
            byte[] data = new byte[rect.Height * stride];
            bs.CopyPixels(rect, data, stride, 0);
            return BitmapSource.Create(width, height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null, data, stride);
        }

        public static Bitmap ConvertToBitmap(this byte[] data, int width, int height)
        {
            var bmp = new Bitmap(width, height);
            for (int w = 0; w < width; w++)
            {
                for (int h = 0; h < height; h++)
                {
                    int index = h * width * 4 + w * 4;

                    int B = data[index];
                    int G = data[index + 1];
                    int R = data[index + 2];
                    int A = data[index + 3];

                    bmp.SetPixel(w, h, System.Drawing.Color.FromArgb(A, R, G, B));
                }
            }
            return bmp;
        }

        public static BitmapSource ConvertToBitmapSource(this Bitmap source)
        {
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }

    }
}
View Code

 

BitmapSource ConvertTo Bitmap

原文:http://www.cnblogs.com/jiailiuyan/p/4081697.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!