简体   繁体   English

将字符串转换为新的Image XAML C#

[英]Converting string into new Image XAML C#

I've got a string of a location for an image, and I'm trying to create a XAML Image object, change the source to the string, and append it to a stackpanel. 我有一个图像位置字符串,并且试图创建XAML Image对象,将源更改为字符串,然后将其附加到堆栈面板。

Here's what I've tried so far: 到目前为止,这是我尝试过的方法:

System.Windows.Media.Imaging.BitmapImage tmp = new System.Windows.Media.Imaging.BitmapImage();
tmp.UriSource = new Uri("Assets/image.jpg", UriKind.RelativeOrAbsolute);
Image image = new Image();
image.Source = tmp;

The problem is that I can't seem to convert System.Windows.Media.Imaging.BitmapImage to Windows.UI.Xaml.Media.ImageSource. 问题是我似乎无法将System.Windows.Media.Imaging.BitmapImage转换为Windows.UI.Xaml.Media.ImageSource。

I'm using that code pretty much directly off the Microsoft MSDN website, and I can't understand why it's not working. 我几乎直接在Microsoft MSDN网站上使用该代码,但我不明白为什么它不起作用。 I've also tried creating an ImageSource object and I just can't seem to get anything to work. 我也尝试过创建ImageSource对象,但似乎什么也无法工作。 It's frustrating. 真令人沮丧

Displaying Image s from string file paths is easy in WPF. 在WPF中,从string文件路径显示Image很容易。 First, ensure that the files have been added into the project using Visual Studio's Add Existing Item command so that it adds the image files with a Build Action value of Resource . 首先,请确保已使用Visual Studio的“ 添加现有项”命令将文件添加到项目中,以便它使用Build Action值为Resource添加图像文件。 Next, you need to use the correct file path. 接下来,您需要使用正确的文件路径。 It is best if you refer to the Pack URIs in WPF page on MSDN for full details, but you can use this format pretty much everywhere in your application: 最好参考MSDN上的WPF页面中的Pack URI以获取完整的详细信息,但是您可以在应用程序中的几乎所有地方使用此格式:

pack://application:,,,/ProjectOrAssemblyName;component/SubFolderName/ImageName.png

To display that image, you can simply set that value to a string property and data bind it to an Image.Source property in the UI, or use it directly: 要显示该图像,您可以简单地将该值设置为string属性,并将数据绑定到UI中的Image.Source属性,或直接使用它:

<Image Source="{Binding YourImageSource}" />

... ...

<Image Source="pack://application:,,,/
    ProjectOrAssemblyName;component/SubFolderName/ImageName.png" />

The problem is that I can't seem to convert System.Windows.Media.Imaging.BitmapImage to Windows.UI.Xaml.Media.ImageSource. 问题是我似乎无法将System.Windows.Media.Imaging.BitmapImage转换为Windows.UI.Xaml.Media.ImageSource。

As per ImageSource reference it seems you are using WinRT app and not WPF. 根据ImageSource参考,似乎您正在使用WinRT应用程序,而不是WPF。

You should use Windows.UI.Xaml.Media.Imaging.BitmapImage if you are targeting WinRT app. 如果您要定位WinRT应用,则应使用Windows.UI.Xaml.Media.Imaging.BitmapImage System.Windows.Media.Imaging.BitmapImage meant to be used from WPF. System.Windows.Media.Imaging.BitmapImage打算从WPF中使用。

You also can try this: 您也可以尝试以下操作:

BitmapImage BitmapToImageSource(System.Drawing.Bitmap bitmap, System.Drawing.Imaging.ImageFormat imgFormat)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, imgFormat);
                memory.Position = 0;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();

                return bitmapImage;
            }
        }

(source: http://pastebin.com/PAJ7Cd0x ) (来源: http : //pastebin.com/PAJ7Cd0x

Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM