gpt4 book ai didi

c# - 保持图像质量

转载 作者:太空狗 更新时间:2023-10-30 01:05:03 24 4
gpt4 key购买 nike

我有一个 winform C# 桌面应用程序。

我不断收到 jpeg 文件。我正在将当前图像与之前的 1 进行比较。通过使用第 3 方工具 - Emgu - 我可以创建一个仅包含差异的新图像。

然后我将该图像转换为内存流,然后再转换为字节数组。

在接收应用程序中,我使用这个字节数组并使用这些字节通过内存流加载图像。

问题是图像质量下降了很多。

如果在客户端将图像转换为内存流之前将图像保存到硬盘驱动器,则图像质量很好。

问题出在我将它作为内存流加载时。

我将其编码为 jpeg。
如果我在将其发送到服务器之前将其编码为 PNG,那么质量又会很好。将字节数组中的大小编码为 PNG 的问题会激增。

我的意图一直是减少我必须上传的字节数以缩短响应时间。

我是做错了什么还是不能这样做?

这是我的代码:

Bitmap imgContainingDifference
= GetDiffFromEmgu(CurrentJpegImage, PreviousJpegImage);

using (System.IO.MemoryStream msIn = new System.IO.MemoryStream())
{
holding.Save(msIn, System.Drawing.Imaging.ImageFormat.Jpeg);
data = msIn.ToArray();
}

//test here
using (System.IO.MemoryStream msOut = new System.IO.MemoryStream(_data))
{
Bitmap testIMG = (Bitmap)Image.FromStream(msOut);
}

//result is image is poor/degrades

如果我这样做:

using (System.IO.MemoryStream msIn = new System.IO.MemoryStream())
{
holding.Save(msIn, System.Drawing.Imaging.ImageFormat.Png);
data = msIn.ToArray();
}

using (System.IO.MemoryStream msOut = new System.IO.MemoryStream(_data))
{
Bitmap testIMG = (Bitmap)Image.FromStream(msOut);
}

//Image is good BUT the size of the byte array is
//10 times the size of the CurrentFrame right at the start.

这是使用以下 child 建议时图像的样子:

enter image description here

我现在已经尝试使用来自@MagnatLU 的友好建议的编码器,如果我使用 FreeImage.Net,我也可以获得相同质量的图像。

最佳答案

你可以设置JPEG compression level将文件编码为质量和大小之间最佳经验权衡的值。

关于c# - 保持图像质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20167206/

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