3

我有一个为 Windows Phone 平台编写的应用程序,它可以解密图像。为了测试应用程序,我在 windows 经典(桌面)平台上有一个代码,它通过 TCP 连接与 windows phone 应用程序通信。现在我在 Windows Phone 应用程序中有一个已解密的图像,我想验证它是否与原始图像相同(加密前)。使用 XMLSerialization 我无法在 TCP 连接上发送图像类型;所以我使用以下代码将其转换为字节:

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 格式的情况下获取字节?

4

1 回答 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();
    }
于 2013-10-16T00:34:13.850 回答