如何将图像转换为字节数组而不将其类型转换为JPEG

3

我有一个针对Windows Phone平台编写的应用程序,可以解密图像。为了测试该应用程序,我在Windows Classic(桌面)平台上编写了一段代码,通过TCP连接与Windows Phone应用程序进行通信。现在,我在Windows Phone应用程序中有一张解密后的图像,我想验证它是否与原始图像(加密之前)相同。使用XMLSerialization我无法在TCP连接上发送Image类型;因此,我使用以下代码将其转换为字节:

BitmapImage bitmapImage = image.Source as BitmapImage;
using (MemoryStream ms = new MemoryStream())
{
    WriteableBitmap btmMap = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);

    Extensions.SaveJpeg(btmMap, ms, bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);

    bytes = ms.ToArray();
}

然而,我得到的字节与原始图像字节不匹配,因为该图像编码为Jpeg格式。即使在测试时将原始图像转换为jpeg格式,字节也不匹配。但是,当我将字节发送到WinPhone应用程序并将两者都转换为Jpeg时,它们是相等的。如何在不将图像转换为Jpeg格式的情况下获取字节?

1个回答

0
以下代码对我来说运行正常。
public static byte[] imageToByteArray(System.Drawing.Image sourceImage)
    {
        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

        sourceImage.Save(memoryStream, sourceImage.RawFormat);

        return memoryStream.ToArray();
    }

在Windows Phone平台上,Image类没有Save方法。 - Pooneh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接