プロが教える店舗&オフィスのセキュリティ対策術

お世話になります。
visual studio community WPF C# で作成しています。

以下のような状況です。
・プロジェクトに「flower.png」を組み込んでいる。※プロパティを「Embedded Resource」
・xamlに直接imageコントロールを載せて、source="flower.png"にすると画像は表示される。
・以下のソースで、画像をmitmapimageに取り込んで、ソースで生成した「imageコントロール」に
表示させているが、Height=250,Width=250のimageコントロールの領域は表示される(マウスで選択ができる)が、実際のflower.pngの画像が表示されない
---------------------------------------------------------------------------
Image MyImage = new Image();
BitmapImage gazou = new BitmapImage(new Uri("application:,,,/flower.png", UriKind.RelativeOrAbsolute));
MyImage.Source = gazou;
MyImage.Height = 250;
MyImage.Width = 250;
---------------------------------------------------------------------------


どのようにすれば、ソースで生成した「imageコントロール」に「flower.png」を表示することができるでしょうか?

何卒よろしくお願い足します。

質問者からの補足コメント

  • BitmapImage gazou = new BitmapImage(new Uri("application:,,,/flower.png", UriKind.RelativeOrAbsolute));

    BitmapImage gazou = new BitmapImage(new Uri("pack://application:,,,/flower.png", UriKind.RelativeOrAbsolute));
    にすると、以下のエラーが出る状況でございます。

    「BitmapImageに画像を表示したい」の補足画像1
      補足日時:2016/06/16 17:31

A 回答 (2件)

WPFは判りませんが……



>BitmapImage gazou = new BitmapImage(new Uri("application:,,,/flower.png", UriKind.RelativeOrAbsolute));
でgazouがnullになっている(つまり失敗している)ということはありませんか?

Uriのコンストラクタはその形式でいいんでしょうか?
https://msdn.microsoft.com/ja-jp/library/aa97006 …
だと"pack://application:~"と続くように見えますが。
# その形式で直接リソース指定できるのかは知らない。
    • good
    • 0
この回答へのお礼

お返事いただきまして、誠にありがとうございます。
BitmapImage gazou = new BitmapImage(new Uri("application:,,,/flower.png", UriKind.RelativeOrAbsolute));

BitmapImage gazou = new BitmapImage(new Uri("pack://application:,,,/flower.png", UriKind.RelativeOrAbsolute));
にすると、以下のエラーが出る状況でございます。

全く知識がないため、手も足もでない状況です。
もしご存知でしたら、何卒よろしくお願いいたします。

お礼日時:2016/06/16 17:32

https://msdn.microsoft.com/ja-jp/library/ms131565

>uriString に指定されたポート番号が無効であるか、解析できません。
といわれている…みたいですな。


http://o-maguro.hatenablog.com/entry/2015/10/10/ …
こんなページも見つけましたが…。
WPF触ったことないので後は不明。

とりあえず、変更前の状態でgazouがnullかどうか位は判定できるんじゃないですかね?
    • good
    • 0
この回答へのお礼

ありがとうございます!!!

>http://o-maguro.hatenablog.com/entry/2015/10/10/ …
これでいけました!!

本当にありがとうございます!!

お礼日時:2016/06/20 11:21

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!