Wpf Image 展示方式 图片处理 显示

Wpf Image 展示方式 图片处理 显示

1、创建Bitmap

bash 复制代码
   public Bitmap CreateBitmap(int width,int height,int step,IntPtr pdata)
   {
       return new Bitmap(
           width,
           height,
           step,
           System.Drawing.Imaging.PixelFormat.Format8bppIndexed,
           pdata);
   }

2、Bitmap转BitmapSource显示

csharp 复制代码
   public void ShowImg(System.Drawing.Bitmap bitmap)
   {
       IntPtr hBitmap = bitmap.GetHbitmap();
       Img = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
   }

3、Bitmap转BitmapImage显示

csharp 复制代码
   public void ShowImgEx(System.Drawing.Bitmap bitmap)
   {
       MemoryStream ms = new MemoryStream();
       bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
       byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以
       ms.Close();
       BitmapImage image = new BitmapImage();
       image.BeginInit();
       image.StreamSource = new MemoryStream(bytes);
       image.EndInit();
       Img = image; 
       bitmap.Save("111.bmp",ImageFormat.Bmp);
   }

4、WriteableBitmap 显示

csharp 复制代码
   public void ShowImgEx2(int width, int height, IntPtr pdata)
   {
       var (pixel, palette) = GetPixelFormat(1);
       int bufferLen = width * height;
       BitmapSource? bitmapSource = 
           WriteableBitmap.Create(width, height
           , 96, 96, pixel, palette, pdata, bufferLen, width);
       Img = bitmapSource;
   }
相关推荐
WYH2874 分钟前
为什么在cubeide里勾选了can1,生成的工程里没有can.c?
c语言·开发语言
7 分钟前
java关于键盘录入
java·开发语言
马猴烧酒.9 分钟前
JAVA后端对象存储( 图片分享平台)详解
java·开发语言·spring·腾讯云
天才奇男子17 分钟前
LVS原理及部署
linux·运维·云原生·wpf·lvs·linux chrony
wearegogog12318 分钟前
基于MATLAB的D2D仿真场景实现
开发语言·网络·matlab
froginwe1121 分钟前
Chart.js 散点图详解
开发语言
独自破碎E23 分钟前
【纵向扫描】最长公共前缀
java·开发语言
2601_9495758627 分钟前
Flutter for OpenHarmony二手物品置换App实战 - 自定义组件实现
android·javascript·flutter
nuo53420229 分钟前
C语言实现类似面向对象的三大特性
c语言·开发语言
object not found29 分钟前
基于uniapp开发小程序自定义顶部导航栏状态栏标题栏
前端·javascript·小程序·uni-app