How to use LoadImage, ResizeImage and ImageGadget...

Just starting out? Need help? Post your questions and find answers here.
User avatar
HwyStar
Enthusiast
Enthusiast
Posts: 101
Joined: Mon Apr 05, 2010 7:13 pm
Location: Reno, Nevada

How to use LoadImage, ResizeImage and ImageGadget...

Post 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
joebones
New User
New User
Posts: 7
Joined: Sun May 30, 2010 11:02 am

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

Post 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. :)
Last edited by joebones on Thu Jun 17, 2010 1:44 am, edited 1 time in total.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

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

Post 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
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

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

Post 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.
oh... and have a nice day.
Post Reply