WPF(C#)中Bitmap与BitmapImage相互转换
一、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);

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
C#模拟PrtScn实现截屏
原文: C#模拟PrtScn实现截屏 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangshubo1989/article/details/47808101 有了之前的基础知识了解,现在开始实现PrtScn和Alt+PrtScn。 首先新建一个WPF应用程序,命名为PrintscreenAndAltPrintScreen 导入keybd_event方法: 需要为DllImport添加using System.Runtime.InteropServices; [DllImport("user32.dll")] static extern void keybd_event ( byte bVk,// 虚拟键值 byte bScan,// 硬件扫描码 uint dwFlags,// 动作标识 IntPtr dwExtraInfo// 与键盘动作关联的辅加信息 ); 编写PrtScn函数: public void PrintScreen() { keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//...
- 下一篇
vim python一键执行、高亮等一键安装
wget -qO- https://raw.github.com/ma6174/vim/master/setup.sh | sh -x
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7安装Docker,走上虚拟化容器引擎之路
- MySQL8.0.19开启GTID主从同步CentOS8
- Red5直播服务器,属于Java语言的直播服务器
- Mario游戏-低调大师作品
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题