将BitmapImage转换为byte [] [英] Convert BitmapImage to byte[]

查看:56
本文介绍了将BitmapImage转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将BitmapImage转换为byte []时遇到问题。每当遇到不同的错误时,我都尝试了很多解决方案,但没有任何效果。

I have problem with converting BitmapImage to byte[]. I tried a lot of solutions and nothing works, every time i get different errors.

例如,我找到了不错的解决方案,但它也不起作用。

For example i found nice solutions but it also doesn't work. What's wrong with it?

我正在使用Windows Phone 8.1。

I'm using Windows Phone 8.1.

public static byte[] ImageToBytes(BitmapImage img)
{
    using (MemoryStream ms = new MemoryStream())
    {
        WriteableBitmap btmMap = new WriteableBitmap(img);
        System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
        img = null;
        return ms.ToArray();
    }
}

这是从此处获取的:将位图图像转换为字节数组(Windows Phone 8)


没有给出与 WriteableBitmap.WriteableBitmap(int,int)的必需形式
参数'pixelHeight'相对应的参数。

There is no argument given that corresponds to the required formal parameter 'pixelHeight' of 'WriteableBitmap.WriteableBitmap(int, int)'


命名空间 System.Windows.Media.Imaging中不存在类型或命名空间名称扩展(您是否缺少程序集
参考?)

The type or namespace name 'Extensions' does not exist in the namespace 'System.Windows.Media.Imaging' (are you missing an assembly reference?)

,或者如果有人对如何转换有其他想法,请发表。非常感谢您的帮助!

or if somebody has got another idea how to convert it, please post it. Thanks a lot for any help!

我也尝试过这样做: BitmapImage转换为字节[]

但使用时出现问题


BitmapImage是 System.Windows.Media.Imaging.BitmapImage和 Windows.UI.Xaml.Media.Imaging.BitmapImage之间的模糊引用。

'BitmapImage' is an ambiguous reference between 'System.Windows.Media.Imaging.BitmapImage' and 'Windows.UI.Xaml.Media.Imaging.BitmapImage'

所以我使用了 BitmapEncoder,但是它没有像Save and Frame这样的方法。

so I used "BitmapEncoder" but it doesn't have method like Save and Frame.

推荐答案

<我认为这不能在这个平台上完成。我将项目更改为Windows Phone Silverlight / 8.0,并且一切正常。

I think that it can't be done on this platform. I change my project to Windows Phone Silverlight/8.0 and there is working everything.

 public static BitmapImage BytesToImage(byte[] bytes)
    {
        BitmapImage bitmapImage = new BitmapImage();
        try
        {
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                bitmapImage.SetSource(ms);
                return bitmapImage;
            }
        }
        finally { bitmapImage = null; }
    }

    public static byte[] ImageToBytes(BitmapImage img)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            WriteableBitmap btmMap = new WriteableBitmap(img);
            System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
            img = null;
            return ms.ToArray();
        }
    }

这篇关于将BitmapImage转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆