0

好的,从字节到字符串等有无数种,但我根本找不到任何关于如何将资源文件转换为 Visual Studio 的内容,无论我做什么,我都可以很好地转换为字节 [],转换为普通图像所以我实际上可以在我的 C# 代码中使用它吗?

4

2 回答 2

1

您可以使用 valueconverter 来执行此操作 -

public class InMemoryImageValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var image = new BitmapImage();
        var memoryStream = new MemoryStream((byte[])value);
        image.SetSource(memoryStream);
        return image;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
        return null;
    }
}

改编自https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/PictureChooser/Cirrious.MvvmCross.Plugins.PictureChooser.WindowsPhone/MvxInMemoryImageValueConverter.cs#L17

那么您可以在 Image 控件中将其用作

<Image Source="{Binding TheBytes, Converter={StaticResource InMemoryImage}}" />
于 2013-05-07T08:51:32.983 回答
0

如果您的图像是应用程序的资源,那么您可以执行以下操作

Uri uriR = new Uri("/WP7SampleProject3;component/images/appbar.feature.email.rest.png", UriKind.Relative);
BitmapImage imgSourceR = new BitmapImage(uriR);
this.imageR.Source = imgSourceR;
于 2013-05-07T08:54:41.640 回答