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, , , 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的更多相关文章
- WPF GDI+ bitmap.save 一般性错误
做水印图片的时候,发现WPF的System.Windows.Shapes类有绘制直线,椭圆等形状.却没有绘字符串的类. 无奈之下又用回GDI+ 发生的GDI+一般性错误初步估计的线程的原因. 在loa ...
- C#中Image , Bitmap 和 BitmapData
先说Image,Image 就是个图像,不能实例化,提供了位图和源文件操作的函数.本篇文章他就是来打酱油的,这里提供一个Bitmap转成BitmapSource的方法. [DllImport(&quo ...
- Bitmap 与ImageSource之间的转换
public class ImageConverter { [DllImport("gdi32.dll", SetLastError = true)] private static ...
- C# Bitmap转化为BitmapImage方法
public BitmapImage BitmapToBitmapImage(Bitmap bitmap) { Bitmap bitmapSource = new Bitmap(bitmap.Widt ...
- WPF(C#)中Bitmap与BitmapImage相互转换
原文:WPF(C#)中Bitmap与BitmapImage相互转换 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wangshubo1989/art ...
- 【WPF】wpf image控件加载网络图片不显示问题,
1.加载网络图片到内存system.drawing.image对象中2.内存中的image 转Bitmap 再转适合system.windows.controls.image 的BitmapImage ...
- WPF 如何画一颗心
如何用WPF画一个心. MainWindow.xaml <Window x:Class="Heart.MainWindow" xmlns="http://schem ...
- WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要针对WPF项目 ...
- WPF 引用DLL纯图像资源包类库中的图片
原文:WPF 引用DLL纯图像资源包类库中的图片 1.建立WPF应用程序 过程略. 2.创建类库项目(图片资源包) 创建图片资源类库项目MyImages,删除 ...
随机推荐
- ASP.NET 状态的传递和保存
1,HTTP协议是无状态的.服务器不会记住上次给浏览器的处理结果,如果需要上次处理结果(上次状态)就需要浏览器把处理结果值(上次状态)再次给服务器. 2,URL传值:通过URL参数或者通过Form表单 ...
- 小菜鸟带着梦想学chromium
风雨送春归, 飞雪迎春到. 已是悬崖百丈冰, 犹有花枝俏. 俏也不争春, 只把春来报. 待到山花烂漫时, 她在丛中笑. 这首卜算子·咏梅可是应了我的心情了.最近换工作,受到频频打击,面试过程中发现满世 ...
- jQuery 判断是否为数字的方法 及 转换数字函数
<script language="javascript"> var t=$("#id").val();//这个就是我们要判断的值了 if(!isN ...
- [老老实实学WCF] 第五篇 再探通信--ClientBase
老老实实学WCF 第五篇 再探通信--ClientBase 在上一篇中,我们抛开了服务引用和元数据交换,在客户端中手动添加了元数据代码,并利用通道工厂ChannelFactory<>类创 ...
- eclipse安装tomcate插件步骤
今天在网上看了很多关于eclipse需要安装tomcat的文章,小编也没有指责的意思,只是想说为什么有那么多雷同且不正确的搭建过程,真是ctrl+c, ctrl+v玩多了吧,不知道流程有的是错误的,不 ...
- 分享8款最新HTML5/CSS3功能插件及源码下载
1.HTML5/CSS3鬼脸表情下拉菜单 超级可爱 这款HTML5/CSS3鬼脸表情下拉菜单真的很特别,虽然菜单的实现并没有利用复杂的HTML5/CSS3技术,但是创意的确不错. 在线演示 源码下载 ...
- Geodatabase介绍
一.概述 (1)Geodatabase是什么? ArcGIS操作基于GIS文件格式和存储于地理数据库(Geodatabase)中的地理信息.Geodatabase是ArcGIS的本地数据结构,是用于编 ...
- 济南学习 Day1 T3 am
凝视[问题描述]背包是个好东西,希望我也有.给你一个二维的背包,它的体积是N*M.现在你有一些大小为1× 2和1×3的物品,每个物品有自己的价值.你希望往背包里面装一些物品,使得它们的价值和最大,问最 ...
- 转帖:使用TortoiseGit处理代码冲突
原址:http://www.cnblogs.com/jason-beijing/p/5718190.html 场景一 user0 有新提交 user1 没有pull -> 写新代码 -&g ...
- 判断不在Update Task中
CALL FUNCTION 'TH_IN_UPDATE_TASK' IMPORTING IN_UPDATE_TASK = IN_UPDATE_TASK. "0 then not ...