
Question:
I have a server-client application, i want to get a Screen Shot from server,but on the line <br />bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
<br /> 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?
<strong>Update:</strong> It works when i use ImageFormat.Jpeg
Instead of ImageFormat.Png
, but i still need a PNG format.
You say:
<blockquote>i want to get a Screen Shot from server
</blockquote>And the error being GDI+ related suggests that the context under which this code is being run is perhaps a service context (such as IIS), and not a desktop. Is that correct? If so, what do you expect to return in a screenshot?
For the record, the following (minimal) code works fine:
var 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);
}
Both bitmap
and ms
contain the data expected. As such, I think you need to provide information on where and in what this code is running.
For me I was using the Image.Save(Stream, ImageCodecInfo, EncoderParameters)
and apparently this was causing the infamous A generic error occurred in GDI+
error.
I was trying to use EncoderParameter
to save the jpegs in 100% quality. This was working perfectly on "my machine" (doh!) and not on production.
When I used the Image.Save(Stream, ImageFormat)
instead, the error disappeared! So like an idiot I continued to use the latter although it saves them in default quality which I assume is just 50%.
Hope this info helps someone.
Answer3:I know this is an old post, but I wanted to add this information as well, in case someone like me runs across this issue in the future..
I wasn't able to save .PNG images and it because the IIS App Pool Account didn't have access to this, key.
HKEY_CLASSES_ROOT\CLSID{FAE3D380-FEA4-4623-8C75-C6B61110B681}
Which is required when you use png graphics.
Answer4:it worked with me this way :
<ul><li>When a client wants to a receive a screenshot, you need to know image size before transferring starts.
<br />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;
}
}
Answer5: