在WPF中放置图标对象
我已经实例化了几个System.Drawing.Icon对象。请注意,这些文件是在运行时创建的,不是从文件系统存储和加载的。我想把这些图像放在我的WPF应用程序中 但是,正如我在过去几个小时中发现的,不可能简单地将诸如system.drawing.image或图标之类的对象直接添加到画布/堆栈面板,也不可能将system.Windows.Controls.image的源设置为未存储在文件系统中的图像或图标(或者在我看来是这样)在WPF中放置图标对象,wpf,Wpf,我已经实例化了几个System.Drawing.Icon对象。请注意,这些文件是在运行时创建的,不是从文件系统存储和加载的。我想把这些图像放在我的WPF应用程序中 但是,正如我在过去几个小时中发现的,不可能简单地将诸如system.drawing.image或图标之类的对象直接添加到画布/堆栈面板,也不可能将system.Windows.Controls.image的源设置为未存储在文件系统中的图像或图标(或者在我看来是这样) 有什么想法吗?这对我来说很有效,可以动态设置WPF图像,其中包含从动
有什么想法吗?这对我来说很有效,可以动态设置WPF图像,其中包含从动态生成或从磁盘加载的位图加载的字节:
using System;
using System.Collections.Generic;
using System.Text;
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.IO;
using System.Drawing.Imaging;
namespace Examples
{
public class Util
{
private static void SetBitmap(Image imgDest, Bitmap bmpSource)
{
byte[] imageBytes;
using (MemoryStream stream = new MemoryStream())
{
bmpSource.Save(stream, ImageFormat.Png);
imageBytes = stream.ToArray();
}
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageBytes);
bitmapImage.EndInit();
imgDest.Source = bitmapImage;
}
}
}
这对我来说非常有效,可以动态设置WPF图像,其中包含从动态生成或从磁盘加载的位图加载的字节:
using System;
using System.Collections.Generic;
using System.Text;
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.IO;
using System.Drawing.Imaging;
namespace Examples
{
public class Util
{
private static void SetBitmap(Image imgDest, Bitmap bmpSource)
{
byte[] imageBytes;
using (MemoryStream stream = new MemoryStream())
{
bmpSource.Save(stream, ImageFormat.Png);
imageBytes = stream.ToArray();
}
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(imageBytes);
bitmapImage.EndInit();
imgDest.Source = bitmapImage;
}
}
}
您好,大卫,谢谢。是的,我早些时候尝试过类似的方法,但遇到了一个“错误HRESULT E_FAIL已从对COM组件的调用中返回”。当尝试“bmpSource.Save”时,我不得不将图标添加到一个好的老式系统中。Windows.Forms.ImageList()First您好,大卫,谢谢。是的,我早些时候尝试过类似的方法,但遇到了错误“调用COM组件时返回错误HRESULT E_FAIL。”在尝试“bmpSource.Save”时,我必须先将图标添加到一个好的老式系统中。Windows.Forms.ImageList()