保持图像质量

时间:2013-11-23 20:02:43

标签: c# image-processing

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

我有一连串的jpeg进来。 我将当前图像与之前的图像进行比较。 通过使用第三方工具 - 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.

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

enter image description here

我现在尝试使用来自@MagnatLU的类型建议的编码器,如果我使用FreeImage.Net,我也会得到相同质量的图像。

1 个答案:

答案 0 :(得分:5)

您可以在将文件编码为值时设置JPEG compression level,这是质量和尺寸之间最佳的经验权衡。

相关问题