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,删除 ...
随机推荐
- 话说Centos下nginx,php,mysql以及phpmyadmin的配置
大话centos下部署phalcon框架 Centos还是ubuntu? 当我沿用这个标题的时候,心里在想"我能说我之前用的windows吗?",windows下xampp,wam ...
- MongoDB - The mongo Shell, Data Types in the mongo Shell
MongoDB BSON provides support for additional data types than JSON. Drivers provide native support fo ...
- MVC 使用Jquery实现AJax
View <script type="text/javascript"> function GetTime() { $.get("Home/GetTime&q ...
- jQuer中 height scrollTop
jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变时(如最 ...
- ASP.NET 窗体间传值实现方法详解
假设ParentForm.aspx 页面上有TextBox1文本框和Open按钮点击Open按钮弹出SubForm.aspx,SubForm.aspx页面上有TextBox1文本框和Close按钮点击 ...
- Hadoop集群“WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable”解决办法
Hadoop集群部署完成后,经常会提示 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platfo ...
- Webservice发布出现 测试窗体只能用于来自本地计算机的请求
今天发布了一个接口,一开始以为是.netframework版本的问题,从3.5降到2.0到服务器发布,发布后还是会出现 测试窗体只能用于来自本地计算机的请求 上网查找资料发现原来是 webconfig ...
- C语言获取系统时间的几种方式[转]
C语言获取系统时间的几种方式 C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * timer ) 精确到秒 2 使用clock_t clock() 得到的是CPU时间 ...
- Controller层的写法
项目中的两个Controller层实现类,一个是跳转到jsp页面,一个是以Json形式返回Map键值对. 跳转到jsp页面: package com.controller; import java.i ...
- HIV T2
甲学者将HIV病毒的遗传物质彻底水解后得到A.B.C三种化合物,乙学者将组成T2噬菌体的遗传物质彻底水解后得到了A.B.D三种化合物.你认为C.D两种化合物分别指的是 A.尿嘧啶.胸腺嘧啶 B.胸腺嘧 ...