0

最近,我决定使用 Windows Presentation Foundation 并创建一个国际象棋的变体。整个事情已经完成了(我相信),但现在已经很长时间了,因为我找不到将我的 PNG 文件设置为图像控件的方法。这是受问题影响的示例类:

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)

有任何想法吗?

4

3 回答 3

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 派生的。

-杰西

于 2012-06-13T15:01:12.150 回答
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
于 2013-01-11T05:44:57.817 回答
0

好的,这是一个改进的 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不会。

-杰西

于 2012-06-14T22:40:47.397 回答