当生成操作为Content/Resources时,相对uri错误不支持此操作

本文关键字:操作 相对 uri 错误 不支持 Resources Content | 更新日期: 2023-09-27 18:23:40

问题:我想将图像写入隔离存储。我使用下面的代码来获取图像的流,并使用可写位图将其写入隔离存储。

我尝试过什么:在谷歌搜索后,我实现了以下解决方案

String myImage = "myImage.png";
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (myIsolatedStorage.FileExists(myImage))
    {
        myIsolatedStorage.DeleteFile(myImage);
    }
    IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(myImage, FileMode.Create, myIsolatedStorage);
    //Uri uri = new Uri("/myApplication;component/Images/AppIcons/" + myImage, UriKind.Relative);
    //StreamResourceInfo sri = Application.GetResourceStream(uri); // Trying to get image whose Build action is set to 'Resource'
    //Uri uri1 = new Uri("Images/AppIcons/White.png", UriKind.Relative); // Content
    //StreamResourceInfo sri1 = Application.GetResourceStream(uri1); // Trying to get image whose Build action is set to 'Content'
    string[] names = this.GetType().Assembly.GetManifestResourceNames();
    string name = names.Where(n => n.EndsWith(myImage)).FirstOrDefault();
    if (name != null)
    {
        // Trying to get image whose Build action is set to 'Embedded Resource'
        Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
        if (resourceStream != null)
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(resourceStream);
            WriteableBitmap wb = new WriteableBitmap(bitmap);
            // Encode WriteableBitmap object to a JPEG stream.
            wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
        }
    }
    fileStream.Close();
}

这里的问题是,当我试图通过Application.GetResourceStream(如上面注释的代码所示)获得图像时,结果是:

相对uri 不支持此操作

我查看了MSDN的文章,并提出了这些SO问题(获取本地映像流。Windows phone 8和Application.GetResourceStream抛出IOException)来验证映像的URI路径,但它仍然无法获取StreamResourceInfo

当我将映像的构建操作设置为"Embedded Resource"并尝试使用GetManifestResourceStream获取流时,它工作正常(如上面的代码所示)。

如何使用构建操作为"内容"/"资源"的图像?在我们的项目中,所有图像大多是"内容"/"资源"。

如有任何帮助,我们将不胜感激。非常感谢您的时间和精力。

当生成操作为Content/Resources时,相对uri错误不支持此操作

StreamResourceInfo sri1 = Application.GetResourceStream(new Uri("image.png", UriKind.Relative));

非常适合我。图像构建操作设置为Content

确保路径是正确的