偶遇需要把 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, , , 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, , , (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 / ;
byte[] data = new byte[rect.Height * stride];
bs.CopyPixels(rect, data, stride, );
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 / ;
byte[] data = new byte[rect.Height * stride];
bs.CopyPixels(rect, data, stride, );
return BitmapSource.Create(width, height, , , 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 = ; w < width; w++)
{
for (int h = ; h < height; h++)
{
int index = h * width * + w * ; int B = data[index];
int G = data[index + ];
int R = data[index + ];
int A = data[index + ]; 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());
} }
}

BitmapSource ConvertTo Bitmap的更多相关文章

  1. WPF GDI+ bitmap.save 一般性错误

    做水印图片的时候,发现WPF的System.Windows.Shapes类有绘制直线,椭圆等形状.却没有绘字符串的类. 无奈之下又用回GDI+ 发生的GDI+一般性错误初步估计的线程的原因. 在loa ...

  2. C#中Image , Bitmap 和 BitmapData

    先说Image,Image 就是个图像,不能实例化,提供了位图和源文件操作的函数.本篇文章他就是来打酱油的,这里提供一个Bitmap转成BitmapSource的方法. [DllImport(&quo ...

  3. Bitmap 与ImageSource之间的转换

    public class ImageConverter { [DllImport("gdi32.dll", SetLastError = true)] private static ...

  4. C# Bitmap转化为BitmapImage方法

    public BitmapImage BitmapToBitmapImage(Bitmap bitmap) { Bitmap bitmapSource = new Bitmap(bitmap.Widt ...

  5. WPF(C#)中Bitmap与BitmapImage相互转换

    原文:WPF(C#)中Bitmap与BitmapImage相互转换 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wangshubo1989/art ...

  6. 【WPF】wpf image控件加载网络图片不显示问题,

    1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...

  7. WPF 如何画一颗心

    如何用WPF画一个心. MainWindow.xaml <Window x:Class="Heart.MainWindow" xmlns="http://schem ...

  8. WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要针对WPF项目 ...

  9. WPF 引用DLL纯图像资源包类库中的图片

    原文:WPF 引用DLL纯图像资源包类库中的图片 1.建立WPF应用程序              过程略.   2.创建类库项目(图片资源包)       创建图片资源类库项目MyImages,删除 ...

随机推荐

  1. 十四、Android学习笔记_Android回调函数触发的几种方式 广播 静态对象

    一.通过广播方式: 1.比如登录.假如下面这个方法是外界调用的,那么怎样在LoginActivity里面执行登录操作,成功之后在回调listener接口呢?如果是平常的类,可以通过构造函数将监听类对象 ...

  2. DWZ (JUI) 教程 table 排序

    dwz排序是后台排序,不是前台的js排序,他的流程和搜索,分页是一样的,当你点击排序的按钮时,从新发送请求刷新当前的navTable 和 dialog. <th width="60&q ...

  3. 本地wamp的Internal Server Error错误解决方法

    一.本地wamp下调试url重写,加入htaccess文件后提示:500 Internal Server Error...,而删除这个文件网站又可以正常访问,其实就是没有开启url重写的功能.开启一下 ...

  4. VLAN系列

    Write From Yangwj Sunday, March 9, 2014 一. Vlan的识别 1. 交换机端口是访问端口,它就属于某一个Vlan:如果是中继端口,它就可以属于所有Vlan. 2 ...

  5. @@ROWCOUNT (Transact-SQL)

    @@ROWCOUNT (Transact-SQL) 返回受上一语句影响的行数.如果行数大于 20 亿,请使用 ROWCOUNT_BIG. Transact-SQL 语法约定 语法 @@ROWCOUNT ...

  6. UINavigationController与UITabbarController的样式

    之前虽然也手写过这两中视图控制器,但是更多的还是使用SB来创建,最近发现了一些问题,现在总结一下. 1.改变UINavigationBar的颜色 在UINavigationController中,之前 ...

  7. 将DataTable格式化为json字符串返回

    一般用于ajax局部刷新的使用比较多,通过查询得到了DataTable数据,要想将数据放回需要将DataTable转换为json格式,以下为转换的调用函数: string json = "& ...

  8. format——MATLAB

    format:设置输出格式 对浮点性变量,缺省为format short. format并不影响matlab如何计算和存储变量的值.对浮点型变量的计算,即单精度或双精度,按合适的浮点精度进行,而不论变 ...

  9. [Silverlight] Visual Studio2010不能安装Silverlight4_Tools,提示语言不一致

    今天在装Silverlight4_Tools时出现“必须先安装与 Silverlight Tools 4 语言版本相一致的 Visual Studio 2010.Visual Web Develope ...

  10. hidden(隐藏域)

    <input type="hidden">? 这等于是隐藏值,客户端无需显示,但提交后后台能够接受,例如你放个表单验证值在里边,这样可以拒绝来源不明的表单提交.还有判断 ...