gpt4 book ai didi

c# - 如何在不将其类型转换为 JPEG 的情况下将图像转换为字节数组

转载 作者:行者123 更新时间:2023-11-30 18:31:52 25 4
gpt4 key购买 nike

我有一个为解密图像的 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 格式的情况下获取字节?

最佳答案

下面的代码对我来说效果很好。

public static byte[] imageToByteArray(System.Drawing.Image sourceImage)
{
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

sourceImage.Save(memoryStream, sourceImage.RawFormat);

return memoryStream.ToArray();
}

关于c# - 如何在不将其类型转换为 JPEG 的情况下将图像转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com