c# 将图像(缩放)绘制为图形,无法正确插值。修复?

发布于 2024-10-06 18:17:00 字数 326 浏览 1 评论 0原文

我有一个 1px 宽和一定高度的图像。我需要在其 OnPaint 事件上在控件的整个宽度上绘制此图像。我可以绘制它,但不正确。看起来当它拉伸它时,它实际上并没有填充所有像素。就好像插值关闭一样。有没有办法说“别再聪明了,直接画出来吧”?我在图形对象的选项中没有看到 InterpolationMode.Off 或 .None。

我可以确认我实际上使用宽度为 X 的图像绘制了完整宽度,其中 X 与控件的宽度相同。然后当它绘制时,它会像平常一样覆盖整个区域。然而,这个控件的大小一直在调整,为了节省内存,使用 1px 宽图像的爵士乐在网络世界中是很正常的。但这是针对桌面 C# 应用程序的。关于如何解决这个问题有什么想法吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-10-13 18:17:00

好吧,我找到了神奇的关键字:

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

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

如果不设置插值模式,您会得到奇怪的混合(预期)。
如果不设置 PixelOffsetMode,最近邻算法在空白涂料上没有可比较的邻居,因此仅绘制一半图像、一半宽度。将其设置为偏移一半,将所有内容移动 -0.5px,并允许该算法适用于块纹理。

Ok I figured out the magic keywords:

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

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

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.

甜妞爱困 2024-10-13 18:17:00

InterpolationMode.NearestNeighbor 是您在这种情况下要使用的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文