Save image to memory or something else?

Just starting out? Need help? Post your questions and find answers here.
abc123
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Apr 18, 2007 9:27 pm

Save image to memory or something else?

Post 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?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Open the image with ReadFile(), read the contents with ReadData() having first allocated enough memory and then, when ready, use CatchImage().
I may look like a mule, but I'm not a complete ass.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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!!
abc123
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Apr 18, 2007 9:27 pm

Post 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
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post 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
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
abc123
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Apr 18, 2007 9:27 pm

Post by abc123 »

Thanks, works perfectly!
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post 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

None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
abc123
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Apr 18, 2007 9:27 pm

Post by abc123 »

thanks for the help! :)
User avatar
Maxus
User
User
Posts: 71
Joined: Thu Feb 16, 2006 9:35 am
Location: Russia
Contact:

Post 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.
Sorry my English, I'm Russian
AMT Laboratory
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

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