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
  • 相关阅读:
    Printing a DataGridView on DotNet Framework
    对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
    PetShop配置readme
    关于DGVPrinter.cs中的PrintRange
    foobar2000 0.9.5正式版放出
    Another DataGridView Printer
    getline及读文件总结
    C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据
    vector的clear和swap
    写程序前不需指定数组中的常量,而是动态编译中决定
  • 原文地址:https://www.cnblogs.com/jiailiuyan/p/4081697.html
Copyright © 2011-2022 走看看