我正在做一个项目,在这个项目中,我需要捕捉桌面屏幕,并通过互联网发送到其他客户端。为了压缩图像,我想将其转换为256色图像。我有一个256色的调色板,我用欧几里得距离来找到最近的颜色。问题是我需要以每秒10-15帧的速度发送这些图像,制作256色图像需要7秒。我想知道其他程序(如teamviewer或真正的VNC)是如何做到这一点的。
for (int y=0;y<900;y++) //loop through the height
for (int x=0;x<1600;x++) //loop through the width
for (int p=0;p<256;p++) //loop through palette colors
{
calculate Euclidean distance of each pixel for each color in pallette and
find the nearest color
** these nested loops take 7 seconds to complete
}
谢谢
OK。经过几天与许多捕获方法和颜色量化器的斗争,我终于找到了一个解决方案。现在我可以以10~14 FPS的速度发送整个桌面图像,并以20~30 FPS的速度发送桌面的更改区域。
在代码中,我使用rcravens的类来捕获屏幕和屏幕的变化。然后我将图像裁剪成10个小块。之后,我用八叉树颜色量化器把它们做成256色,感谢@Dai给我指出了这个方向。在颜色还原后,我将每个片段转换为字节数组并使用LZ4压缩它们。网络图书馆。
代码如下:
int all_count = 0;
Bitmap _desktop = null;
Bitmap _merged_bitmap = new Bitmap(1600, 900);
int _height_part_ = 0;
int _total_rows = 10;
Bitmap[] crops = null;
Bitmap[] _new_crops = null;
Stopwatch sw = new Stopwatch();
int _desktop_height = 0;
int _desktop_width = 0;
ImageManipulation.OctreeQuantizer _q ;
RLC.RemoteDesktop.ScreenCapture cap = new RLC.RemoteDesktop.ScreenCapture();
private void CaptureAndSend()
{
sw.Restart();
//cap = new RLC.RemoteDesktop.ScreenCapture();
int _left = -1, _top = -1; //Changed regions
_desktop = cap.Screen(out _left, out _top); //Capture desktop or changed region of it
if (_desktop == null) return; //if nothing has changed since last capture skip everything
_desktop_height = _desktop.Height;
_desktop_width = _desktop.Width;
// If very small part has changed since last capture skip everything
if (_desktop_height < 10 || _desktop_width < 10) return;
TotalRows(_total_rows); // Calculate the total number of rows
crops = new Bitmap[_total_rows]; // Cropped pieces of image
_new_crops = new Bitmap[_total_rows];
for (int i = 0; i < _total_rows - 1; i++) //Take whole image and split it into smaller images
crops[i] = CropRow(i);
crops[_total_rows - 1] = CropLastRow(_total_rows - 1);
Parallel.For(0, _total_rows, i =>
{
ImageManipulation.OctreeQuantizer _q = new ImageManipulation.OctreeQuantizer(255, 4); // Initialize Octree
_new_crops[i] = _q.Quantize(crops[i]);
using (MemoryStream ms=new MemoryStream())
{
_new_crops[i].Save(ms, ImageFormat.Png);
//Install-Package LZ4.net
//Compress each part and send them over network
byte[] data = Lz4Net.Lz4.CompressBytes(ms.ToArray(), Lz4Net.Lz4Mode.HighCompression);
all_count += data.Length; //Just to check the final size of image
}
});
Console.WriteLine(String.Format("{0:0.0} FPS , {1} seconds , size {2} kb", 1.0 / sw.Elapsed.TotalSeconds, sw.Elapsed.TotalSeconds.ToString(), all_count / 1024));
all_count = 0;
}
private void TotalRows(int parts)
{
_height_part_ = _desktop_height / parts;
}
private Bitmap CropRow(int row)
{
return Crop(_desktop, new Rectangle(0, row * _height_part_, _desktop_width, _height_part_));
}
private Bitmap CropLastRow(int row)
{
return Crop(_desktop, new Rectangle(0, row * _height_part_, _desktop_width, _desktop_height - (row * _height_part_)));
}
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
private unsafe static extern int memcpy(byte* dest, byte* src, long count);
private unsafe Bitmap Crop(Bitmap srcImg, Rectangle rectangle)
{
if ((srcImg.Width == rectangle.Width) && (srcImg.Height == rectangle.Height))
return srcImg;
var srcImgBitmapData = srcImg.LockBits(new Rectangle(0, 0, srcImg.Width, srcImg.Height), ImageLockMode.ReadOnly, srcImg.PixelFormat);
var bpp = srcImgBitmapData.Stride / srcImgBitmapData.Width; // 3 or 4
var srcPtr = (byte*)srcImgBitmapData.Scan0.ToPointer() + rectangle.Y * srcImgBitmapData.Stride + rectangle.X * bpp;
var srcStride = srcImgBitmapData.Stride;
var dstImg = new Bitmap(rectangle.Width, rectangle.Height, srcImg.PixelFormat);
var dstImgBitmapData = dstImg.LockBits(new Rectangle(0, 0, dstImg.Width, dstImg.Height), ImageLockMode.WriteOnly, dstImg.PixelFormat);
var dstPtr = (byte*)dstImgBitmapData.Scan0.ToPointer();
var dstStride = dstImgBitmapData.Stride;
for (int y = 0; y < rectangle.Height; y++)
{
memcpy(dstPtr, srcPtr, dstStride);
srcPtr += srcStride;
dstPtr += dstStride;
}
srcImg.UnlockBits(srcImgBitmapData);
dstImg.UnlockBits(dstImgBitmapData);
return dstImg;
}
我知道我的代码不是内存有效的。如果有人能帮我优化这段代码,我将不胜感激。再次感谢我的朋友A. Abramov, Dai, HansPassant, TaW和其他人。
EDIT 2:我完全删除了我的旧帖子,因为它不相关!你说的256色,我以为你指的是256位——而你说的是256字节!我把原始坐标(900 x 900
)代入计算器,然后乘以256表示颜色。结果是20,7360,000
位,大致是2.5 MB
位。压缩后,它可以达到1 MB
-而位颜色等效(除以8)将是300 KB
基数,压缩后会小得多。解决办法很简单——拍这样一张照片确实需要这么长时间。你所说的大多数应用,比如teamviewer,都有较低的FPS。较低的图像质量,基于计算机性能。因此,我很抱歉,但解决方案是,用像你这样的电脑,可能不可能在你要求的时间内完成。
编辑3: Hans在你的问题下面的评论中做了数学计算——我们说的是22 GB
。这不是一台普通电脑的正常工作。这不是不可能的,但从2015年开始,家用电脑在一秒钟内处理这么多数据是很不常见的。