在C#中截取桌面的屏幕截图

时间:2014-07-30 17:50:39

标签: c# .net

我收到错误,说运行时Save()行需要编码器参数。我不知道我应该为这个参数添加什么。有什么想法吗?

using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
{
                using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                {
                    g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                     Screen.PrimaryScreen.Bounds.Y,
                                     0, 0,
                                     bmpScreenCapture.Size,
                                     CopyPixelOperation.SourceCopy);

                    MemoryStream ms = new MemoryStream();
                    bmpScreenCapture.Save(ms, bmpScreenCapture.RawFormat);   // <---- ERROR
                    byte[] bytes = ms.GetBuffer();
                    ms.Close();
                }
}

1 个答案:

答案 0 :(得分:1)

将该行更改为

bmpScreenCapture.Save(ms, ImageFormat.Png); 

BTW:您可以使用 ImageFormat 支持的任何格式

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat(v=vs.110).aspx

相关问题