wxBitmap scaling

909 views
Skip to first unread message

Steve Cookson

unread,
Sep 6, 2009, 8:19:20 PM9/6/09
to wx-u...@googlegroups.com

Hi,

I'm trying to display a thumbnail of a bitmap I'm using, but I can't see any
obvious way of 'scaling', 'zooming' or 'stretching' the bitmap to fit my required
thumbnail space. 

Any suggestions gratefully received.

Regards

Steve

 
Blank Bkgrd.gif

Vadim Zeitlin

unread,
Sep 6, 2009, 9:06:42 PM9/6/09
to wx-u...@googlegroups.com
On Sun, 6 Sep 2009 21:19:20 -0300 Steve Cookson <steve....@sca-uk.com> wrote:

SC> I'm trying to display a thumbnail of a bitmap I'm using, but I can't
SC> see any obvious way of 'scaling', 'zooming' or 'stretching' the bitmap
SC> to fit my required thumbnail space.

You need to convert your wxBitmap to wxImage, call Scale() and then
convert back. In 2.9 you can also use wxDC::StretchBlit().

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Ralph Pass

unread,
Sep 6, 2009, 9:10:02 PM9/6/09
to wx-u...@googlegroups.com
Steve:

You could subsample the image. Or you could do something like

region is the a rectangle of the correct size for your thumbnail.
mBitmap is the bitmap to draw.

//-------------------------------------------------------------
void DisplayImage::drawTo(wxDC & dc, const wxRect & region)
{
mRegion = region;

//
// find factors to fill the client rectangle
//
mHFactor = 1.0 * region.GetWidth() / mBitmap.GetWidth();
mVFactor = 1.0 * region.GetHeight() / mBitmap.GetHeight();
double factor = mHFactor;
if (factor > mVFactor)
{
factor = mVFactor;
}
if (mPreserveProportions)
{
mHFactor = factor;
mVFactor = factor;
}

//
// use factor to determine blit parameters
//
wxMemoryDC temp_dc;
temp_dc.SelectObject(mBitmap);
dc.SetUserScale(mHFactor, mVFactor);

//
// the factors affect both corner and size in the Blit call. If
we assume the region
// is absolute, then the left and top will not be correct if the
factors are not 1
//
// so we adjust them
//
int left = int(region.GetLeft() / mHFactor + 0.5);
int top = int(region.GetTop() / mVFactor + 0.5);
dc.Blit(left, top, mBitmap.GetWidth(), mBitmap.GetHeight(),
&temp_dc, 0, 0);
dc.SetUserScale(1.0, 1.0);

Steve Cookson

unread,
Sep 6, 2009, 10:13:59 PM9/6/09
to wx-u...@googlegroups.com
Hi Vadim,

That's great, thanks very much.

Regards

Steve

Kenneth Porter

unread,
Sep 7, 2009, 2:38:05 AM9/7/09
to wx-u...@googlegroups.com
--On Sunday, September 06, 2009 10:19 PM -0300 Steve Cookson
<steve....@sca-uk.com> wrote:

> I'm trying to display a thumbnail of a bitmap I'm using, but I can't see
> any obvious way of 'scaling', 'zooming' or 'stretching' the bitmap to fit
> my required thumbnail space.

Others have already answered the howto question. The jargon you want is
"resampling":

<http://en.wikipedia.org/wiki/Resampling_%28bitmap%29>

Scaling, zooming, and stretching can be ambiguous terms that might refer to
optical effects. Resampling is the strictly digital effect of operating on
an array of pixels.

Here's a nice page that shows some example images and how different
resampling algorithms affect the quality of the resulting image:

<http://www.jiscdigitalmedia.ac.uk/stillimages/advice/resampling-raster-images/>

Julian Smart

unread,
Sep 7, 2009, 3:09:54 AM9/7/09
to wx-u...@googlegroups.com
Ralph Pass wrote:
> //
> // use factor to determine blit parameters
> //
> wxMemoryDC temp_dc;
> temp_dc.SelectObject(mBitmap);
> dc.SetUserScale(mHFactor, mVFactor);
>
Note that the native scaling on Windows is pretty bad and will often
result in a streaky-looking image with weird colours, so doing the
scaling explicitly will result in a better quality image (albeit with a
time penalty).

Regards,

Julian

Dave Silvia

unread,
Sep 7, 2009, 4:56:50 AM9/7/09
to wx-u...@googlegroups.com
If you're looking for some sample code try:
 
MDI Bitmap Browser
 
In the green floating menu at the top left click the Application radio button and then click the Go button.  In the resulting display, select 'dnld' in the dropdown next to:
 
MDIBitmapBrowser: Image browser and manipulation application
 
Download the archive and unzip to your desired location.
 
The two files you would want to look at are:
 
MDIBitmapBrowserEvtHndlrs.cpp
ImageScaling.cpp
 
ImageScaling.cpp creates the class and sets up the baseline. 
 
In  MDIBitmapBrowserEvtHndlrs.cpp you would look at:
 
MDIBitmapBrowser::OnScaleIdClick
 
This does the actual calculation based on user input and creates the new image.  What type of scaling is controlled by the 'quality' argument to wxImage::Scale().  An idler event handler (also in MDIBitmapBrowserEvtHndlrs.cpp) does the actual blit with a wxBitmap.
 
HTH:
 
Dave S.
 
Development with wxWidgets on MSWindows
http://tech.groups.yahoo.com/group/wxMS_developers/
 
wxWidgets Code Exchange
http://www.wxcodex.net/
 

Steve Cookson

unread,
Sep 7, 2009, 12:44:06 PM9/7/09
to wx-u...@googlegroups.com
Hi Kenneth,

It's true I struggled with the terminology: but I would never have come
up with resampling!

Thanks for illuminating.

Regards

Steve


-----Original Message-----
From: wx-u...@googlegroups.com [mailto:wx-u...@googlegroups.com] On Behalf
Of Kenneth Porter
Sent: 07 September 2009 03:38
To: wx-u...@googlegroups.com
Subject: Re: wxBitmap scaling


Steve Cookson

unread,
Sep 7, 2009, 1:05:23 PM9/7/09
to wx-u...@googlegroups.com
On a related subject,  I've been using .jpg files for storing images.  And I
saw (while looking up 'scaling', 'zooming', etc etc in the wx Book) that jpegs
are not lossless.  I didn't know this before, although I have noticed them
degrading when editing.  What preferences do you have for storing images
for applications, do you always use PNG?  What about GIF?
 
Have a good day.
 
Regards

Steve
 


From: wx-u...@googlegroups.com [mailto:wx-u...@googlegroups.com] On Behalf Of Steve Cookson
Sent: 06 September 2009 21:19
To: wx-u...@googlegroups.com
Subject: wxBitmap scaling

Kenneth Porter

unread,
Sep 7, 2009, 3:16:03 PM9/7/09
to wx-u...@googlegroups.com
--On Monday, September 07, 2009 3:05 PM -0300 Steve Cookson
<steve....@sca-uk.com> wrote:

> On a related subject, I've been using .jpg files for storing images. And
> I saw (while looking up 'scaling', 'zooming', etc etc in the wx Book)
> that jpegs are not lossless. I didn't know this before, although I have
> noticed them degrading when editing. What preferences do you have for
> storing images for applications, do you always use PNG? What about GIF?

Lossy encoding is an option of JPEG, frequently chosen because you usually
get better compression. But you can choose a zero-loss encoding at the time
you save, if the application allows it.

<http://en.wikipedia.org/wiki/JPEG#Lossless_editing>

JPEG is best for storing photograph-like images with continous changes in
colors. I generally use PNG for artificial images with sharp edges and
large areas of a single color, like screen captures and cartoon art.

I don't know if GIF has any advantage anymore. At one time PNG was created
to replace GIF due to a license issue, and I don't know if there are any
features of GIF that it doesn't support.

<http://en.wikipedia.org/wiki/Portable_Network_Graphics>

Screen captures in Windows often default to saving in uncompressed BMP
format, and I hate getting those in email, as they're huge and I archive my
mail. You can save these as PNG in Windows Paint since XP came out.

If you're working with vector art, consider using Scalable Vector Graphic
format (SVG). You can edit this with the free Inkscape program. This
combination is similar in capability to the old Corel Draw program. SVG is
an XML format (which can be delivered gzipped for compression). Inkscape
adds its own XML nodes to describe high-level objects like layers and other
edit-time organization.

Steve Cookson

unread,
Sep 7, 2009, 5:54:15 PM9/7/09
to wx-u...@googlegroups.com
Hi Kenneth,

Thanks for this. I guess I'm going to use JPG for photos then and
PNG for icons. I've noticed the icons have been degrading over time
as I "improve" them.

But back to the thread, does the format make any difference to "resampling".

Regards

Steve



-----Original Message-----
From: wx-u...@googlegroups.com [mailto:wx-u...@googlegroups.com] On Behalf

Brian Ravnsgaard Riis

unread,
Sep 7, 2009, 6:10:48 PM9/7/09
to wx-u...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steve Cookson skrev:


> Hi Kenneth,
>
> Thanks for this. I guess I'm going to use JPG for photos then and
> PNG for icons. I've noticed the icons have been degrading over time
> as I "improve" them.
>
> But back to the thread, does the format make any difference to "resampling".
>
> Regards
>
> Steve

The resampling has nothing to do with the filetype whatsoever. You
only resample raw image data. Any lossy compression will degrade your
image quality *every time you save*, not while resampling. Try opening
a JPEG file in your favorite editor, choosing to save a copy as JPEG
using quality 95 or so, reopen the newly saved file, save again using
same options, etc. Even though you don't make any edits to the picture
the quality will steadily degrade. This is a feature! Not a bug. It's
intrinsic to the way JPEG works. The 'P' in JPEG is for 'Photographic'
and it really says it all: The format is intended for photographs
where the slight omissions made by the compression algorithm don't matter.

You want to maintain your quality? Choose another format. PNG springs
to mind. :-)

On another note (and off-topic really, I guess) if you want to make
several edits to photographs you've shot or downloaded you may want to
save them in a lossless format, at least while you're doing your
edits, and only use JPEG for the final save...

/Brian

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqlhOAACgkQk1tAOprY6QHpFwCgtt1sJeSHr7sOEKR3l/HQJ0P6
NCMAn3zFr3eS+vashFwi4hYgG4fVDf4N
=sbYL
-----END PGP SIGNATURE-----

Steve Cookson

unread,
Sep 7, 2009, 7:47:30 PM9/7/09
to wx-u...@googlegroups.com
Hi Brian,

Thanks for that. That was the advice I was looking for.

Especially the last bit.

Regards

Steve



-----Original Message-----
From: wx-u...@googlegroups.com [mailto:wx-u...@googlegroups.com] On Behalf
Of Brian Ravnsgaard Riis
Sent: 07 September 2009 19:11
To: wx-u...@googlegroups.com
Subject: Re: wxBitmap scaling


Graeme Gill

unread,
Sep 8, 2009, 3:27:31 AM9/8/09
to wx-u...@googlegroups.com
Brian Ravnsgaard Riis wrote:
> only resample raw image data. Any lossy compression will degrade your
> image quality *every time you save*, not while resampling. Try opening
> a JPEG file in your favorite editor, choosing to save a copy as JPEG
> using quality 95 or so, reopen the newly saved file, save again using
> same options, etc. Even though you don't make any edits to the picture
> the quality will steadily degrade. This is a feature! Not a bug. It's

This isn't necessarily true. Typically the same quantization tables
will get used for a given quality level, and the degradation will
taper off, the files reaching a stable state. This is because
once a DCT coefficient has been quantized to a certain level, it will
be re-quantized to the same level. (Yes I've done the above
exercise to confirm this.) Of course if you alter the file, new losses
will occur.

Graeme Gill.

Kenneth Porter

unread,
Sep 8, 2009, 8:51:37 AM9/8/09
to wx-u...@googlegroups.com
--On Tuesday, September 08, 2009 1:10 AM +0200 Brian Ravnsgaard Riis
<br...@ravnsgaard.net> wrote:

> You want to maintain your quality? Choose another format. PNG springs
> to mind. :-)

Ah, further reading of the wiki article I referenced earlier indicates that
the file format is always lossy from computing coefficients, so the only
time one doesn't get loss is when one re-saves data with the same
coefficients. I think. The math is on the edge of my understanding, and I
defer to JPEG experts here.

Reply all
Reply to author
Forward
0 new messages