質問

私は、WPFのImageコントロールでそれを使用できるようにBitmap (SystemIcons.Question)BitmapImageを変換しようとしています。

私はBitmapSourceに変換するには、次の方法がありますが、それは今の問題はInteropBitmapImageに変換する方法である、BitmapImageを返します。直接的なキャストが動作するようには思えません。

誰もがそれを行う方法を知っていますか?

CODEます:

 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();
            }
        }
役に立ちましたか?

解決

InteropBitmapImageImageSourceから継承するので、あなたは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;
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top