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;
   }
相关推荐
默_笙8 小时前
🍕 AI 的嘴巴装了水管(上):从"等它说完"到"边说边听"的流式输出指南
前端·javascript
SamChan908 小时前
PDF翻译后的格式完整性校验:用Python自动比对译文与原文档的表格与段落结构
开发语言·python·ai·pdf·机器翻译
暖焰核心8 小时前
继承全解——继承、默认成员函数、切片、隐藏与虚继承
java·前端·javascript
君顾18 小时前
外卖CPS系统开发实战指南:从架构设计到部署全流程解析
java·开发语言·外卖
艾伦野鸽ggg9 小时前
前端异步请求的状态竞争(请求竞态)问题
前端·javascript·axios
是立不是利9 小时前
前端交互基石:深入剖析JavaScript三级联动背后的设计哲学
开发语言·前端·javascript
老刘的望远镜9 小时前
AgentScope Java 从零(03):Agent 的记性默认全开,我在第 50 轮翻了车
java·开发语言·javascript
Littlehero_1219 小时前
QT自定义控件之电气接线图(源码开源)
开发语言·qt
中科三方9 小时前
DNS拨测到底在拨测什么?
开发语言·github·php·dns·云拨测