GDI+ DrawImage stretches incorrectly

M

Martin Maercker

Dear group,

I posted this question before but still don't have a clue why
DrawImage _occasionally_ drops pixels. I don't want to be pushy but
am under increasing time pressure to find a solution. So I hope
you'll pardon me for trying again under a catchier subject line.

I need to stretch a semitransparent, originally 1 pixel wide image to
a new width that is set at runtime.

I use:

pGraphics->SetInterpolationMode(InterpolationModeNearestNeighbor);
pGraphics->SetPixelOffsetMode(PixelOffsetModeHalf);

pGraphics->DrawImage(pBmap,
Rect(x,y,
dynamicWidth,height),
0,0,width,height,
UnitPixel,0,0,0);


"width" and "height" are the width and height of the image to be stretched.
"dynamicWidth" is the width that the output image is supposed to wind up
having.

This works fine for most widths. For _some_ widths, however, the resulting
stretched image is one or two pixels too small. An example of such a width
is 480 pixels. Problem is, my application needs to be able to set
the exact size of the stretched image for all widths.

How can I make sure that DrawImage will produce an image of exactly the
specified size for every specified size?


Any help greatly appreciated,

Martin Maercker
 
A

alejandro lapeyre

your image is 1 pixel wide
does the height remain constant?

if you are in hurry you may do the following:

stretch to 2 or 4 pixels more than needed then copy the appropiate rectangle
to the ouput bitmap.

regards,
Alejandro Lapeyre
 
B

Bob Powell [MVP]

I ran some tests and you're right. It looks like this is definitely a bug.

I would suggest that a workaround would be to fill a rectangle of the
desired size with a brush made from the 1 pixel wide image.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
M

Martin Maercker

Hi Alejandro,
your image is 1 pixel wide
does the height remain constant?
Yes, the input is always the same height. The height in the output is
consistently the same as well. The problems I am experiencing are re-
lated only to the dimension in which the stretching is going on.
if you are in hurry you may do the following:

stretch to 2 or 4 pixels more than needed then copy the appropiate rectangle
to the ouput bitmap.
That's a possibility. In my case it would be a bit cumbersome, since I
would have to divert to a separate bitmap an operation that is now
integrated into several other compositing steps all acting on one
bitmap. Also, I was kind of hoping to find a solution that just gets
the API to do what one would expect anyway. Thanks for the suggestion,
though. Currently it looks like that is what I will end up doing since
time is running out.


Regards and thanks again,

Martin
 
M

Martin Maercker

Dear group,

I am currently using the following solution (with thanks
to Alejandro Lapeyre):

pGraphics->SetInterpolationMode(InterpolationModeNearestNeighbor);
pGraphics->SetPixelOffsetMode(PixelOffsetModeHalf);

Rect destRect(
x,y,
dynamicWidth,height);

pGraphics->SetClip(destRect);

// widen beyond actual goal
destRect.Width += 4;

pGraphics->DrawImage(pBmap,
destRect,
0,0,width,height,
UnitPixel,0,0,0);

pGraphics->ResetClip();


This works reliably.

I'd still be interested in a solution that doesn't need
the "trick" used above, though.


Regards,

Martin
 
Top