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;
   }
相关推荐
写bug如流水5 分钟前
【Python】使用 Pydantic + SQLAlchemy + MySQL 实现自动记录创建时间和更新时间
开发语言·python·mysql
与妖为邻1 小时前
房屋水电费记账本:内置的数组数据击按钮不能删除,页面手动添加的可以删除
前端·javascript·css·html·localstorage·房租水电费记账本
Watermelon Y1 小时前
【C++】多肽
开发语言·c++
郑州吴彦祖7721 小时前
java入门基础(一篇搞懂)
java·开发语言
菜鸡中的奋斗鸡→挣扎鸡1 小时前
初始爬虫11
开发语言·爬虫·python
miniwa1 小时前
JavaScript 中最快的循环是什么?
前端·javascript
碧海蓝天20221 小时前
C++ 线性表、内存操作、 迭代器,数据与算法分离。
开发语言·数据结构·c++·算法
计算机科研之友(Friend)1 小时前
物联网(一)——CMC特刊推荐
开发语言·人工智能·深度学习·物联网·计算机视觉·网络安全
PowerBI学谦1 小时前
Python in Excel 正式发布!
开发语言·python·excel
凡人的AI工具箱2 小时前
15分钟学 Python 第35天 :Python 爬虫入门(一)
开发语言·数据结构·人工智能·后端·爬虫·python