8,755
社区成员




public ImageWindow(byte[] imageByte)
{
InitializeComponent();
MemoryStream ms = new MemoryStream(imageByte);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
image.Source = bitmapImage;
}
public partial class ImageWindow : Window
{
byte[] iByte;
public ImageWindow(byte[] imageByte)
{
InitializeComponent();
iByte = imageByte;
}
private void image_Loaded(object sender, RoutedEventArgs e)
{
MemoryStream ms = new MemoryStream(iByte);
//ms.Seek(0, SeekOrigin.Begin);
//BitmapImage bitmapImage = new BitmapImage();
//bitmapImage.BeginInit();
//bitmapImage.StreamSource = new MemoryStream()
//bitmapImage.EndInit();
//image.Source = bitmapImage;
Bitmap bm = (Bitmap)System.Drawing.Image.FromStream(ms);
ms.Close();
BitmapImage bitmapImage = new BitmapImage();
bitmapImage = BitmapToBitmapImage(bm);
image.Source = bitmapImage;
}
public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height);
int i, j;
for (i = 0; i < bitmap.Width; i++)
for (j = 0; j < bitmap.Height; j++)
{
System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
bitmapSource.SetPixel(i, j, newColor);
}
MemoryStream ms = new MemoryStream();
bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
bitmapImage.EndInit();
return bitmapImage;
}
}
还是出错,byte[]装bitmap那儿,参数无效。。。
public static Bitmap ConvertByteToImg(byte[] bytes)
{
Bitmap img = null;
try
{
if (bytes != null && bytes.Length != 0)
{
MemoryStream ms = new MemoryStream(bytes);
img = new Bitmap(ms);
}
}
catch (Exception ex)
{
LogerHelper.WriteException("Unity:55" + ex.Message);
}
return img;
}
/// <summary>
/// bitmap转成BitmapImage
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
{
BitmapImage bitmapImage = new BitmapImage();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap.Save(ms, bitmap.RawFormat);
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}