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
  • 相关阅读:
    Laravel schema构建器列类型
    wkhtmltopdf docker + java(环境搭建及一些坑)
    dockerfile,仓库,私有仓库流程(转载)
    wkhtmltopdf参数详解及精讲使用方法(转载)
    传统前端项目中进行模块化编程并引入使用vue、elementui 前端
    vue3 + vuex4 实践 前端
    elementplus 原生开发 日期国际化语言 前端
    Vite2.0打包elementplus UI报错 前端
    vue3 Vetur报错:has no default export 组件没导出 前端
    windows版的HbuilderX连接iPad真机测试(uniapp)
  • 原文地址:https://www.cnblogs.com/jiailiuyan/p/4081697.html
Copyright © 2011-2022 走看看