2012年7月6日金曜日

[Windows Phone]リソースの画像ファイル取得ではまった

リソースの画像ファイル(プロジェクトに登録した画像)を、プログラムから取り出して表示したかったのですが、なかなか取り出せませんでした。
1.pngファイルを Images/Category/Image01.pngとして登録
2.画像のプロパティ「ビルドアクション」を「コンテンツ」に設定
3.以下のコードで実行

StreamResourceInfo source = Application.GetResourceStream(new Uri("/Images/Category/Image01.png", UriKind.Relative));
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(source.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);

これで実行しても、StreamResourceInfoはnullになってしまいます。
間違っているのは1行目のパス設定方法。
正しくは以下になります。

StreamResourceInfo source = Application.GetResourceStream(new Uri("Images/Category/Image01.png", UriKind.Relative));
画像パスの指定で先頭のスラッシュを消しました。
これで読み込むことが出来ました。

ルートがどこかは不明です。