WPF - 来自基本64字符串(或字节数组)的窗口图标的ImageSource

时间:2014-03-15 16:19:33

标签: wpf icons base64 imagesource

我在WPF中,并且我使用WCF服务将图像作为基本64位编码字符串。我将基本64位编码的字符串转换为ImageSource以分配给Image.Source。我编写了一个测试应用程序来测试该过程,只要我只使用ImageSource作为Image.Source,一切似乎都能正常工作。但我还需要使用一些ImageSources作为窗口图标。这就是我要做的事情:

ImageSource src = Util.Base64StringToImageSource(tbxBase64String.Text);
//img is a System.Windows.Controls.Image
img.Source = src; //This works just fine
//Class is a Window, so Icon is this.Icon (System.Windows.Window.Icon)
Icon = src; //This throws an InvalidOperationException - "ImageSource for Icon property must be an icon file."

以下是转换方法:

public static System.Windows.Media.ImageSource Base64StringToImageSource(string base64String)
{
    using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64String)))
    {
        System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
        bi.BeginInit();
        bi.StreamSource = stream;
        bi.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
        bi.EndInit();
        bi.Freeze();
        return bi;
    }
}

我需要知道如何从基础64字符串(或字节数组)中获取可用于窗口图标的ImageSource。

2 个答案:

答案 0 :(得分:2)

我解决了。我挖了一些,并调查了抛出的确切错误。这是堆栈跟踪:

at System.Windows.Window._OnIconChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)    在System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)    在System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)    在System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)    在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex,DependencyProperty dp,PropertyMetadata metadata,EffectiveValueEntry oldEntry,EffectiveValueEntry& newEntry,Boolean coerceWithDeferredReference,OperationType operationType)    在System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp,Object value,PropertyMetadata metadata,Boolean coerceWithDeferredReference,OperationType operationType,Boolean isInternal)    在System.Windows.DependencyObject.SetValue(DependencyProperty dp,Object value)    在System.Windows.Window.set_Icon(ImageSource值)    at ImageToBase64String.MainWindow.btnStrToImg_Click(Object sender,RoutedEventArgs e)位于c:\ Data \ Source \ TorrentRover \ TorrentRover \ ImageToBase64String \ ImageToBase64String \ MainWindow.xaml.cs:第48行

您会注意到System.Windows.Window._OnIconChanged中引发了异常。这是使用ILSpy的样子:

// System.Windows.Window
private static void _OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    Window window = (Window)d;
    BitmapFrame bitmapFrame;
    if (e.NewValue == null)
    {
        bitmapFrame = null;
    }
    else
    {
        bitmapFrame = (e.NewValue as BitmapFrame);
        if (bitmapFrame == null)
        {
            throw new InvalidOperationException(SR.Get("IconMustBeBitmapFrame"));
        }
    }
    window.OnIconChanged(bitmapFrame);
}

这让我相信我需要一个围绕我的位图图像的BitmapFrame。所以我改变了我的Base64StringToImageSource方法,现在它可以工作了。

public static System.Windows.Media.ImageSource Base64StringToImageSource(string base64String)
{
    using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64String)))
    {
        return BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    }
}

答案 1 :(得分:0)

该文件必须是.ico,而不是bmp / png / jpg