我正在寻找一种方法,该方法允许我减小从屏幕捕获的图像的大小(以字节为单位),而不是像远程桌面一样发送到另一台电脑(这实际上是我的项目目的,顺便说一句,这部分是已经完成了,我只是在压缩图像时遇到了一些麻烦)。
该程序必须运行的计算机具有 Compact Framework 2.0(Windows CE 6.0),我无法更改(@工作我们在某些机器上使用这些计算机,因此更改操作系统和框架有点困难)。
无论如何,这是我用来捕捉屏幕的方法,然后是我用来改变像素颜色深度的实际方法。
int DefaultMaxScreenDiskSize = 212500;
private bool StreamScreen()
{
Rectangle bounds = Screen.PrimaryScreen.Bounds;
IntPtr hdc = GetDC(IntPtr.Zero);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format16bppRgb555);
using (MemoryStream ms = new MemoryStream())
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
IntPtr dstHdc = graphics.GetHdc();
BitBlt(dstHdc, 0, 0, bounds.Width, bounds.Height, hdc, 0, 0, Enums.RasterOperation.SRC_COPY);
graphics.ReleaseHdc(dstHdc);
}
bitmap.Save(ms, ImageFormat.Png);
int length = ms.ToArray().Length;
if (length > DefaultMaxScreenDiskSize)
{
Bitmap nBitmap = ApplyDecreaseColourDepth(128, bitmap);
nBitmap.Save(ms, ImageFormat.Png);
}
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
currentImgHash = Convert.ToBase64String(sha1.ComputeHash(ms.ToArray()));
if (oldImgHash != currentImgHash || ForceScreenRefresh)
{
ReleaseDC(IntPtr.Zero, hdc);
if (ms.ToArray() != null)
{
while (!SendScreen(ZlibStream.CompressBuffer(ms.ToArray()))) ;
oldImgHash = currentImgHash;
ForceScreenRefresh = false;
return true;
}
}
}
return false;
}
这是我使用的另一种方法,即改变像素深度的方法。但是,它在系统上有点慢和沉重,所以我不知道如何改变它。
public Bitmap ApplyDecreaseColourDepth(int offset, Bitmap bitmapImage)
{
for (int y = 0; y < bitmapImage.Height; y++)
{
for (int x = 0; x < bitmapImage.Width; x++)
{
Color pixelColor = bitmapImage.GetPixel(x, y);
int R = Math.Max(0, (pixelColor.R + offset / 2) / offset * offset - 1);
int G = Math.Max(0, (pixelColor.G + offset / 2) / offset * offset - 1);
int B = Math.Max(0, (pixelColor.B + offset / 2) / offset * offset - 1);
bitmapImage.SetPixel(x, y, Color.FromArgb(R, G, B));
}
}
return bitmapImage;
}
知道如何更改深度像素或压缩位图吗?我试图压缩图像,但是(那是一个 .PNG)它只改变了几个字节(例如,正常大小 ms.length = 38689,压缩它 ms.length = 38489)