2012-01-17 70 views
14

我需要帮助,我有这样的方法从一个byte []如何转换字节[]到BitmapImage的

public BitmapSource ByteToBitmapSource(byte[] image) 
{ 
    BitmapImage imageSource = new BitmapImage(); 

    using (MemoryStream stream = new MemoryStream(image)) 
    { 
     stream.Seek(0, SeekOrigin.Begin); 
     imageSource.BeginInit(); 
     imageSource.StreamSource = stream; 
     imageSource.CacheOption = BitmapCacheOption.OnLoad; 
     imageSource.EndInit(); 
    } 

    return imageSource; 
} 

imageSource.EndInit();抛出一个错误得到的BitmapImage“我们找到了适合完成这个操作没有成像组件“。

+2

格式是什么你想图像加载?你确定它支持吗? – Shimmy 2012-01-17 15:27:56

回答

0

您应该为我们提供更多关于您的图片的信息。
我可以认为它不受系统支持,我会建议您使用外部工具,例如imageMagik或任何其他专用于图像的转换器。

0

我做了类似的东西,但它不是与BitmapImage的,希望它可以帮助...

首先,我从路径获取图像,所以我得到一个BMP,铸成一个byte [] :

private byte[] LoadImage(string szPathname) 
    { 
    try 
    { 
     Bitmap image = new Bitmap(szPathname, true); 

     MemoryStream ms = new MemoryStream(); 
     image.Save(ms, ImageFormat.Bmp); 
     return ms.ToArray(); 
    }catch (Exception){...} 

    return null; 
    } 

我把这个在我的代码是这样的:

byte[] bitmapData1 = LoadImage(@"C:\Users\toto\Desktop\test1.bmp"); 

而当我想以字节[]浇铸成System.Windows.Controls.Image我这样做:

protected virtual void OnByteArrayChanged(DependencyPropertyChangedEventArgs e) 
    { 
    try 
    { 
     // PropertyChanged method 
     BitmapImage bmpi = new BitmapImage(); 
     bmpi.BeginInit(); 
     bmpi.StreamSource = new MemoryStream(ByteArray); 
     bmpi.EndInit(); 

     System.Windows.Controls.Image image1 = (get my image in my xaml); 
     image1.Source = bmpi; 
    }catch (Exception){...} 
    } 
24

Image.Source设置为XAML中的字节数组属性。

<Image x:Name="MyImage" Source="{Binding Path=MyByteArrayProperty}" /> 

如果你真的你可以在代码中做背后这样

public void DecodePhoto(byte[] byteVal) 
     { 
      if (byteVal == null) 
{ 
return ; 
} 


      try 
      { 
       MemoryStream strmImg = new MemoryStream(byteVal); 
       BitmapImage myBitmapImage = new BitmapImage(); 
       myBitmapImage.BeginInit(); 
       myBitmapImage.StreamSource = strmImg; 
       myBitmapImage.DecodePixelWidth = 200; 
       myBitmapImage.EndInit(); 
       MyImage.Source = myBitmapImage; 
      } 
      catch (Exception) 
      { 
      } 
     } 
+0

当ImageSource类型为ImageSource时,如何将Image.Source设置为byte []? – TimothyP 2012-11-26 09:56:29

+3

@TimothyP我的意思是在XAML中。 0x4f3759df 2012-11-26 14:59:32

0

这也可能有助于:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
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.Drawing; 
using System.Runtime.InteropServices; 
using System.IO; 
using System.ComponentModel; 


public class MakeBitmapSource 
{ 
    [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")] 
    public static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length); 



    public static BitmapSource FromNativePointer(IntPtr pData, int w, int h, int ch) 
    { 
     PixelFormat format = PixelFormats.Default; 

     if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255 
     if (ch == 3) format = PixelFormats.Bgr24; //RGB 
     if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha 


     WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null); 
     CopyMemory(wbm.BackBuffer, pData, (uint)(w * h * ch)); 

     wbm.Lock(); 
     wbm.AddDirtyRect(new Int32Rect(0, 0, wbm.PixelWidth, wbm.PixelHeight)); 
     wbm.Unlock(); 

     return wbm; 
    } 

    public static BitmapSource FromArray(byte[] data, int w, int h, int ch) 
    { 
     PixelFormat format = PixelFormats.Default; 

     if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255 
     if (ch == 3) format = PixelFormats.Bgr24; //RGB 
     if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha 


     WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null); 
     wbm.WritePixels(new Int32Rect(0, 0, w, h), data, ch * w, 0); 

     return wbm; 
    } 
}