2016-10-07 8 views
0

実行時に表示されるイメージを変更するにはどうすればよいですか?実行時に表示されるイメージを変更する

私のコードは以下の通りです:に起因する無行く

<Image Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <Image.Source> 
     <BitmapImage x:Name="Profile" 
      DecodePixelWidth="300" 
      DecodePixelHeight="300" 
      UriSource="/MyAssembly;component/Resources/Profile1.png" /> 
    </Image.Source> 
</Image> 

私が使用するつもりだ画像がそうサムネイルのこの種を作成するために、直接、画像要素を使用して、基本的に線画ありませんピクセルリサイズ。

UriSourceの画像を変更する必要はありません。画像のソースを使用すると、Image.Sourceセクション全体が無効になることがあります。

+0

どのようにして、プログラム 'UriSource'を変更できますか?変更したC#コードを表示できますか? –

+0

BitmapImageを実際にImage要素のSourceに渡すことができます。 –

+0

public ImageSource FileToImageSource(Uriファイル、int DecodeSize = 300) { BitmapImage bitmap = new BitmapImage(File); bitmap.DecodePixelWidth = DecodeSize; bitmap.Freeze(); 戻りビットマップ。 } –

答えて

0

あなたのUriSourceのこのフォーマットはうまくいくと思います。

<Image Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <Image.Source> 
     <BitmapImage x:Name="Profile" 
      DecodePixelWidth="300" 
      DecodePixelHeight="300" 
      UriSource="pack://application:,,,/MyAssembly;component/Resources/Profile1.png" /> 
    </Image.Source> 
</Image> 
+0

私はまだUriにエラーが発生しています。それを渡すと認識したくありません。 –

+0

イメージのビルドアクションを「リソース」に設定しましたか? –

0

パス文字列からImageSourceを作成して返すようにIValueConverterを使用してください。例えば;あなたの画像(desert.jpeg、penguins.jpg)はルートフォルダのImagesフォルダにあります。画像にはBuild Action Type to Content, and Copy To Output Directory to Alwaysを設定してください。

<local:UriToImgConverter x:Key="UriToImgCnvKey"/> 

<Image x:Name="ImgCtrl" Source="{Binding Img, Mode=OneWay,Converter={StaticResource UriToImgCnvKey}}"/> 

コード:

ImgViewModel vm1 = new ImgViewModel () ; 
ImgCtrl.DataContext = vm1; 
vm1.Img = "Images/Desert.jpg"; 

のViewModel /コンバーターコード:

public class ImgViewModel:INotifyPropertyChanged 
    {  
     string _path ; 
     public string Img { 

      get { return _path; } 
      set { _path = value; OnPropertyChanged("Img"); }   
     } 

     public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
     public void OnPropertyChanged(string prop) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
     } 
    } 

    public class UriToImgConverter : IValueConverter 
    { 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      BitmapImage img = new BitmapImage(new Uri(value.ToString(), UriKind.Relative)); 
      BitmapFrame frame = BitmapFrame.Create(img); 

      return frame; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
+0

Uriまたは文字列をImageSourceに変換するためのバインディングコンバータは必要ありません。これはImageSourceクラスのTypeConverterとして登録されているImageSourceConverterクラスによって既に行われています。さらに、ビルドアクションをResourceに設定して、イメージファイルをアセンブリに埋め込むことをお勧めします。したがって、追加の外部イメージファイルはありません。 – Clemens

+0

@Clemens Builtinコンバータには、パッケージURIが必要です。相対的なウイは働きません。 – AnjumSKhan

+0

OPはPack URIを使用するので、何がポイントですか?それに加えて、組み込みの型変換は、有効な任意のURIで動作します。 – Clemens

関連する問題