Page 1 of 1

How to use LoadImage, ResizeImage and ImageGadget...

Posted: Fri May 21, 2010 11:47 pm
by HwyStar
I know this sounds beyond simple to you experienced guys but it took me two hours to figure out how to resize an image. The reason being is that the help text and the provided example code does not provide us with a sample resize an image example.

You have to use all three elements: LoadImage, ResizeImage and ImageGadget. The first two make sense (since they are documented) but a new person will forget to declare the object using ImageGadget! They will think that LoadImage does the work of creating the object. "And" the UseJPEGImageDecoder() is also kind of important... :)

Yes, I reviewed the manual and every occurrence of ResizeImage on the forum. Don't shoot me please!

Here is simple code that new people can get their heads around:

Code: Select all

If OpenWindow(0, 100, 100, 500, 300, "PureBasic - Image")

  UseJPEGImageDecoder()

  If LoadImage(1,"C:\Image.jpg") ;Change the path to your image...
    ResizeImage(1, 300, 250)
    ImageGadget(1, 10, 10, #PB_Ignore, #PB_Ignore, ImageID(1))
  EndIf
  
  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
  
EndIf

Re: How to use LoadImage, ResizeImage and ImageGadget...

Posted: Wed Jun 09, 2010 3:01 am
by joebones
Thank you for taking the time to post this. I am new here and it really helped. It not only showed me how to load and display an image in a window, but also handled resizing. (I had figured resizing would be very difficult.)

All done in a tiny, simple, and up-to-date block of code that works. :)

Re: How to use LoadImage, ResizeImage and ImageGadget...

Posted: Wed Jun 09, 2010 11:42 am
by c4s
I your code is a bit misleading because the following does the same as you can see (and saves a new modified one):

Code: Select all

UseJPEGImageDecoder()  ; Don't put these "inside" the code!
UseJPEGImageEncoder()



Define File.s, ImageNr


File = "C:\Image.jpg"  ; Change the path to your image...

ImageNr = LoadImage(#PB_Any, File)  ; Better use #PB_Any
If ImageNr
	ResizeImage(ImageNr, ImageWidth(ImageNr) / 2, ImageHeight(ImageNr) / 2)  ; Halves width & height

	ShowLibraryViewer("Image", ImageNr)  ; Show that it's already modified
	CallDebugger

	SaveImage(ImageNr, File + "_halved.jpg", #PB_ImagePlugin_JPEG)  ; Save the new image

	FreeImage(ImageNr)  ; Get used to type this as well
	Debug "Successfully saved the new image!"
Else
	Debug "Error to load the image!"
EndIf

Re: How to use LoadImage, ResizeImage and ImageGadget...

Posted: Wed Jun 09, 2010 1:02 pm
by Kaeru Gaman
HwyStar wrote:You have to use all three elements: LoadImage, ResizeImage and ImageGadget. The first two make sense (since they are documented) but a new person will forget to declare the object using ImageGadget! They will think that LoadImage does the work of creating the object.
sorry, mate.
the LoadImage does the job of creating an Object of the type Image.

that an Image is something in Memory and nothing you necessarily see, is an understanding problem when you come from "Visual" stuff environments, but not if you are a "beginner" or come from other non-"visual" environments.