LoginSignup
8
7

More than 5 years have passed since last update.

WPF BitmapImageをImageにバインドしたのにうまく表示されない

Last updated at Posted at 2015-09-17

「イメージファイル(png)をBitmapImageに読み込んでxaml上でImageにバインドしたけど表示されないことがある」
という問題が発生。表示できるものとできないものがある。
結果、うまく表示できるようになったのでそのメモ。

結論:Imageの「Stretch="None"」の指定やめて、Witdh,Heightにイメージのサイズを指定したら表示された

うまく表示されないときのxaml。

<Image Source="{Binding ImageSource}" Stretch="None"/>

うまく表示されるxaml。

<Image Source="{Binding ImageSource}" Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}" Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}" />

修正は二箇所。

修正1 Stretch="None"はずした

Stretchの説明には、None「コンテンツの現在のサイズが維持されます。」と書いてある。
なので、てっきりNoneにすればいい具合に等倍で表示されると思い込んで指定してた。

修正2 Width, Heightの設定

BitmapImageがおかしいんじゃないかと思い調べたところ、Width, Heightに謎の巨大な値が入っていた(Dpiとの兼ね合い?)。
PixelWidth, PixelHeightには正しいイメージのサイズが入っていたので、ImageのWidth,Heightにその値をバインドしたところ、うまく表示されるようになった。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7