[英]This operation is not supported for a relative uri error when build action is Content/Resource
問題:我想將圖像寫入隔離存儲。 我正在使用下面的代碼來獲取圖像流,並使用可寫位圖將其寫入隔離存儲。
我嘗試了什么:谷歌搜索后,我實現了以下解決方案
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問題( 獲取本地圖像流。WindowsPhone 8和Application.GetResourceStream引發IOException )以驗證圖像的URI路徑,但仍無法獲取StreamResourceInfo
。
當我將圖像的構建動作設置為' Embedded Resource '並嘗試使用GetManifestResourceStream
獲取流時,則它工作正常(如上面的代碼所示)。
如何使其與具有“ 內容” /“資源 ”的構建動作的圖像一起使用? 在我們的項目中,所有圖像大多是“內容” /“資源”。
任何幫助將不勝感激。 非常感謝您的時間和精力。
StreamResourceInfo sri1 = Application.GetResourceStream(new Uri("image.png", UriKind.Relative));
對我來說很完美。 圖像構建操作設置為Content
。
確保路徑正確
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.