I am writing a program in which the user can type the name of an image, for example "image.jpg". I am trying to devise a function that gets that image, and without cropping it, resizes it in a manner that allows it to fit in a Rectangle shape of 470 x 410 pixels. Would anyone happen to know how to obtain the numerical values of the size of the image and/or resize the image so that it fits inside that Rectangle?
Asked
Active
Viewed 1,622 times
1 Answers
1
There's an example program in the FLTK documentation called pixmap_browser.cpp. On my Linux system, I found it under /usr/share/doc/fltk-1.3.2/examples
Here's the essence of the code you're looking for:
#include <FL/Fl_Shared_Image.H>
// ...
// Load the image file:
Fl_Shared_Image *img = Fl_Shared_Image::get(filename);
// Or die:
if (!img) {
return;
}
// Resize the image if it's too big, by replacing it with a resized copy:
if (img->w() > box->w() || img->h() > box->h()) {
Fl_Image *temp;
if (img->w() > img->h()) {
temp = img->copy(box->w(), box->h() * img->h() / img->w());
} else {
temp = img->copy(box->w() * img->w() / img->h(), box->h());
}
img->release();
img = (Fl_Shared_Image *) temp;
}
The method that does the resizing is Fl_Image::copy. Basically, the code replaces the source image with a resized copy if it's too big.

Daniel Hanrahan
- 4,801
- 7
- 31
- 44