Good evening all,
Started a little project becuase i was bored. :)
I want to change the wallpaper of the desktop in code. I keep running into a GDI+ error:
A generic error occurred in GDI+.
Love MSs error msgs :)
varibles end up with these values:
strFileToLoad = "C:\My Docs\My Photos\Ladies\tombraider2_1024x768.jpg"
strWallpaperImageIs = "C:\Program Files\My Apps\WolfHound Applications\WallpaperMaster\bin\Debug\Wallpaper\Current_Wallpaper.bmp"
Heres my code:
Imports
System.Runtime.InteropServices 'For the wallpaper changing API interface
Imports System.io
'Wallpaper changing API
Public Shared SPI_SETDESKTOPWALLPAPER As Integer = 20
Public Shared SPIF_UPDATEINIFILE As Integer = 1
Public Shared SPIF_SENDWININICHANGE As Integer = 2
Dim
strFileToLoad As String = Directory.GetFiles(txtBoxPhotoFolder.Text, "*.jpg").GetValue(intPhotoOn)
Dim objImage As New System.Windows.Forms.PictureBox
With objImage
.ImageLocation = strFileToLoad
.Load()
End With
strFileToLoad = StrReverse(strFileToLoad)
strFileToLoad = strFileToLoad.Substring(0, (strFileToLoad.IndexOf(
"\")))
strFileToLoad = StrReverse(strFileToLoad)
txtBoxCurrentWP.Text = strFileToLoad.Substring(0, (strFileToLoad.IndexOf(
".")))
Dim strWallpaperImageIs As String = (My.Application.Info.DirectoryPath & "\Wallpaper\Current_Wallpaper.bmp")
If (File.Exists(strWallpaperImageIs) = True) Then
File.Delete(strWallpaperImageIs)
End If
With objImage
.Image.Save(strWallpaperImageIs, System.Drawing.Imaging.ImageFormat.Bmp)
End With
objImage = Nothing
'Change the wall paper!
SystemParametersInfo(SPI_SETDESKTOPWALLPAPER, 0, strWallpaperImageIs, SPIF_SENDWININICHANGE)
The error happens at:
.Image.Save(strWallpaperImageIs, System.Drawing.Imaging.ImageFormat.Bmp)
I load file from different location to where I am saving and use different filename so locking, as far as I can see should not be a problem. i am at a lost.
Would appreciate all the help you can give on this one.
Thanks
Deasun