Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
WPF C#图像源_C#_Wpf_Image - Fatal编程技术网

WPF C#图像源

WPF C#图像源,c#,wpf,image,C#,Wpf,Image,我对在WPF表单中显示图像非常陌生,在转换和分配GUI的图像源时遇到了麻烦 System.Drawing.Image testImg = ImageServer.DownloadCharacterImage(charID, ImageServer.ImageSize.Size128px); byte[] barr = imgToByteArray(testImg); CharImage.Source = ByteToImage(barr);

我对在WPF表单中显示图像非常陌生,在转换和分配GUI的图像源时遇到了麻烦

        System.Drawing.Image testImg = ImageServer.DownloadCharacterImage(charID, ImageServer.ImageSize.Size128px);
        byte[] barr = imgToByteArray(testImg);
        CharImage.Source = ByteToImage(barr);          

    public byte[] imgToByteArray(System.Drawing.Image testImg)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            testImg.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
    }

    public System.Drawing.Image ByteToImage(byte[] barr)
    {
        MemoryStream ms = new MemoryStream(barr);
        System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
        return returnImage;
    }

因此,我从EVE在线C#API库中获取一幅图像(JPEG),然后尝试将其转换为字节数组并返回到正确的图像。然而,我总是会遇到这样的错误:“无法将类型‘System.Drawing.Image’隐式转换为‘System.Windows.Media.ImageSource’”。对于如何解决这个问题,我完全目瞪口呆

一种可能的解决方案是将图像文件(例如,.
jpg
)保存为WPF嵌入式资源,然后使用以下代码段获取
BitmapImage

清单1。从EmbeddedResource获取位图图像

相应地,将WPF
Image
控件(例如,
Image1
)的
Source
属性设置为函数返回的
BitmapImage

Image1.Source = GetEmbeddedBitmapImage(_strEmbeddedPath);
注意:您应参考以下内容:

using System.Windows.Media.Imaging;
using System.Reflection;
另一种可能的解决方案是使用
Uri
对象从图像文件中获取
BitmapImage
,如以下代码段(清单2)所示:

清单2。从文件获取位图图像(使用Uri)


希望这能有所帮助。

System.Drawing.Image
是WinForms,而不是WPF。您的ByteToImage方法应该返回
BitmapSource

从字节数组创建
BitmapSource
的最简单方法可能是
BitmapFrame。创建

public BitmapSource ByteArrayToImage(byte[] buffer)
{
    using (var stream = new MemoryStream(buffer))
    {
        return BitmapFrame.Create(stream,
            BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    }
}
您可以将上述方法的返回值指定给
图像
控件的
属性:

image.Source = ByteArrayToImage(barr);

那么我将把图像本身放在哪里呢?抱歉,对WPF来说太陌生了,所以我已经没有深度了。请不要忘记设置
bi.CacheOption=BitmapCacheOption.OnLoad,否则在需要显示图像时可能无法加载图像。您可能认为框架会知道如何执行此操作,但有几次我被它抓住了。您应该在XAML网格中放置一些图像控件,并命名为,例如,将BitmapImage应用于其属性Image1.Source。最好的方面是,比嵌入式资源更好的是普通资源。然后,您将从一个类似的
新位图(新Uri(“pack://application:,,,/Images/Test.jpg”)
public BitmapSource ByteArrayToImage(byte[] buffer)
{
    using (var stream = new MemoryStream(buffer))
    {
        return BitmapFrame.Create(stream,
            BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    }
}
image.Source = ByteArrayToImage(barr);