C # Draw an image (scaled) in Graphics, do not interpolate correctly. Corrections?

I have an image 1px wide and some height. I need to draw this image over the entire width of the control on this OnPaint event. I get to draw it, however not right. It seems that when it stretches it, it does not actually fill all the pixels. As if interpolation is disabled. Is there a way to say "stop being smart, just draw it already"? I do not see InterpolationMode.Off or .None in the parameters for the graphic.

I can confirm that I am actually drawing the full width using an image of width X, where X is the same width as the control. Then, when he draws, he covers the entire area as usual. However, this control is constantly changing, and to save memory and all this jazz using 1-pixel-wide images, it’s quite normal in the world of the Internet. This is for a C # desktop application. Any ideas on how to fix this?

+4
source share
2 answers

Ok, I understood the magic words:

g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; 

In combination with setting the interpolation mode to NearestNeighbor, you can make a complete block.

Without setting the interpolation mode you will get a weird mix (expected). Without setting PixelOffsetMode, the nearest neighbor algorithm does not have a neighbor to compare with empty paint, and therefore only half of the image draws half the width. Setting the offset to half, everything moves by -0.5px and allows this algorithm to work with block textures.

+9
source

InterpolationMode.NearestNeighbor is what you want to use in this case.

0
source

All Articles