更新时间:2022-12-09 19:48:48
它的工作我是这样的:
当客户想要接收的截图,你需要传递开始前就知道图像尺寸。
结果呼叫 GetScreenShotSize()
来获得图像的大小。
一旦你获得了尺寸,叫 GetScreenShot()
来接收图像数据。
我用使用(MemoryStream的毫秒=新的MemoryStream())
所以现在PNG格式工作。
私人图片IMG = NULL;
众长GetScreenShotSize()
{
矩形范围= Screen.GetBounds(Point.Empty);使用(BMP位图=新位图(bounds.Width,bounds.Height))
{
使用
(图形G = Graphics.FromImage(BMP))
{
g.CopyFromScreen(Point.Empty,Point.Empty,bounds.Size);
}使用
(MemoryStream的毫秒=新的MemoryStream())
{
bmp.Save(MS,System.Drawing.Imaging.ImageFormat.Png);
IMG = Image.FromStream(毫秒);
返回ms.Length;
}
}
}
公共无效GetScreenShot(IPEndPoint EP)
{$ B $使用B(Socket套接字=新的Socket(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp))
{
socket.Connect(EP);
使用(MemoryStream的毫秒=新的MemoryStream())
{
img.Save(MS,System.Drawing.Imaging.ImageFormat.Png);
socket.Send(ms.ToArray(),SocketFlags.None);
}
img.Dispose();
IMG = NULL;
}
}
I have a server-client application, i want to get a Screen Shot from server,but on the line bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
i get this exception : 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);
}
}
Why is that happening and how would i fix it?
Update: It works when i use ImageFormat.Jpeg
Instead of ImageFormat.Png
, but i still need a PNG format.
it worked with me this way :
When a client wants to a receive a screenshot, you need to know image size before transferring starts.
Call GetScreenShotSize()
to get the size of the image.
Once you get the size, call GetScreenShot()
to receive image data.
i used using (MemoryStream ms = new MemoryStream())
so now PNG format is working.
private Image img = null;
public long GetScreenShotSize()
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
img = Image.FromStream(ms);
return ms.Length;
}
}
}
public void GetScreenShot(IPEndPoint ep)
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect(ep);
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
socket.Send(ms.ToArray(), SocketFlags.None);
}
img.Dispose();
img = null;
}
}