这对我来说很有效,它动态地设置了一个wpf图像,其中包含从动态生成或从磁盘加载的位图中加载的字节:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Drawing.Imaging;
namespace Examples
{
public class Util
{
private static void SetBitmap(Image imgDest, Bitmap bmpSource)
{
byte[] imageBytes;
using (MemoryStream stream = new MemoryStream())
{
bmpSource.Save(stream, ImageFormat.Png);
imageBytes = stream.ToArray();
}
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageBytes);
bitmapImage.EndInit();
imgDest.Source = bitmapImage;
}
}
}