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;
   }
相关推荐
xieliyu.4 小时前
Java算法精讲:双指针(二)
java·开发语言·算法
云水一下4 小时前
TypeScript 从零基础到精通(五):高级类型与泛型
前端·javascript·typescript
何以解忧,唯有..4 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
雪的季节5 小时前
RabbitMQ详解
开发语言
云水一下5 小时前
TypeScript 从零基础到精通(六):类型声明与模块化
javascript·typescript
ice8130331815 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
三品吉他手会点灯5 小时前
C语言学习笔记 - 44.运算符和表达式 - 运算符2 - 除法与取余运算符
c语言·开发语言·笔记·算法
kkeeper~5 小时前
0基础C语言积跬步之动态内存管理
c语言·开发语言
橘右今5 小时前
2026 Java后端高频面试宝典
java·开发语言·面试
xiaofeichaichai6 小时前
Map / Set / WeakMap / WeakSet
前端·javascript