2012-06-13 17 views
0

最近,我決定玩Windows Presentation Foundation並創建一個國際象棋的變體。整個事情有點兒完成了(我相信),但現在已經很長時間了,因爲我找不到將PNG文件設置爲Image控件的方法。這是由一個問題影響的示範類:BitmapFrameDecode into Image

public class Field 
{ 
    public Image Image { get; set; } 

    public Image GetImage(String keyName) 
    { 
     ResourceDictionary imagesDictionary = new ResourceDictionary(); 
     imagesDictionary.Source = new Uri("file:/[...]/ImagesDictionary.xaml"); 
     var var = imagesDictionary[keyName]; 
     Image image = (Image) imagesDictionary[keyName]; 
     return image; 
    } 

    public void RefreshImage() 
    { 
     if (Unit.Subclass.CompareTo("Bishop").Equals(0)) 
     { 
      if (Unit.Player.IsWhite) 
      { 
       this.Image = this.GetImage("WhiteBishop"); 
      } 
      ... 
    } 

} 

我ImagesDictionary.xaml文件:

<ResourceDictionary> 
    <ImageSource x:Key="BlackBishop">BlackBishop.png</ImageSource> 
    <ImageSource x:Key="WhiteBishop">WhiteBishop.png</ImageSource> 
    ... 
</ResourceDictionary> 

而問題是,我不知道怎麼的getImage

的輸出轉換

(System.Windows.Media.Imaging.BitmapFrameDecode)

成在

(System.Windows.Controls.Image)

任何想法?

回答

1

System.Windows.Media.Imaging.BitmapFrame派生自ImageSource。

你應該能夠做到這一點:

this.Image = new Image(); 
this.Image.Source = this.GetImage("WhiteBishop"); 

this.Image.Source = this.GetImage("WhiteBishop").Source; 

如果BitmapFrameDecode是真正來源於System.Windows.Imaging.Image,而不是從ImageSource的。

-Jesse

+0

它的工作方式,有沒有錯誤...但是,即使具有源集,Image控件顯示什麼...但感謝,我不得不去維護一些。 ;-) – smsware

+0

Szymon,跑進一個可能是問題的弱點。如果您正在更新的圖像恰好是一個UI元素,並將其設置爲空,則與其父項的鏈接將丟失。總是設置圖像源,以便對象關係不會在那裏丟失。 –

0

OK,這裏是一個改進GetImageSource(更名,以反映其真正的目的)。

/// <summary> 
    /// Get an ImageSource from the ResourceDictionary referred to by the 
    ///  <paramref name="uriDictionary"/>. 
    /// </summary> 
    /// <param name="keyName">The ResourceDictionary key of the ImageSource 
    ///  to retrieve.</param> 
    /// <param name="uriDictionary">The Uri to the XAML file that holds 
    ///  the ResourceDictionary.</param> 
    /// <returns><c>null</c> on failure, the requested <c>ImageSource</c> 
    ///  on success.</returns> 
    /// <remarks>If the requested resource is an <c>Image</c> instead of 
    ///  an <c>ImageSource</c>, 
    /// then the <c>image.Source</c> is returned.</remarks> 
    public static ImageSource GetImageSource(String keyName, Uri uriDictionary) 
    { 
     if (String.IsNullOrEmpty(keyName)) 
      throw new ArgumentNullException("keyName"); 
     if (null == uriDictionary) 
      throw new ArgumentNullException("uriDictionary"); 

     ResourceDictionary imagesDictionary = new ResourceDictionary(); 
     imagesDictionary.Source = uriDictionary; 
     var var = imagesDictionary[keyName]; 
     Object blob = imagesDictionary[keyName]; 
     Debug.WriteLine(String.Format(
      "error: GetImageSource('{0}', '{1}')" 
      + " value is: {2}", 
      keyName, 
      uriDictionary, 
      (null == blob) ? "null" : blob.GetType().FullName)); 
     if (null != blob) 
     { 
      if (blob is ImageSource) 
      { 
       return blob as ImageSource; 
      } 
      if (blob is Image) 
      { 
       Image image = blob as Image; 
       return image.Source; 
      } 
      if (blob is System.Drawing.Image) 
      { 
       System.Drawing.Image dImage = blob as System.Drawing.Image; 
       MemoryStream mem = new MemoryStream(); 
       dImage.Save(mem, System.Drawing.Imaging.ImageFormat.MemoryBmp); 
       mem.Position = 0; 
       BitmapDecoder decode = new BmpBitmapDecoder(
        mem, 
        BitmapCreateOptions.None, 
        BitmapCacheOption.None); 
       return decode.Frames[0]; 
      } 
      Debug.WriteLine(String.Format(
       "error: GetImageSource('{0}', '{1}')" 
       + " can't handle type: {2}", 
       keyName, 
       uriDictionary, 
       blob.GetType().FullName)); 
     } 
     return null; 
    } 

,並使用它,你會做這樣的:

String packText = String.Format("pack://application:,,,/{0};component/{1}", 
    Assembly.GetEntryAssembly().FullName, 
    "ImageDictionary.xaml"); 
Uri imageUri = new Uri(packText); 
// or if you prefer: 
// Uri imageUri = new Uri("file:///.../ImageDictionary.xaml"); 
// 

ImageSource source = GetImageSource(imageKey, imageUri); 

if (null != source) 
{ 
    this.Image.Source = source; 
} 
else 
{ 
    // bail ... 
} 

Drawing.Image情況下將處理BMP,PNG,JPG,GIF等,即使我用BmpBitmapDecoder。但請注意,XAML圖像非常漂亮,但Drawing.Image不。

-Jesse

+0

@Szymon你有沒有機會嘗試這個'Drawing.Image'轉換代碼呢? –

0
Dim img As New BitmapImage 

Using mem As New System.IO.MemoryStream(<InputArrayHere>) 
    img.BeginInit() 
    img.StreamSource = mem 
    img.EndInit() 
End Using 

return img