Page 1 of 1

saving a Sprite off screen as a PNG?

Posted: Mon Jun 25, 2007 4:36 am
by Andy72
Ok, I'm finding out I know very little about grabbing images and saving them, using code I found on the forums (thanks guys :D ), I can grab a sprite and display it in an image gadget, but I cant for the life of me figure out how to save it to a PNG, heres the basics of what I've got...

Code: Select all

If UsePNGImageDecoder() And UsePNGImageEncoder() And InitSprite()

OpenWindow(1, 0, 0, 1024, 768, "Test",#PB_Window_BorderLess)
    OpenWindowedScreen(WindowID(1), 0, 0, 1024, 768,0,0,0)
    
Procedure CreateBitmapFromSprite(Sprite)
  hDC=StartDrawing(SpriteOutput(Sprite))
  bmp.BITMAP\bmWidth=SpriteWidth(Sprite)
  bmp\bmHeight=SpriteHeight(Sprite)
  bmp\bmPlanes=1
  bmp\bmBitsPixel=GetDeviceCaps_(hDC,#BITSPIXEL)
  bmp\bmBits=DrawingBuffer()
  bmp\bmWidthBytes=DrawingBufferPitch()
  hBmp=CreateBitmapIndirect_(bmp)
  StopDrawing()
  ProcedureReturn hbmp
EndProcedure


Procedure SendAnswer()
    GrabSprite(50,40,40,709,650)
    ImageId=CreateBitmapFromSprite(50)
    SaveImage(ImageId, "answer.png", #PB_ImagePlugin_PNG)
    FreeSprite(50)
EndProcedure

endif
now I know to to save it has to be an image number Not an ImageID, is there any way around this?

Thanks in advance :wink:

Posted: Mon Jun 25, 2007 5:09 am
by Andy72
This seems to work nicely but is there a better way?

Code: Select all

Procedure SendAnswer()

    GrabSprite(50,40,40,709,650)
    ImageID1=CreateBitmapFromSprite(50)
    ImageID2=CreateImage(51,709, 650)
    source_image = CreateCompatibleDC_(main_dc) 
    SelectObject_(source_image,ImageID1)
    dest_image = CreateCompatibleDC_(main_dc)
    SelectObject_(dest_Image,ImageID2)
    BitBlt_(dest_image,0,0,709,650,source_image,0,0,#SRCCOPY)
    SaveImage(51, "answer.png", #PB_ImagePlugin_PNG)
    FreeSprite(50)
EndProcedure
sometimes you shouldn't program tired and hungry!!

Posted: Mon Jun 25, 2007 7:51 am
by Kaeru Gaman
have a look into the Help, command SaveSprite()

...you don't need to rape your code converting it into an image first via API..

Posted: Mon Jun 25, 2007 8:22 am
by Andy72
I should take more time reading the help files I think!!!!!!!!, boy do I feel silly now :oops:

Thanks for the reply, think I'll use that insted!!