2016-03-04 173 views
2

我很新来显示WPF窗体中的图像,并且我在为GUI转换和分配图像源时遇到了麻烦。WPF C#图像源

 System.Drawing.Image testImg = ImageServer.DownloadCharacterImage(charID, ImageServer.ImageSize.Size128px); 
     byte[] barr = imgToByteArray(testImg); 
     CharImage.Source = ByteToImage(barr);   

    public byte[] imgToByteArray(System.Drawing.Image testImg) 
    { 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      testImg.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); 
      return ms.ToArray(); 
     } 
    } 

    public System.Drawing.Image ByteToImage(byte[] barr) 
    { 
     MemoryStream ms = new MemoryStream(barr); 
     System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); 
     return returnImage; 
    } 

所以我把从在线前夕C#API库的图像(JPEG)中,然后尝试将其转换为一个字节数组,然后返回到正确的图像。然而,我总是得到这个错误:“不能隐式转换类型'System.Drawing.Image'到'System.Windows.Media.ImageSource'”我完全傻眼如何解决这个问题。

回答

2

System.Drawing.Image是WinForms,而不是WPF。您的ByteToImage方法应改为返回BitmapSource

的创建从一个字节数组BitmapSource可能最简单的方法是BitmapFrame.Create

public BitmapSource ByteArrayToImage(byte[] buffer) 
{ 
    using (var stream = new MemoryStream(buffer)) 
    { 
     return BitmapFrame.Create(stream, 
      BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
    } 
} 

您将上述方法的返回值分配给Image控制的Source属性:

image.Source = ByteArrayToImage(barr); 
+0

It Worked! http://imgur.com/C8cUQTO – Phillip

2

一个可能的解决方案是保存图像文件(例如,jpg)为WPF嵌入的资源,然后用下面的代码来获得BitmapImage

清单1.获取的BitmapImage从EmbeddedResource

private string GetAssemblyName() 
{ 
    try { return Assembly.GetExecutingAssembly().FullName.Split(',')[0]; } 
    catch { throw; } 
} 

private BitmapImage GetEmbeddedBitmapImage(string pathImageFileEmbedded) 
{ 
    try 
    { 
     // compose full path to embedded resource file 
     string _fullPath = String.Concat(String.Concat(GetAssemblyName(), "."), pathImageFileEmbedded); 

     BitmapImage _bmpImage = new BitmapImage(); 
     _bmpImage.BeginInit(); 
     _bmpImage.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream(_fullPath); 
     _bmpImage.EndInit(); 
     return _bmpImage; 
    } 
    catch { throw; } 
    finally { } 
} 

相应地,WPF Image控制的Source属性(例如,Image1)设置为通过函数返回BitmapImage

Image1.Source = GetEmbeddedBitmapImage(_strEmbeddedPath); 

注意:应该参考以下:

using System.Windows.Media.Imaging; 
using System.Reflection; 

另一种可能的解决方案是使用Uri对象作为显示在下面的代码段,以获得从图像文件中的BitmapImage(清单2):

清单2.从文件中获取BitmapImage(使用Uri)

private BitmapImage GetBitmapImageFromFile(string ImagePath) 
{ 
    Uri BitmapUri; 
    StreamResourceInfo BitmapStreamSourceInfo; 
    try 
    { 
     // Convert stream to Image. 
     BitmapImage bi = new BitmapImage(); 
     BitmapUri = new Uri(ImagePath, UriKind.Relative); 
     BitmapStreamSourceInfo = Application.GetResourceStream(BitmapUri); 
     bi.BeginInit(); 
     bi.StreamSource = BitmapStreamSourceInfo.Stream; 
     bi.EndInit(); 
     return bi; 
    } 
    catch { throw; } 
} 

希望这可能有所帮助。

+0

那么,在这我会把图像本身?对于WPF来说很抱歉,所以我不在这里深入 – Phillip

+0

不要忘记设置'bi.CacheOption = BitmapCacheOption.OnLoad;',否则图像可能无法在需要显示时加载。你会认为这个框架会知道这么做,但它让我感到很困惑。 –

+0

您应该在XAML Grid中放置一些Image控件,并将其命名为将BitmapImage应用于其属性Image1.Source。最好的问候, –