C#将Bitmap图像转换为BitmapImage

 public void InitImage(Bitmap bitmap)
 {
     try
     {
         // 将Bitmap转换为WPF的BitmapImage
         BitmapImage bitmapImage;
         using (MemoryStream memory = new MemoryStream())
         {
             bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
             memory.Position = 0;
             bitmapImage = new BitmapImage();
             bitmapImage.BeginInit();
             bitmapImage.StreamSource = memory;
             bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
             bitmapImage.EndInit();
         }
         Image_Img.Source = bitmapImage;
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
posted @ 2025-05-21 09:37  qiutian-hao  阅读(67)  评论(0)    收藏  举报