我通过这种方式提供SharpAvi屏幕截图位图,以将其保存为MJPEG:
using (var BMP = new Bitmap(Params.Width, Params.Height))
{
using (var g = Graphics.FromImage(BMP))
{
g.CopyFromScreen(Point.Empty, Point.Empty, new Size(Params.Width, Params.Height), CopyPixelOperation.SourceCopy);
g.Flush();
var bits = BMP.LockBits(new Rectangle(0, 0, Params.Width, Params.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
Marshal.Copy(bits.Scan0, buffer, 0, buffer.Length);
BMP.UnlockBits(bits);
}
}
但是,这导致4k视频具有正确的颜色,这是很大的,所以我缩小了位图:
Bitmap bmp_with_actual_resolution = new Bitmap(Params.WidthSource, Params.HeightSource);
Graphics g = Graphics.FromImage(bmp_with_actual_resolution);
g.CopyFromScreen(0, 0, 0, 0, new Size(Params.WidthSource, Params.HeightSource));
Bitmap bmp_800_600 = new Bitmap(Params.TargetWidth, Params.TargetHeight, PixelFormat.Format32bppRgb);
Graphics g_800_600 = Graphics.FromImage(bmp_800_600);
g_800_600.DrawImage(bmp_with_actual_resolution, 0, 0, Params.TargetWidth, Params.TargetHeight);
using (var memoryStream = new MemoryStream())
{
bmp_800_600.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
return buffer = memoryStream.ToArray();
}
但这会导致绿色视频垂直翻转: