Best use of types when scaling image

G

gw7rib

I want to show an image in a window, and for this particular option, I
want to keep the aspect ratio the same but otherwise make it as big as
possible. The code I have is as follows:

myGetImageSize(image_file_name, imagewidth, imageheight);
xrat = (double) w / imagewidth;
yrat = (double) h / imageheight;
if (xrat > yrat) xrat = yrat;
myDrawImage(hdc, image_file_name, xrat * imagewidth, xrat *
imageheight);

where imagewidth, imageheight, w, h and the last two parameters to
myDrawImage are ints and xrat and yrat are doubles. This works, but on
compiling I get warnings about converting doubles to ints. Clearly
there are various ways round this, but I was wondering if there was a
particularly neat way of doing it or, if not, what you would
recommend?

Thanks for any suggestions.
Paul.
 
G

gw7rib

A cast should clear the warnings.  If you know you need to lose the
fractional part, just cast it.  If you want to round to the nearest, add
0.5 before casting to 'int'.  Something like

    myDrawImage(hdc, image_file_name, int(xrat * imagewidth),
                int(xrat * imageheight) );

or (for rounding instead of truncating):

    myDrawImage(hdc, image_file_name, int(xrat * imagewidth + 0.5),
                  int(xrat * imageheight + 0.5) );

Thanks - that clears the warnings. I take it casts are less frowned
upon than usual in this particular context?

Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,429
Latest member
JacelynKit

Latest Threads

Top