Grab desktop wallpaper to image?

Just starting out? Need help? Post your questions and find answers here.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: Grab desktop wallpaper to image?

Post by RASHAD »

Using Danilo & NM code with the simplicity of RASHAD :P
Tested with 7,8 & 10

Code: Select all

Procedure LoadPictureFile(image,File.s) 
  hFile = CreateFile_(File, #GENERIC_READ, 0, #Null, #OPEN_EXISTING, 0, #Null) 
  If hFile 
    dwFileSize = GetFileSize_(hFile, #Null) 
    HGLOBAL    = GlobalAlloc_(#GMEM_MOVEABLE, dwFileSize) 
    If HGLOBAL 
      pvData = GlobalLock_(HGLOBAL)
      bRead = ReadFile_(hFile, pvData, dwFileSize, @dwBytesRead, #Null) 
      GlobalUnlock_(HGLOBAL)      
      If bRead 
        If CreateStreamOnHGlobal_(HGLOBAL, #True, @pstm.IStream) = #S_OK 
          If OleLoadPicture_(pstm, dwFileSize, #False,?IID_IPicture, @Bild.IPicture) = #S_OK 
            Bild\get_Height(@Height) 
            Bild\get_Width(@Width)             
            ExamineDesktops()
            result = CreateImage(image,DesktopWidth(0),DesktopHeight(0))            
            If result
              hDC = StartDrawing(ImageOutput(image)) 
                Bild\Render(hDC,0,DesktopHeight(0),DesktopWidth(0),-DesktopHeight(0),0,0,Width,Height,0) 
              StopDrawing() 
            EndIf           
            Bild\Release() 
          EndIf 
          pstm\Release() 
        EndIf 
      EndIf 
    EndIf 
    CloseHandle_(hFile) 
  EndIf 
  ProcedureReturn result
EndProcedure 

img$ = Space(#MAX_PATH)
SystemParametersInfo_(#SPI_GETDESKWALLPAPER, #MAX_PATH, @img$, 0)
image = LoadPictureFile(1,img$) 

SaveImage(1,GetHomeDirectory()+"test.bmp",#PB_ImagePlugin_BMP)

DataSection 
  IID_IPicture: 
    Data.l $7BF80980 
    Data.w $BF32,$101A 
    Data.b $8B,$BB,$00,$AA,$00,$30,$0C,$AB 
EndDataSection 

Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Grab desktop wallpaper to image?

Post by Dude »

I get this:

Code: Select all

Line: 37 - The specified #Image is not initialised.
Reason: there's a desktop image shown, but no file for it. That's why I can't rely on loading a file to get the image.

It's okay - I'll just toggle the desktop icons before snapshotting the image. Don't worry about it anymore. :)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Grab desktop wallpaper to image?

Post by netmaestro »

@RASHAD: Nice implementation, works perfectly here. Thanks for the post :mrgreen:
BERESHEIT
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Grab desktop wallpaper to image?

Post by Dude »

netmaestro wrote:works perfectly here
Yep, because the wallpaper file still exists for you. ;) It doesn't for me, so I can't use his solution.
Post Reply