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,删除 ...
随机推荐
- 【CSS3】---结构性伪类选择器—nth-child(n)+nth-last-child(n)
结构性伪类选择器—nth-child(n) “:nth-child(n)”选择器用来定位某个父元素的一个或多个特定的子元素.其中“n”是其参数,而且可以是整数值(1,2,3,4),也可以是表达式(2n ...
- django 学习-12 Django表单 初步
1.先创建项目和应用 django.admin.py startproject cs cd cs django.admin.py startapp blog 2.vim setti ...
- SQL SERVER 2008 R2 错误代码 17000 - 17999
错误 严重性 是否记录事件 说明(消息正文) 17000 10 否 用法: sp_autostats <table_name> [, {ON|OFF} [, <index_name& ...
- 解决ashx文件下的Session“未将对象引用设置到对象的实例”
using System; using System.Collections.Generic; using System.Linq; using System.Web; using PPT_DAL; ...
- Java中List与Map初始化的一些写法
Java的在还没有发现新写法之前时,我一直是这么初始化List跟Map: 代码如下 复制代码 //初始化List List<string> list = new ArrayList ...
- Java实现邮箱找回密码 --转载
通过邮件找回密码功能的实现 1.最近开发一个系统,有个需求就是,忘记密码后通过邮箱找回.现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回.通过java发送邮件的 ...
- iOS自定义NavigationBar
日常开发中少不了用到UINavigationController,但是很多情况都要自定义NavigationBar.依稀记得自己刚开始也踩了好多坑,凑今天有空,就把想到的写下来.有时间了,考虑再把自定 ...
- OC10_代理反向传值
// // ProtectedDelegate.h // OC10_代理反向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...
- struts2指定集合元素的泛型
public class LoginAction implements Action{ private List users; public void setUsers(List users){ th ...
- Java序列化与反序列化(Serializable)
Java序列化与反序列化(Serializable) 特别注意: 1.要序列化的类必须实现Serializable借口 2.在反序列化(读取对象)的时候必须额外捕获EOFException 3.序列化 ...