EncodeSprite()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

EncodeSprite()

Post by jacdelad »

Hello,
I wish to get the function EncodeSprite(), which converts a sprite into an image. For now, when taking a screenshot from a screen (opened with OpenScreen()) I have to grab the sprite and save it on disk, then reload it. This is suboptimal:

Code: Select all

Procedure ScreenToClipBoard()
  Protected Sprite,TempImage
  Sprite=GrabSprite(#PB_Any,0,0,ScreenWidth(),ScreenHeight())
  If Sprite
    If SaveSprite(Sprite,GetTemporaryDirectory()+"MyTempImage.bmp",#PB_ImagePlugin_BMP)
      TempImage=LoadImage(#PB_Any,GetTemporaryDirectory()+"MyTempImage.bmp",0)
      If TempImage
        SetClipboardImage(TempImage)
        FreeImage(TempImage)
      EndIf
      DeleteFile(GetTemporaryDirectory()+"MyTempImage.bmp",#PB_FileSystem_Force)
    EndIf
    FreeSprite(Sprite)
  EndIf
EndProcedure
Couldn't find an easier way, thouth it's surely possible with Windows API.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
idle
Always Here
Always Here
Posts: 5840
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: EncodeSprite()

Post by idle »

windows you only need to use bitblt

Code: Select all

Procedure ScreentoClipboard(left,top,width,Height) 
  
  Protected image,hdc,thdc
  #CAPTUREBLT = $40000000 
  
  hdc = GetDC_(0)
  
  Image = CreateImage(#PB_Any,Width-Left,Height-top) 
  If Image
    thdc = StartDrawing(ImageOutput(Image))
    BitBlt_(thdc,left,top,width,height,hdc,0,0,#SRCCOPY | #CAPTUREBLT)
    StopDrawing()  
    SetClipboardImage(Image) 
    FreeImage(Image) 
  EndIf 
  
EndProcedure  

Delay(3000) 

ScreentoClipboard(0,0,800,600) 

OpenWindow(0,0,0,800,600,"clip") 
StartDrawing(WindowOutput(0)) 
DrawImage(GetClipboardImage(0),0,0,800,600) 
StopDrawing() 
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow    
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 385
Joined: Thu Jul 09, 2015 9:07 am

Re: EncodeSprite()

Post by pf shadoko »

and with GrabDrawingImage ?
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: EncodeSprite()

Post by jacdelad »

Oh my, it's much easier than I thought:

Code: Select all

Procedure SpriteToClipboard()
  Protected rv.a,TempImage.i
  StartDrawing(ScreenOutput())
  TempImage=GrabDrawingImage(#PB_Any,0,0,ScreenWidth(),ScreenHeight())
  If TempImage
    SetClipboardImage(TempImage)
    FreeImage(TempImage)
    rv=#True
  EndIf
  StopDrawing()
  ProcedureReturn rv
EndProcedure
Please ignore me...and thanks you both!
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: EncodeSprite()

Post by Fred »

On a side note, checking the result of StartDrawing() is strongly recommanded
Post Reply