Image.Save exception “A generic error occurred in GDI+.” when saving to MemoryStream


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:


i want to get a Screen Shot from server


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.


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.


Which is required when you use png graphics.


it worked with me this way :


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.

</li> <li>

Once you get the size, call GetScreenShot() to receive image data.

</li> </ul>

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; } }


abrirConexion(); cmd = new SqlCommand(" INSERT INTO [SICRE_BI].[Credito_CCSS].[bi_img](Descripcion,img) VALUES (@nom_img,@img)",cn); cmd.Parameters.Add("@Descripcion", SqlDbType.NChar); cmd.Parameters.Add("@img", SqlDbType.Image); cmd.Parameters["@Descripcion"].Value = descripcion; System.IO.MemoryStream ms = new System.IO.MemoryStream(); pbImagen.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//Aqui se me cae.Aqui se me cae.Aqui . cmd.Parameters["@img"].Value = ms.GetBuffer(); //ms.GetBuffer(); cmd.ExecuteNonQuery(); return true;


  • Error:1722 Getting session names
  • Wcf WS-Security server
  • Invalid response while accessing the Selenium Server
  • Web API Basic Auth inside an MVC app with Identity Auth
  • Gradle dynamic flavor
  • SiteMesh: Changing the content-type of the response
  • Disadvantages to high make job values
  • const char **a = {“string1”,“string2”} and pointer arithametic
  • Matplotlib save file as JPEG encoder error
  • Read the values from XML file
  • MVVM: Image Bind Source from FileOpenPicker
  • Floated image with variable width and heading with background image
  • Elasticsearch script query involving root and nested values
  • Why cepheus don't send int without quotes to orion?
  • Can't delete or rename original file after resizing
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • How to have background script and something similar to a default popup?
  • Python urlparse: small issue
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Javascript Callbacks with Object constructor
  • ILMerge & Keep Assembly Name
  • Perl system calls when running as another user using sudo
  • Font Awesome Showing Box instead of Icons
  • Large data - storage and query
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • WOWZA + RTMP + HTML5 Playback?
  • GridView Sorting works once only
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Acquiring multiple attributes from .xml file in c#
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Cant find why the layout is getting smaller
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal