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;
   }
相关推荐
言兴几秒前
从输入 URL 到页面显示:深入理解浏览器缓存机制
前端·javascript·面试
讨厌吃蛋黄酥几秒前
前端跨域难题终结者:从JSONP到CORS,一文搞定所有跨域问题!
前端·javascript·后端
阿星做前端1 分钟前
coze源码解读:项目启动
前端·javascript
言兴2 分钟前
面试题之解析“类组件”与“组件”的本质
前端·javascript·面试
aidingni8889 分钟前
告别 DDL 难题:使用 Skapi 的零设置关系数据库 API
前端·javascript
草莓熊Lotso13 分钟前
【C++】--函数参数传递:传值与传引用的深度解析
c语言·开发语言·c++·其他·算法
东北南西13 分钟前
设计模式-单例模式
前端·javascript·单例模式·设计模式
Ice__Cai19 分钟前
Flask 路由详解:构建灵活的 URL 映射系统
开发语言·python·flask
托比-马奎尔30 分钟前
ES6变量与解构:let、const与模板字符串全解析
javascript
l1t1 小时前
分析xml标签属性和压缩级别对xlsx文件读取解析的影响
xml·开发语言·python·sql·duckdb