WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Wpf ImageSource对象与Bitmap对象的互相转换

Posted on 2023-12-21 15:44  WebEnh  阅读(164)  评论(0编辑  收藏  举报
    1. Bitmap to ImageSource
      将得到的Bitmap对象转换为wpf常用的Imagesource对象
BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

得到的BitmapSource是Imagesource的子类

  1. ImageSource to Bitmap
首先得到ImageSource对象_imagesource

System.IO.MemoryStream ms = new System.IO.MemoryStream();
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)_imagesource));
encoder.Save(ms);

Bitmap bp = new Bitmap(ms);
ms.Close();