saving a Sprite off screen as a PNG?

Just starting out? Need help? Post your questions and find answers here.
Andy72
User
User
Posts: 11
Joined: Fri Jun 02, 2006 8:25 am
Location: New Zealand

saving a Sprite off screen as a PNG?

Post 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:
Last edited by Andy72 on Mon Jun 25, 2007 9:34 am, edited 1 time in total.
Andy72
User
User
Posts: 11
Joined: Fri Jun 02, 2006 8:25 am
Location: New Zealand

Post 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!!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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..
oh... and have a nice day.
Andy72
User
User
Posts: 11
Joined: Fri Jun 02, 2006 8:25 am
Location: New Zealand

Post 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!!
Post Reply