C# 位图帧解码为图像 最近,我决定玩Windows演示文稿基金会,并创建一个棋棋。整个事情已经完成了(我相信),但现在已经很长时间了,因为我找不到一种方法将我的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"); } ... } }
My ImagesDictionary.xaml文件:C# 位图帧解码为图像 最近,我决定玩Windows演示文稿基金会,并创建一个棋棋。整个事情已经完成了(我相信),但现在已经很长时间了,因为我找不到一种方法将我的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"); } ... } },c#,.net,wpf,exception-handling,bitmap,C#,.net,Wpf,Exception Handling,Bitmap,My ImagesDictionary.xaml文件: <ResourceDictionary> <ImageSource x:Key="BlackBishop">BlackBishop.png</ImageSource> <ImageSource x:Key="WhiteBishop">WhiteBishop.png</ImageSource> ... </ResourceDictionary>
<ResourceDictionary>
<ImageSource x:Key="BlackBishop">BlackBishop.png</ImageSource>
<ImageSource x:Key="WhiteBishop">WhiteBishop.png</ImageSource>
...
</ResourceDictionary>
blackishop.png
WhiteBishop.png
...
问题是我不知道如何转换GetImage的输出
(System.Windows.Media.Imaging.BitmapFrameDecode)
进入
(System.Windows.Controls.Image)
有什么想法吗?System.Windows.Media.Imaging.BitmapFrame源自ImageSource 您应该能够做到这一点:
this.Image = new Image();
this.Image.Source = this.GetImage("WhiteBishop");
或
如果BitmapFrameDecode确实来自System.Windows.Imaging.Image,而不是来自ImageSource
-Jesse好的,下面是一个改进的GetImageSource(更改名称以反映其真正用途)
Drawing.Image
案例将处理BMP、PNG、JPG、GIF等,即使我使用了BmpBitmapDecoder
。但是请注意,XAML图像的拉伸非常漂亮,但是Drawing.Image
-Jesse将img设置为新位图图像
使用mem作为新的System.IO.MemoryStream()使用
img.BeginInit()
img.StreamSource=mem
img.EndInit()
终端使用
返回img
它以一种没有错误的方式工作。。。但是,即使设置了源代码集,图像控件也不会显示任何内容。。。但是谢谢,我还需要调试一些Szymon遇到了一个可能是问题所在的弱点。如果您正在更新的图像恰好是一个UI元素,并且您将其设置为null,则其与其父级的链接将丢失。始终设置mage.Source,以便上面的对象关系不会丢失。@Szymon您有机会尝试此Drawing.Image
转换代码吗?
this.Image.Source = this.GetImage("WhiteBishop").Source;
/// <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 ...
}
Dim img As New BitmapImage
Using mem As New System.IO.MemoryStream(<InputArrayHere>)
img.BeginInit()
img.StreamSource = mem
img.EndInit()
End Using
return img