BitMapImage에서 interopbitmap
-
19-09-2019 - |
문제
나는 변환하려고 노력하고있다 Bitmap (SystemIcons.Question)
a BitmapImage
따라서 WPF 이미지 컨트롤에서 사용할 수 있습니다.
나는 그것을 BitmapSource
, 그러나 반환합니다 InteropBitmapImage
, 이제 문제는 그것을 BitmapImage
. 직접 캐스트는 작동하지 않는 것 같습니다.
아무도 그것을하는 방법을 아는 사람이 있습니까?
암호:
public BitmapSource ConvertToBitmapSource()
{
int width = SystemIcons.Question.Width;
int height = SystemIcons.Question.Height;
object a = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(SystemIcons.Question.ToBitmap().GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(width, height));
return (BitmapSource)a;
}
BitMapImage를 반환 할 속성 : (내 이미지 제어에 바인딩)
public BitmapImage QuestionIcon
{
get
{
return (BitmapImage)ConvertToBitmapSource();
}
}
해결책
InteropBitmapImage
상속 ImageSource
, 당신은 그것을 직접 사용할 수 있습니다 Image
제어. 당신은 그것이 필요하지 않습니다 BitmapImage
.
다른 팁
사용할 수 있어야합니다.
public BitmapImage QuestionIcon
{
get
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap dImg = SystemIcons.ToBitmap();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(ms.ToArray());
bImg.EndInit();
return bImg;
}
}
}
public System.Windows.Media.Imaging.BitmapImage QuestionIcon
{
get
{
using (MemoryStream ms = new MemoryStream())
{
System.Drawing.Bitmap dImg = SystemIcons.ToBitmap();
dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
var bImg = new System.Windows.Media.Imaging.BitmapImage();
bImg.BeginInit();
bImg.StreamSource = ms;
bImg.EndInit();
return bImg;
}
}
}
제휴하지 않습니다 StackOverflow