Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# InteropBitmap到BitmapImage_C#_Wpf_Bitmapimage_Bitmapsource_Interopbitmapimage - Fatal编程技术网

C# InteropBitmap到BitmapImage

C# InteropBitmap到BitmapImage,c#,wpf,bitmapimage,bitmapsource,interopbitmapimage,C#,Wpf,Bitmapimage,Bitmapsource,Interopbitmapimage,我正在尝试将位图(SystemIcons.Question)转换为位图图像,以便在WPF图像控件中使用它 我使用以下方法将其转换为BitmapSource,但它返回InteropBitmapImage,现在的问题是如何将其转换为BitmapImage。直接演员似乎不起作用 有人知道怎么做吗 代码: 属性返回BitmapImage:(绑定到我的图像控件) 您应该能够使用: public BitmapImage QuestionIcon { get {

我正在尝试将
位图(SystemIcons.Question)
转换为
位图图像
,以便在WPF图像控件中使用它

我使用以下方法将其转换为
BitmapSource
,但它返回
InteropBitmapImage
,现在的问题是如何将其转换为
BitmapImage
。直接演员似乎不起作用

有人知道怎么做吗

代码:

属性返回BitmapImage:(绑定到我的图像控件)


您应该能够使用:

    public 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;
                System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
                bImg.BeginInit();
                bImg.StreamSource = new MemoryStream(ms.ToArray());
                bImg.EndInit();
                return bImg;
            }
        }
    }

InteropBitmapImage
继承自
ImageSource
,因此您可以在
图像
控件中直接使用它。您不需要它是从ImageSource继承的
BitmapImage

InteropBitmapImage,因此可以在图像控件中使用它。你为什么需要它成为位图图像?@Thomas,没错!我不需要做转换就解决了它!:)把你的评论作为回答,我会接受的!
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;
        }
    }
}
    public 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;
                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;
        }
    }
}