From db6d565fade5bdae3b30cafc845a8a4ce5d45d6f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 10 Apr 2022 19:45:59 +0100 Subject: [PATCH] Handle wxDC scale factor in wxBitmap ctor taking wxDC in wxMSW When using wxBitmap ctor taking wxDC, the bitmap should inherit the scale factor of the DC, both because it's already the case in the other ports and because it makes sense to do it. Add a unit test checking that this is the case in all ports now. --- src/msw/bitmap.cpp | 9 ++++++++- tests/graphics/bitmap.cpp | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 990bc08030..c859c162d7 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -755,7 +755,14 @@ bool wxBitmap::Create(int width, int height, const wxDC& dc) { wxCHECK_MSG( dc.IsOk(), false, wxT("invalid HDC in wxBitmap::Create()") ); - return DoCreate(width, height, -1, dc.GetHDC()); + const double scale = dc.GetContentScaleFactor(); + + if ( !DoCreate(wxRound(width*scale), wxRound(height*scale), -1, dc.GetHDC()) ) + return false; + + GetBitmapData()->m_scaleFactor = scale; + + return true; } bool wxBitmap::CreateWithDIPSize(const wxSize& size, double scale, int depth) diff --git a/tests/graphics/bitmap.cpp b/tests/graphics/bitmap.cpp index 0507ea4032..f852e0ee65 100644 --- a/tests/graphics/bitmap.cpp +++ b/tests/graphics/bitmap.cpp @@ -1701,6 +1701,28 @@ TEST_CASE("Bitmap::DC", "[bitmap][dc]") #endif // wxUSE_SVG } +#if defined(wxHAS_DPI_INDEPENDENT_PIXELS) || defined(__WXMSW__) + +TEST_CASE("Bitmap::ScaleFactor", "[bitmap][dc][scale]") +{ + // Create a bitmap with scale factor != 1. + wxBitmap bmp; + bmp.CreateWithDIPSize(8, 8, 2); + REQUIRE( bmp.GetScaleFactor() == 2 ); + CHECK( bmp.GetSize() == wxSize(16, 16) ); + + // wxMemoryDC should use the same scale factor as the bitmap. + wxMemoryDC dc(bmp); + CHECK( dc.GetContentScaleFactor() == 2 ); + + // A bitmap "compatible" with this DC should also use the same scale factor. + wxBitmap bmp2(4, 4, dc); + CHECK( bmp2.GetScaleFactor() == 2 ); + CHECK( bmp2.GetSize() == wxSize(8, 8) ); +} + +#endif // ports with scaled bitmaps support + #if wxUSE_GRAPHICS_CONTEXT inline void DrawScaledBmp(wxBitmap& bmp, float scale, wxGraphicsRenderer* renderer)