我有一个服务器-客户端应用程序,我想从服务器获取屏幕截图,但是在线上bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
我得到了这个异常:A generic error occurred in GDI+.
private Socket ScreenSocket;
private MemoryStream ms;
public void ConnectScreenShot(IPEndPoint ep)
{
if (ScreenSocket != null)
{
ScreenSocket.Dispose();
ScreenSocket = null;
}
if (ms != null)
{
ms.Dispose();
ms = null;
}
ScreenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ScreenSocket.Connect(ep);
ms = new MemoryStream();
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bitmap.Size);
}
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
}
}
为什么会发生这种情况,我将如何解决?
更新:当我使用ImageFormat.Jpeg
而不是时它可以工作ImageFormat.Png
,但我仍然需要 PNG 格式。