当前位置: 首页 > news >正文

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

原文: WPF(C#)中Bitmap与BitmapImage相互转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangshubo1989/article/details/47296339

一、WPF的Image控件中设置ImageSource

image1.Source = new BitmapImage(new Uri(@"image file path", Urikind.RelativeOrAbsolute));

还可以使用:

System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length);
fs.Close(); fs.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit();
ms.Dispose();
image1.Source = bitmapImage;

还可以使用:

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.UriSource = new Uri(szPath);//szPath为图片的全路径
bitmapImage.EndInit();
bitmapImage.Freeze();
image1.Source = bitmapImage;

二、Bitmap转BitmapImage
先将Bitmap储存成memorystream,然后指定给BitmapImage

private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
BitmapImage bitmapImage = new BitmapImage();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bitmap.Save(ms, bitmap.RawFormat);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
}
return bitmapImage;
}

image1.Source = BitmapToBitmapImage(bitmap);

三、Bitmap转BitmapSource

/*-------------------------------------------------------------------------
//Imaging.CreateBitmapSourceFromHBitmap方法,基于所提供的非托管位图和调色板信息的指针,
//返回一个托管的BitmapSource
---------------------------------------------------------------------------*/
Bitmap bitmap = CaptureScreen.GetDesktopImage();
IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象

BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

image1.Source = bitmapSource;

四、BitmapSource转Bitmap

BitmapSource m = (BitmapSource)image1.Source;

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 

System.Drawing.Imaging.BitmapData data = bmp.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); 

m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data);

相关文章:

  • 产品三维模型在线预览
  • 文本导出的方方面面
  • springcloud超时时间与重试次数配置
  • 4S店使用OA系统实现集团化管控,让权力收放自如
  • 三星清洁机器人 欲破iRobot统治霸权
  • 「postgre」查看DB的物理占用空间
  • Unity3D客户端实时同步技术
  • 第十六章:数据绑定(八)
  • 目标检测框架网络模型分析(二 双塔奇兵)
  • sqlserver查看死锁进程工具脚本p_lockinfo
  • 每日一题--1
  • 开挂了!这5个Word技巧真的是超级实用,值得收藏!
  • 如何打印consul的错误信息
  • ifconfig 失效
  • 安装Cloudera manager agent步骤详解
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • CentOS 7 防火墙操作
  • Consul Config 使用Git做版本控制的实现
  • Docker入门(二) - Dockerfile
  • HTTP那些事
  • HTTP请求重发
  • java正则表式的使用
  • JDK9: 集成 Jshell 和 Maven 项目.
  • Leetcode 27 Remove Element
  • MQ框架的比较
  • sublime配置文件
  • Vue.js源码(2):初探List Rendering
  • vue的全局变量和全局拦截请求器
  • Vue官网教程学习过程中值得记录的一些事情
  • Web设计流程优化:网页效果图设计新思路
  • 分享一份非常强势的Android面试题
  • 关于Java中分层中遇到的一些问题
  • 力扣(LeetCode)357
  • 力扣(LeetCode)56
  • 判断客户端类型,Android,iOS,PC
  • 前端性能优化——回流与重绘
  • 学习Vue.js的五个小例子
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • raise 与 raise ... from 的区别
  • 带你开发类似Pokemon Go的AR游戏
  • (NSDate) 时间 (time )比较
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (vue)页面文件上传获取:action地址
  • (差分)胡桃爱原石
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (分布式缓存)Redis哨兵
  • (亲测)设​置​m​y​e​c​l​i​p​s​e​打​开​默​认​工​作​空​间...
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)memcache、redis缓存
  • (转载)OpenStack Hacker养成指南
  • .libPaths()设置包加载目录
  • .NET CORE 第一节 创建基本的 asp.net core
  • .NET 将多个程序集合并成单一程序集的 4+3 种方法
  • .net/c# memcached 获取所有缓存键(keys)