GDI + DrawImage JPG with white background not white

I am showing jpg in a C ++ CWnd window using GDI +. JPG has a clean white background, 0xffffff, but when displayed using graphics.DrawImage, the background is not completely white with a combination of pixel colors such as 0xfff7f7, 0xf7fff7, 0xf7f7f7. Below is the code, I tried various settings like CompositingMode, SmoothingMode, etc. Image does not scale.

The strange thing is that the background color differs depending on other non-white content in the image. If I make a plain white JPG, then it works or even basically white with some kind of black text. Image comparison is shown below.

CClientDC dc(this); Gdiplus::Graphics graphics(dc); Gdiplus::Bitmap* bmp = Gdiplus::Bitmap::FromFile( L"c:\\test.jpg" ); graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQuality); //graphics.SetCompositingQuality(Gdiplus::CompositingQualityHighQuality); graphics.SetCompositingQuality(Gdiplus::CompositingQualityDefault); graphics.SetCompositingMode(Gdiplus::CompositingModeSourceCopy); //graphics.SetSmoothingMode( Gdiplus::SmoothingMode::SmoothingModeDefault ); graphics.DrawImage(bmp, 0, 0, bmp->GetWidth(), bmp->GetHeight() ); 

Here I have text and some confusion only on the left side of the image (without alpha, this is JPG). Everything to the right is pure white. You can see that the background is gray.

alt text

Here I started to delete the internal content (only on the left side). After a certain moment, the whole background begins to appear white. ???

alt text

It really doesn't matter how much of the image area I delete before it starts displaying white if I delete most of it. The same thing happens for pngs.

Here is the original image test.jpg ...

alt text

+2
source share
2 answers

I answer my question with the solution I found. It seems that using .DrawImage graphics directly on the transferred HDC has some problems in my case. If I use DC memory for the initial drawing, then BitBlt is on HDC, then it works.

I also had some problems with PNG and transparency. Using the solution below, I was able to solve this problem. My PNG was loaded from a stream using Bitmap :: FromStream. The alpha channel was lost, and I tried to make various attempts using LockBits and recreate the bitmap using PixelFormat32bppARGB as well as Cloning. I was able to get something to work (after a lot of effort and extra code), but I still had the gray background problem that I asked here.

In my case, I have a solid background color for transparent areas. I used Bitmap.GetHBITMAP and passed the background color. Then the bitmap was drawn in DC memory. Here I was able to solve both my problems!

 Gdiplus::Bitmap* bmp = Gdiplus::Bitmap::FromFile( L"c:\test.jpg" ) Gdiplus::Color backColor( 0xff, 0xff, 0xff ); HBITMAP hBmp; bmp->GetHBITMAP( backColor, &hBmp ); CDC bitmapDC; bitmapDC.CreateCompatibleDC(hdc); // pass original HDC for drawing HBITMAP oldBmp = bitmapDC.SelectBitmap(hBitmap); ::BitBlt( hdc, x, y, cx, cy, bitmapDC.m_hDC, 0, 0, SRCCOPY ); bitmapDC.SelectBitmap(oldBmp); DeleteObject( hBmp ); 

If anyone knows, I would be wondering why this fixes the problem.

+3
source

How did you confirm that the background of the jpg is really white? JPG implements compression, which can vary the color of pixels. If there are mixed colors, then there may be certain types of blending and blending that are part of this compression.

Can you show us the original image?

0
source

All Articles