Paste Image

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Paste Image

Post by spacebuddy »

Is there any way to paste an image into another image in PB?

I know grabImage creates a new image, but I need to paste several small images
into a big image. I could not find any call to do this in the help file.

Thanks.
User avatar
TI-994A
Addict
Addict
Posts: 2791
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Paste Image

Post by TI-994A »

spacebuddy wrote:...I need to paste several small images into a big image.
Hi spacebuddy! I believe that the DrawImage() function is what you're looking for. Here's an example:

Code: Select all

EnableExplicit

UseJPEGImageDecoder()
UsePNGImageDecoder()

Enumeration
  #MainWindow
  #ImageDisplay
  #image1
  #image2
EndEnumeration

Define wFlags.i, imageFile.s

imageFile = OpenFileRequester("Select Main Image:", "", "JPG|*.jpg|BMP|*.bmp|PNG|*.png", 0)
If imageFile And LoadImage(#image1, imageFile)
  imageFile = ""
  imageFile = OpenFileRequester("Select Smaller Image:", "", "JPG|*.jpg|BMP|*.bmp|PNG|*.png", 0)
  If imageFile And LoadImage(#image2, imageFile)
    wFlags = #PB_Window_SizeGadget | #PB_Window_ScreenCentered
    wFlags = wFlags | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
    OpenWindow(#MainWindow, #PB_Any, #PB_Any, 640, 480, "DrawImage", wFlags)
    ImageGadget(#ImageDisplay, 0, 0, 640, 480, 0)
    If StartDrawing(ImageOutput(#image1))
      DrawImage(ImageID(#image2), 30, 30, ImageWidth(#image2), ImageHeight(#image2))
      StopDrawing()
    EndIf
    SetGadgetState(#ImageDisplay, ImageID(#image1))
  Else
    End
  EndIf
Else
  End
EndIf
  
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Hope it helps!
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: Paste Image

Post by spacebuddy »

yes, that works thanks. :D

When I save the image as a .png it is not transparent.

How do you make it transparent?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Paste Image

Post by wilbert »

spacebuddy wrote:How do you make it transparent?
Was the image you draw onto created transparent ?
Like

Code: Select all

CreateImage(0, 255, 255, 32 | #PB_Image_Transparent)
User avatar
TI-994A
Addict
Addict
Posts: 2791
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Paste Image

Post by TI-994A »

spacebuddy wrote:When I save the image as a .png it is not transparent.
Hello again spacebuddy. If your original image already has transparency, saving it using the UsePNGImageEncoder() will preserve the transparency, even if you use DrawImage() to place another non-transparent image onto it. I've modified the example to save the resultant image as a PNG file:

Code: Select all

EnableExplicit

UseJPEGImageDecoder()
UsePNGImageDecoder()
UsePNGImageEncoder()

Enumeration
  #MainWindow
  #ImageDisplay
  #image1
  #image2
EndEnumeration

Define wFlags.i, imageFile.s

imageFile = OpenFileRequester("Select Main Image:", "", "JPG|*.jpg|BMP|*.bmp|PNG|*.png", 0)
If imageFile And LoadImage(#image1, imageFile)
  imageFile = ""
  imageFile = OpenFileRequester("Select Smaller Image:", "", "JPG|*.jpg|BMP|*.bmp|PNG|*.png", 0)
  If imageFile And LoadImage(#image2, imageFile)
    wFlags = #PB_Window_SizeGadget | #PB_Window_ScreenCentered
    wFlags = wFlags | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
    OpenWindow(#MainWindow, #PB_Any, #PB_Any, 640, 480, "DrawImage", wFlags)
    ImageGadget(#ImageDisplay, 0, 0, 640, 480, 0)
    If StartDrawing(ImageOutput(#image1))
      DrawImage(ImageID(#image2), 30, 30, ImageWidth(#image2), ImageHeight(#image2))
      StopDrawing()
    EndIf
    SetGadgetState(#ImageDisplay, ImageID(#image1))
    imageFile = SaveFileRequester("Enter/Select Filename:", "C:\savedImage.png", "PNG|*.png", 0)
    SaveImage(#image1, imageFile, #PB_ImagePlugin_PNG)
  Else
    End
  EndIf
Else
  End
EndIf
  
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: Paste Image

Post by spacebuddy »

Thank you all, got it working :D
Post Reply