zoukankan      html  css  js  c++  java
  • BitmapSource ConvertTo Bitmap

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

    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
  • 相关阅读:
    Python 操作 MySQL 的5种方式
    ffiddler抓取手机(app)https包
    Git远程操作详解(clone、remote、fetch、pull、push)
    Mysql学生课程表SQL面试集合
    Python获取list中指定元素的索引
    python找出字典中value最大值的几种方法
    python sort、sorted高级排序技巧
    JMeter之BeanShell常用内置对象
    Docker从容器拷贝文件到宿主机或从宿主机拷贝文件到容器
    编程必备基础知识|计算机组成原理篇(10):输入输出设备
  • 原文地址:https://www.cnblogs.com/jiailiuyan/p/4081697.html
Copyright © 2011-2022 走看看