2011-04-28 6 views
2

일부 이미지는 Properties.Resources에 추가되었습니다.Xaml에서 Properties.Resources의 이미지를 바인딩하는 방법?

Properties.Resources.LayerIcon; 

Xaml에서 이미지를 사용하고 싶지만이를 수행하는 방법을 모릅니다.

WPF 프로젝트에 이미지를 추가하는 여러 가지 방법이 있지만 Properties.Resources을 사용해야합니다. 응용 프로그램을 리플렉션을 통해 시작할 때 이미지가 나타나는 위치에서 찾을 수있는 유일한 방법이기 때문에 Properties.Resources을 사용해야합니다.

답변

9

Properties.Resources의 이미지는 System.Drawing.Bitmap이지만, WPF는 System.Windows.Media.ImageSource을 사용합니다.

[ValueConversion(typeof(System.Drawing.Bitmap), typeof(ImageSource))] 
public class BitmapToImageSourceConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var bmp = value as System.Drawing.Bitmap; 
     if (bmp == null) 
      return null; 
     return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
        bmp.GetHbitmap(), 
        IntPtr.Zero, 
        Int32Rect.Empty, 
        BitmapSizeOptions.FromEmptyOptions()); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

을하고 다음과 같이 사용합니다 : 당신은 변환기를 만들 수 있습니다

<Image Source="{Binding Source={x:Static prop:Resources.LayerIcon}, Converter={StaticResource bitmapToImageSourceConverter}}" /> 

이 자원은 공공이 아닌 내부로 설정되어 있는지 확인합니다.

+0

감사합니다. GetHBitmap을 어디서 얻었습니까? 추가해야하는 관리되지 않는 방법입니까? –

+0

@ 조안 Venge, 아니, 그건 [비트 맵 클래스의 방법] (http://msdn.microsoft.com/en-us/library/1dz311e4.aspx) –

+0

고마워,하지만 난이 오류가 어떤 이유로 얻을 : System.Drawing.Bitmap '에'GetHBitmap '에 대한 정의가없고'System.Drawing.Bitmap '유형의 첫 번째 인수를 수락하는 확장 메서드'GetHBitmap '이 없습니다.' –

관련 문제