我目前有一个应用程序可以截取演示者桌面的屏幕截图,然后通过自定义协议(protocol)将其广播给观众。为了使图像传输速度足够快以达到每秒 2 - 3 张图像的帧速率,我需要确保图像大小始终小于 ~ 300 KB。
我将 C# 用于演示应用程序,它通过以下过程将屏幕截图编码为 JPEG。我担心的是,使用静态压缩设置时图像质量可能会有很大差异。如果我的应用程序捕获我的屏幕,当我使用 Visual Studio 全屏时图像输出将约为 200 KB,但如果我最小化我的屏幕并显示我的桌面背景,它将约为 400 KB。
我可以将编码过程放入循环中,并不断减小图像大小,直到字节数组的大小小于 300 KB,但这似乎是一个乏味的操作。我可以使用其他方法吗?
提前致谢。
// get the screenshot
System.Drawing.Rectangle totalSize = System.Drawing.Rectangle.Empty;
//foreach (Screen s in Screen.AllScreens)
totalSize = System.Drawing.Rectangle.Union(totalSize, Screen.PrimaryScreen.Bounds);
Bitmap screenShotBitmap = new Bitmap(totalSize.Width, totalSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
screenShotBitmap.SetResolution(96, 96);
Graphics screenShotGraphics = Graphics.FromImage(screenShotBitmap);
screenShotGraphics.CopyFromScreen(totalSize.X, totalSize.Y,
0, 0, totalSize.Size, CopyPixelOperation.SourceCopy);
screenShotGraphics.Dispose();
// image codec information
ImageCodecInfo imageCodecInfo = GetEncoderInfo("image/jpeg");
// encoder settings
System.Drawing.Imaging.Encoder encoderQuality;
System.Drawing.Imaging.Encoder encoderColor;
encoderQuality = System.Drawing.Imaging.Encoder.Quality;
encoderColor = System.Drawing.Imaging.Encoder.ColorDepth;
// compression & quality for JPEG output
Int64 quality = 40L;
// storage for exported JPEG
byte[] screenShotByteArray;
// encoder parameters
EncoderParameter encoderQualityParameter = new EncoderParameter(encoderQuality, quality);
//EncoderParameter encoderColorParameter = new EncoderParameter(encoderColor, 8L);
// encoder parameters table
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = encoderQualityParameter;
//encoderParameters.Param[1] = encoderColorParameter;
// get the code into a memory stream
MemoryStream screenShotMemoryStream = new MemoryStream();
screenShotBitmap.Save(screenShotMemoryStream, imageCodecInfo, encoderParameters);
// convert to a byte array
screenShotByteArray = screenShotMemoryStream.GetBuffer();
// close the memory stream
screenShotMemoryStream.Close();
最佳答案
如果您将事物放入循环中,请小心使用类似于二分查找的方法,而不是仅仅将质量参数增加/减少固定数量,直到达到所需的大小。
编辑:稍微解释一下二分查找。假设图片压缩到 quality*10000
字节,因此最佳质量设置为 30。现在天真的方法是尝试一些固定的质量设置(例如 80,这将提供 800,000字节),然后减少一定数量,直到达到 300000 字节。如果你觉得在每个步骤中将图像质量降低 5,您将使用此方法尝试 12 种质量设置,直到找到所需的设置。 binary search会更快地给出结果,像这样:
Quality Size Next step
80 800000 Too big, so quality := quality/2
40 400000 Too big, so quality := quality/2
20 200000 Too small, so quality := (40+20)/2
30 300000 Reached desired size
这仅在 4 次尝试后给出了结果(或 3 次,取决于 200000 字节太小或对您来说刚刚好)。由于大小与质量没有线性关系,这个例子有点不切实际,但二分搜索应该仍然比朴素的方法给你更好的结果。
您还可以使用一些典型图像进行“训练”。使用不同的质量设置(例如 100,90,...,20,10)对它们进行编码,并查看它们相对于原始大小有多大。在大多数情况下,这可能会给出一个很好的初步估计,但在遇到细节或多或少的图像时仍然需要进行调整。
或者,看看 JPEG2000 编码器,它们可以选择设置文件大小而不是质量。
编辑:我不知道用于 C# 的 JPEG2000 编码库,似乎只有解码器在 float ,所以这可能比我最初想象的要复杂。你可以给 CSJ2K尝试一下,但描述听起来不像是随时可用的。
关于c# - 确保导出的 JPEG 小于最大文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5970495/