c#绘制图像(缩放)为图形,不能正确插值。修复? [英] c# Draw Image (Scaled) to Graphics, does not interpolate correctly. Fixes?

查看:113
本文介绍了c#绘制图像(缩放)为图形,不能正确插值。修复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宽度为1px的图片,以及一些高度。我需要在它的OnPaint事件的整个控件宽度上绘制这个图像。我得到它,但不正确。它看起来像延伸它时,它并不实际填充所有像素。就好像插值关闭一样。有没有办法说停止聪明,只是已经画出来?我在图形对象的选项中看不到InterpolationMode.Off或.None。

I have an image that is 1px wide, and some height. I need to draw this image across the entire width of the control on it's OnPaint event. I get it to draw, however not correctly. It seems like when it stretches it, it doesn't actually fill all the pixels. As if the interpolation is off. Is there a way to say "stop being smart, just draw it already"? I see no InterpolationMode.Off or .None in the options for the graphics object.

我可以通过使用宽度X的图像来确认实际绘制的全宽,其中X与控件的宽度相同。然后当它画,它正常覆盖整个区域。然而,这个控件一直在调整大小,并且为了节省内存,所有使用1px宽图像的爵士乐在网络世界中是非常正常的。这是用于桌面C#应用程序。关于如何解决这个问题的任何想法?

I can confirm I actually drawing the full width by using an image of width X where X is the same width as the control. Then when it draws, it covers the full area as normal. However this control is resized all the time, and to save memory and all that jazz using 1px wide images is quite normal in the web world. This is for a desktop C# application though. Any ideas on how to fix this?

推荐答案

好吧,我想出了魔术关键字:

Ok I figured out the magic keywords:

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

结合将插值模式设置为NearestNeighbour,可以绘制完整块。

This coupled with setting the Interpolation Mode to NearestNeighbour allows for a full block to be drawn.

如果不设置插值模式,会出现奇怪的混合(预期)。
在没有设置PixelOffsetMode的情况下,最近邻居算法没有邻居可以在空白画面上进行比较,因此只绘制一半图像的宽度的一半。将它设置为偏移一半,将所有内容移动-0.5px,并允许此算法适用于块纹理。

Without setting the Interpolation mode, you get weird blending (expected). Without setting the PixelOffsetMode, the nearest neighbour algorithm has no neighbour to compare to on a blank paint and therefore only draws half the image, for half the width. Setting it to offset half, moves everything over by -0.5px, and allows this algorithm to work for block textures.

这篇关于c#绘制图像(缩放)为图形,不能正确插值。修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆