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}}" />
이 자원은 공공이 아닌 내부로 설정되어 있는지 확인합니다.
감사합니다. GetHBitmap을 어디서 얻었습니까? 추가해야하는 관리되지 않는 방법입니까? –
@ 조안 Venge, 아니, 그건 [비트 맵 클래스의 방법] (http://msdn.microsoft.com/en-us/library/1dz311e4.aspx) –
고마워,하지만 난이 오류가 어떤 이유로 얻을 : System.Drawing.Bitmap '에'GetHBitmap '에 대한 정의가없고'System.Drawing.Bitmap '유형의 첫 번째 인수를 수락하는 확장 메서드'GetHBitmap '이 없습니다.' –