I believe you may need to do some P/Invoking as there is no direct way of doing it in .NET
take a look at this, but its in C#
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=728979&SiteID=1
Conversion to VB.NET, by sure to import the System.Runtime.Reflection namespace:
<DllImport("user32.dll", SetLastError=true)> private shared function SystemParametersInfo (Byval uAction as Integer, ByVal uParam as Integer, ByVal szImagePath as String, ByVal fuWinIni as Integer) as Integer End Function .. .. public function ChangeWallpaper (ByVal path as String) as Boolean const SPI_SETDESKWALLPAPER as Integer = &h14 const SPIF_UPDATEINIFILE As Integer = &H1 'will make your change permanent in the user profile const SPIF_SENDCHANGE as Integer = &H2 ' will inform all the windows that something has changed Dim result as Integer = SystemParametersInfo (SPI_DESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE or SPIF_SENDCHANGE) return Convert.ToBoolean(result) 'Not sure about this end function |
does this help?
Imports System.Runtime.InteropServices
Module Module1
Private Declare Auto Function SystemParametersInfo Lib "user32.dll" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal szImagePath As String, ByVal fuWinIni As Integer) As Boolean
Private Const SPI_SETDESKWALLPAPER As Integer = &H14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDCHANGE As Integer = &H2
Public Function ChangeWallpaper(ByVal path As String) As Boolean
Return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE)
End Function
End Module
jpeg and bitmaps are the supported one's I believe - what happens when you try to give it a jpeg/other file/image formats? the code I posted is the code used for this which NoBugz has kindly corrected/made more accurate.
Windows doesn't like to deal with .jpeg files, just .bmp files.
The shell can, it offers the IActiveDesktop::SetWallpaper()
method. Unfortunately, the COM component that implements this
method (shell32.dll) doesn't export the IActiveDesktop interface so you
don't have a declaration available for it to use in your program.
That is what makes dealing with the shell so difficult in .NET, we need
a Primary Interop Assembly for the shell but none is available
(AFAIK). I hope MSFT is paying attention here.
A workaround for you is to convert the .jpeg file to a .bmp file yourself. Here's code to do this:
Imports System.Runtime.InteropServices
Imports System.Drawing
Module Module1
Private Declare Auto Function SystemParametersInfo Lib
"user32.dll" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal
szImagePath As String, ByVal fuWinIni As Integer) As Boolean
Private Const SPI_SETDESKWALLPAPER As Integer = &H14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDCHANGE As Integer = &H2
Public Function ChangeWallpaper(ByVal path As String) As Boolean
path = ConvertBitmap(path)
Return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE)
End Function
Private Function ConvertBitmap(ByVal srce As String) As String
' Convert image file <srce> to a bitmap file, stored in My Pictures
Dim sbmp As New Bitmap(srce)
Dim dbmp As New Bitmap(sbmp.Width, sbmp.Height, Imaging.PixelFormat.Format24bppRgb)
Dim gr As Graphics = Graphics.FromImage(dbmp)
gr.DrawImage(sbmp, 0, 0)
Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
path += "\wallpaper.bmp"
dbmp.Save(path, Imaging.ImageFormat.Bmp)
sbmp.Dispose()
dbmp.Dispose()
Return path
End Function
End Module