c# - Generic error occurred in GDI+ while setting wallpaper twice -
i have code should set wallpaper
byte[] data = webclient.downloaddata("http://imageurl"); image yourimage = null; using (memorystream mem = new memorystream(data)) { yourimage = null; yourimage = image.fromstream(mem); yourimage.save("image.jpg", imageformat.jpeg); wallpaper.set(new uri("image.jpg", urikind.relative), wallpaper.style.stretched); yourimage.dispose(); }
whenever run twice, stops @ yourimage.save("image.jpg", imageformat.jpeg);
saying was
system.runtime.interopservices.externalexception unhandled hresult=-2147467259 message=allgemeiner fehler in gdi+. source=system.drawing errorcode=-2147467259 stacktrace: bei system.drawing.image.save(string filename, imagecodecinfo encoder, encoderparameters encoderparams) bei system.drawing.image.save(string filename, imageformat format) bei streetviewhintergrundbild.form1.button1_click(object sender, eventargs e) in d:\form1.cs:zeile 70. bei system.windows.forms.control.onclick(eventargs e) bei system.windows.forms.button.onclick(eventargs e) bei system.windows.forms.button.onmouseup(mouseeventargs mevent) bei system.windows.forms.control.wmmouseup(message& m, mousebuttons button, int32 clicks) bei system.windows.forms.control.wndproc(message& m) bei system.windows.forms.buttonbase.wndproc(message& m) bei system.windows.forms.button.wndproc(message& m) bei system.windows.forms.control.controlnativewindow.onmessage(message& m) bei system.windows.forms.control.controlnativewindow.wndproc(message& m) bei system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam) bei system.windows.forms.unsafenativemethods.dispatchmessagew(msg& msg) bei system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(intptr dwcomponentid, int32 reason, int32 pvloopdata) bei system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context) bei system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context) bei system.windows.forms.application.run(form mainform) bei streetviewhintergrundbild.program.main() in d:\program.cs:zeile 19. bei system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) bei system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) bei microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() bei system.threading.threadhelper.threadstart_context(object state) bei system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) bei system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) bei system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) bei system.threading.threadhelper.threadstart() innerexception:
wallpaper.cs:
using microsoft.win32; using system; using system.collections.generic; using system.io; using system.linq; using system.runtime.interopservices; using system.text; using system.threading.tasks; namespace ns { public sealed class wallpaper { wallpaper() { } const int spi_setdeskwallpaper = 20; const int spif_updateinifile = 0x01; const int spif_sendwininichange = 0x02; [dllimport("user32.dll", charset = charset.auto)] static extern int systemparametersinfo(int uaction, int uparam, string lpvparam, int fuwinini); public enum style : int { tiled, centered, stretched } public static void set(uri uri, style style) { system.io.stream s = new system.net.webclient().openread(uri.tostring()); system.drawing.image img = system.drawing.image.fromstream(s); string temppath = path.combine(path.gettemppath(), "wallpaper.bmp"); img.save(temppath, system.drawing.imaging.imageformat.bmp); registrykey key = registry.currentuser.opensubkey(@"control panel\desktop", true); if (style == style.stretched) { key.setvalue(@"wallpaperstyle", 2.tostring()); key.setvalue(@"tilewallpaper", 0.tostring()); } if (style == style.centered) { key.setvalue(@"wallpaperstyle", 1.tostring()); key.setvalue(@"tilewallpaper", 0.tostring()); } if (style == style.tiled) { key.setvalue(@"wallpaperstyle", 1.tostring()); key.setvalue(@"tilewallpaper", 1.tostring()); } systemparametersinfo(spi_setdeskwallpaper, 0, temppath, spif_updateinifile | spif_sendwininichange); } } }
Comments
Post a Comment