wxBitmap/wxImage: Setting resolution/dpi for saving

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
berlinermainzer
In need of some credit
In need of some credit
Posts: 1
Joined: Fri Apr 07, 2006 9:21 am

wxBitmap/wxImage: Setting resolution/dpi for saving

Post by berlinermainzer »

Hi there,

I already searched the forum and on Google without success on this topic. My problem is as follows: I use a wxClientDC to draw some stuff onto a ScrolledWindow, which works fine. In order to save the content of the DC into a file, I use a wxBitmap and a wxImage (see code below). Saving seems to work properly, but the saved image file always (not depending on the format) has a resolution of 1 dpi, which is quite annoying when eg. printing it with some external software.
So here comes my question: Is there a way to set the resoltion to another value? I only found such a feature in wxPostScriptDC.

Thanks & Greetings
Johannes

Here the code I use.

Code: Select all

  int iDCWidth = 0; int iDCHeight = 0;
  m_pwxDC->GetSize(&iDCWidth, &iDCHeight);   // m_pwxDC is the wxClientDC I use for drawing         
  m_wxSaveBitmap.Create(iDCWidth,iDCHeight);   // m_wxSaveBitmap is a wxBitmap I use for saving
  wxMemoryDC wxSaveMemDC = new wxMemoryDC();
  if (!wxSaveMemDC.Ok())
    return false;

  wxSaveMemDC.SelectObject(m_wxSaveBitmap);
  double dX = 0; double dY = 0;
  
  wxSaveMemDC.Blit(wxPoint(0,0),m_pwxDC->GetSize(), m_pwxDC, wxPoint(0,0));
  
  wxImage wxSaveImage = m_wxSaveBitmap.ConvertToImage();

  if (wxsFileName.empty())
  {
    wxSaveMemDC.SelectObject(wxNullBitmap);
    return false;
  }

  wxSaveImage.SaveFile(wxsFileName);
  wxSaveMemDC.SelectObject(wxNullBitmap);
  return true;
gunterkoenigsmann
Earned a small fee
Earned a small fee
Posts: 14
Joined: Tue May 31, 2016 11:30 am

Re: wxBitmap/wxImage: Setting resolution/dpi for saving

Post by gunterkoenigsmann »

For images there is a way to set the dpi/ppi rate:

Code: Select all

    img.SetOption(wxIMAGE_OPTION_RESOLUTION,resolution * bitmapScale);
For bitmaps there is the following command:

Code: Select all

  m_bmp.CreateScaled(10,10,wxBITMAP_SCREEN_DEPTH,1.0/((double)scale));
If setting these options changes anything you'll have to test, though.

...and I know that this is an old thread. But it is always good to have a googleable response for each question that might arise.
Post Reply