Page 1 of 1

Save image to memory or something else?

Posted: Mon Sep 17, 2007 5:18 pm
by abc123
Is there a way to load an image file and save it to memory or other but not in a directory and load the image to an image gadget later and again later?

Posted: Mon Sep 17, 2007 5:22 pm
by srod
Open the image with ReadFile(), read the contents with ReadData() having first allocated enough memory and then, when ready, use CatchImage().

Posted: Mon Sep 17, 2007 5:35 pm
by Derek
Or LoadImage(), SetClipboardImage() and then use GetClipboardImage() to get it back, just bear in mind that any use of the clipboard will delete your image!!

Posted: Mon Sep 17, 2007 10:17 pm
by abc123
Can i have little example please as i havent come across on how to use catch image with allocated memory?

Code: Select all

ReadFile(0, "C:\1.bmp")
len  = Lof(0) 
*mem = AllocateMemory(len)
Datas = ReadData(0, *mem, len)
CloseFile(0)

OpenWindow(0, 200,200,700, 700, "")

CreateGadgetList(WindowID(0))
ButtonGadget(1, 0, 0, 700, 20, "Load")
ImageGadget(2, 0, 20, 700, 700-20, 0)

Repeat
Select WaitWindowEvent()
  Case #PB_Event_Gadget
    Select EventGadget()
      Case 1
        ;
    EndSelect
EndSelect
ForEver

Posted: Mon Sep 17, 2007 10:26 pm
by Inf0Byt3
Try this:

Code: Select all

ReadFile(0, "C:\a.bmp")
len  = Lof(0)
*mem = AllocateMemory(len)
ReadData(0, *mem, len)
CloseFile(0)

OpenWindow(0, 200,200,700, 700, "")

CreateGadgetList(WindowID(0))
ButtonGadget(1, 0, 0, 700, 20, "Load")
ImageGadget(2, 0, 20, 700, 700-20, 0)

Repeat
Event = WaitWindowEvent()
Select Event
  Case #PB_Event_Gadget
    Select EventGadget()
      Case 1
        Image = CatchImage(#PB_Any,*mem,len)
        SetGadgetState(2,ImageID(Image))
    EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
FreeImage(Image)
End

Posted: Mon Sep 17, 2007 10:50 pm
by abc123
Thanks, works perfectly!

Posted: Mon Sep 17, 2007 11:33 pm
by Inf0Byt3
Well yes but you have to free the image before loading it again because each time you press Load a new image is created from the buffer... Here's the corrected version:

Code: Select all

ReadFile(0, "C:\a.bmp")
len  = Lof(0)
*mem = AllocateMemory(len)
ReadData(0, *mem, len)
CloseFile(0)

OpenWindow(0, 200,200,700, 700, "")

CreateGadgetList(WindowID(0))
ButtonGadget(1, 0, 0, 700, 20, "Load")
ImageGadget(2, 0, 20, 700, 700-20, 0)

Repeat
Event = WaitWindowEvent()
Select Event
  Case #PB_Event_Gadget
    Select EventGadget()
      Case 1
        If IsImage(Image)
         FreeImage(Image)
        EndIf
        Image = CatchImage(#PB_Any,*mem,len)
        SetGadgetState(2,ImageID(Image))
    EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
FreeImage(Image)
End


Posted: Mon Sep 17, 2007 11:41 pm
by abc123
thanks for the help! :)

Posted: Wed Sep 19, 2007 10:15 am
by Maxus

Code: Select all

ReadFile(0, "d:\logiii.ico") 
  *mem = AllocateMemory(Lof(0))
  ReadData(0, *mem, Lof(0))
CloseFile(0) 

OpenWindow(0, 200,200,700, 700, "") 
CreateGadgetList(WindowID(0)) 
ButtonGadget(1, 0, 0, 700, 20, "Load") 
ImageGadget(2, 0, 20, 700, 700-20, 0) 

Repeat 
  Event = WaitWindowEvent() 
  Select Event 
    Case #PB_Event_Gadget 
      Select EventGadget() 
        Case 1 
          If IsImage(Image) 
            FreeImage(Image)
          Else
            Image = CatchImage(#PB_Any,*mem,MemorySize(*mem)) 
            SetGadgetState(2,ImageID(Image)) 
          EndIf 
      EndSelect 
  EndSelect 
Until Event = #PB_Event_CloseWindow 
FreeImage(Image) 
End
Little modify.

Posted: Thu Sep 20, 2007 8:06 am
by Shardik
One small hint: instead of using a direct path to a sample image or icon that only resides on the poster's hard disk, one should use an image or icon from the PureBASIC installation:

Code: Select all

ReadFile(0, #PB_Compiler_Home + "Examples\Sources\Data\Geebee2.Bmp")
or

Code: Select all

ReadFile(0, #PB_Compiler_Home + "Examples\Sources\Data\CDPlayer.Ico")
Using it this way someone copying and pasting your code doesn't have to change your code and search for an appropriate image or icon :wink: