wxBitmap scale and resize

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
MikeLift
In need of some credit
In need of some credit
Posts: 4
Joined: Thu May 06, 2021 6:52 pm

wxBitmap scale and resize

Post by MikeLift »

In msw it uses GDIPlusRenderer as default if scale gets smaller than 0.4 with good antialiasing. Pretty fast.
Using wxMemoryDC is very fast, but no antialiasing.
With scale>=0.4, antialiasing is not helpful anyway, but you can decide.
No restriction in scale except scale <= 0.

The trick was realizing that the selected bmpOut in the device context is actually the target of the blitter.

Code: Select all

#include <wx/dcgraph.h>
wxBitmap ScaleBitmap(wxBitmap &bmpIn, double scale, bool antialiasing)
{
  if(scale <= 0.0)
    return wxNullBitmap;
  wxSize sizeIn = bmpIn.GetSize();
  wxSize sizeOut((int)sizeIn.GetX()*scale, (int)sizeIn.GetY()*scale);
  
  wxBitmap bmpOut;
  bmpOut.Create(sizeOut);   // the final size
  
  wxMemoryDC inDC(bmpIn);
  
  if(antialiasing && scale<0.4) 
  {
    wxGCDC outDC(bmpOut);
    outDC.StretchBlit(wxPoint(0,0), sizeOut, &inDC, wxPoint(0,0), sizeIn);
  }
  else
  {
    wxMemoryDC outDC(bmpOut);
    outDC.StretchBlit(wxPoint(0,0), sizeOut, &inDC, wxPoint(0,0), sizeIn);
    outDC.SelectObject(wxNullBitmap);
  }
  inDC.SelectObject(wxNullBitmap);
  return bmpOut;
}
Would like to know if this works in the other environments.
Last edited by MikeLift on Fri May 07, 2021 8:31 am, edited 1 time in total.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxBitmap scale and resize

Post by ONEEYEMAN »

Hi,
IIUC, wxMemoryDC should be avoided at least on GTK.

Thank you.
Post Reply