Thursday, 5 September 2013

GDI+ image scaling issue

GDI+ image scaling issue

I have an application which draws background from some bitmap using gdi+.
Some of bitmaps are vertical non-linear gradients (e.g. they are 1 pixel
wide and should be stretched horizontally to fill whole control width).
The problem is that with small images (like described above), some control
area on the right is not painted.
I've written a test program that scales a 1x1 image to different sizes,
and it shows that issue occurs when scale factor is big enough
void Draw(HDC hDestDC, int destleft, int desttop, int destwidth, int
destheight)
{
COLORREF buffer[] = {0xFF0000FF }; // 1 pixel image
const int width = 1, height = 1;
Gdiplus::Bitmap gdipBitmap(width, height, 4*width,
PixelFormat32bppARGB, (BYTE*)buffer);
Gdiplus::ImageAttributes attrs;
Gdiplus::Rect dest(destleft, desttop, destwidth, destheight);
Gdiplus::Graphics graphics(hDestDC);
graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
graphics.DrawImage(&gdipBitmap, dest, 0, 0, width, height,
Gdiplus::UnitPixel, &attrs);
}
// OnPaint:
for(int i=0; i<800; ++i)
Draw(hdc, 0, i, i, 1); // scale 1x1 image to different width
I would exptect this test to draw a smooth 'triangle', but instead lines
do not have size exactly as specified by destination rect:
http://i.imgur.com/NNpMvmW.png
Is there any way to fix this behavior? I need to output bitmap with size
exactly as specified (considering source image alpha-channel and no
interpolation with background on the edges).
P.S: I have to use GDI+ because actual code uses some ImageAttributes
provided by gdiplus.

No comments:

Post a Comment