偶遇需要把 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. jquery简单的大背景banner图片全屏切换

    详细内容请点击 这个是我初毕业刚进公司那会帮同事(同时也是同学)写的一个PC端的全屏图片切换效果,对于刚毕业的我来说写出来那会的喜悦之情是无法言表的,那时的我还是什么不懂的小白白,俗称菜鸟.个人网站上 ...

  2. SQL Server高级内容之case语法函数

    1.Case函数的用法  (1)使用类似:switch-case与if-else if. (2)语法: case [字段] when 表达式 then 显示数据 when 表达式 then 显示数据 ...

  3. jQuery Ajax应用

    jQuery Ajax应用 本章主要了解jQuery的Ajax与传统的Ajax的区别,掌握JQuery的Ajax常用的方法与Ajax相关的函数. 详细内容,请点击jQuery Ajax应用查看:

  4. LAMP+Proftpd+数据迁移

    on Centos 6.5 64bit minimal 安装mysql [root@ftp ~]# yum install -y mysql mysql-server mysql-devel  --安 ...

  5. MySQL中DATE_FORMATE函数内置字符集解析

    今天帮同事处理一个SQL(简化过后的)执行报错: 代码如下 复制代码 mysql> select date_format('2013-11-19','Y-m-d') > timediff( ...

  6. cocos2d-x中Node中重要的属性

    Node还有两个非常重要的属性:position和anchorPoint. position(位置)属性是Node对象的实际位置.position属性往往还要配合使用anchorPoint属性,为了将 ...

  7. 关于IIS中WEB网站访问弹“验证输入框”及“401限制访问”的解决办法

    最近在配置IIS网站的过程中,不管是本地还是外部访问配置的网站,出现了需要输入用户名.密码.及域的对话框,解决之后又出现页面401限制访问的错误页面. 就这两项错误,依次做一下解决办法的整理. 解决流 ...

  8. (转)Yale CAS + .net Client 实现 SSO(6)

    第一部分:安装配置 Tomcat 第二部分:安装配置 CAS 第三部分:实现 ASP.NET WebForm Client 第四部分:实现基于数据库的身份验证 第五部分:扩展基于数据库的身份验证 第六 ...

  9. 7款超酷HTML5 3D动画精选应用及源码

    对以前来讲,3D动画拿到网页上展示是一件非常奢侈的事情,第一是浏览器不够先进,第二是大部分只能用flash实现伪3D.HTML5的出现,让实现网页3D动画变得非常简单,当然前提是你不要再使用像IE67 ...

  10. SSH 正向/反向代理小记

    上周因为玩耍Minecraft的原因,折腾了下ssh的正向.反向代理,不得不说,科技改变命运..了解了基础的用法之后,很多跨域的事情都可以通过代理解决,而且只需要ssh帐号权限即可. 那么就简单来介绍 ...