Page 1 of 1

EncodeSprite()

Posted: Thu Mar 28, 2024 2:49 am
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.

Re: EncodeSprite()

Posted: Thu Mar 28, 2024 5:57 am
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    

Re: EncodeSprite()

Posted: Thu Mar 28, 2024 9:13 am
by pf shadoko
and with GrabDrawingImage ?

Re: EncodeSprite()

Posted: Fri Mar 29, 2024 4:51 am
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!

Re: EncodeSprite()

Posted: Fri Mar 29, 2024 9:16 am
by Fred
On a side note, checking the result of StartDrawing() is strongly recommanded