c# - A generic error occurred in GDI+ when trying to save image -


i trying save webform image using code:

using system.drawing.printing; using system.drawing; using system.drawing.imaging;           protected void btntopdf_click(object sender, eventargs e)         {              capture("d://screenshot.jpg");//path save captured files          }          public static void capture(string capturedfilepath)         {             bitmap bitmap = new bitmap           (system.windows.forms.screen.primaryscreen.bounds.width, system.windows.forms.screen.primaryscreen.bounds.height);              graphics graphics = graphics.fromimage(bitmap system.drawing.image);             graphics.copyfromscreen(25, 25, 25, 25, bitmap.size);              bitmap.save(capturedfilepath, imageformat.jpeg);//error             bitmap.dispose();             graphics.dispose();          }   

i getting a generic error occurred in gdi+.

stacktrace

[externalexception (0x80004005): generic error occurred in gdi+.]    system.drawing.image.save(string filename, imagecodecinfo encoder, encoderparameters encoderparams) +474386    system.drawing.image.save(string filename, imageformat format) +69    uno.employeeperformancecreate.capture(string capturedfilepath) in d:\project\uno\uno\employeeperformancecreate.aspx.cs:2258    uno.employeeperformancecreate.btntopdf_click(object sender, eventargs e) in d:\project\uno\uno\employeeperformancecreate.aspx.cs:2208    system.web.ui.webcontrols.button.onclick(eventargs e) +9628114    system.web.ui.webcontrols.button.raisepostbackevent(string eventargument) +103    system.web.ui.webcontrols.button.system.web.ui.ipostbackeventhandler.raisepostbackevent(string eventargument) +10    system.web.ui.page.raisepostbackevent(ipostbackeventhandler sourcecontrol, string eventargument) +13    system.web.ui.page.raisepostbackevent(namevaluecollection postdata) +35    system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +1724 

instead of saving directly bitmap path, try below:

 protected void page_load(object sender, eventargs e)     {         bitmap bmpscreenshot = screenshot();         bitmap bm;         byte[] ss = imagetobyte(bmpscreenshot);         using (var ms = new memorystream(ss))         {             bm = new bitmap(ms);             bm.save("d:\\test\\testimage.jpeg", imageformat.jpeg);         }      }     public static byte[] imagetobyte(system.drawing.image img)     {         imageconverter converter = new imageconverter();         return (byte[])converter.convertto(img, typeof(byte[]));     }     private bitmap screenshot()     {         bitmap bmpscreenshot =                 new bitmap(screen.primaryscreen.bounds.width, screen.primaryscreen.bounds.height);          using (var g = graphics.fromimage(bmpscreenshot))         {             g.copyfromscreen(0, 0, 0, 0, screen.primaryscreen.bounds.size);             return bmpscreenshot;         }      } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -