如何在 GDI 中获得一致的渐变填充+当使用旋转的 LinearGradientBrush 时?

发布于 2024-10-03 08:37:27 字数 675 浏览 9 评论 0原文

我在应用程序中使用 GDI+,并且需要使用旋转的 LinearGradientBrush 以完全相同的方式绘制多个矩形。然而,尽管我调用相同的代码来填充每个矩形,但结果并不是我所期望的。下面是创建渐变填充的代码,其中 rcDraw 是包含每个矩形要绘制的区域的矩形。这些坐标位于父窗口的坐标中,因此这两个矩形的坐标不相同。

g_hbrLinear = new LinearGradientBrush( 矩形( 0, rcDraw.top, 0, rcDraw.bottom - rcDraw.top ), clrStart、clrEnd、(实数)80、假);

我在屏幕上看到的内容如下所示(http://www.nnanime.com/bugs/ LinGradBrush-rotate10.png)。您可以看到,就好像第一个矩形的填充继续到第二个矩形。我真正想要的是让两个矩形看起来相同。我想如果我使用自己的客户端坐标单独绘制每个矩形,我可以做到这一点,但出于我的应用程序的目的,我需要使用父窗口的坐标。

我想我要问的是,GDI+ 如何计算填充的“原点”?它总是基于您使用的坐标系中的 0,0 吗?有办法改变它吗?我尝试了 TranslateTransform,但它似乎并没有以我认为可预测或可理解的方式改变填充。

I'm using GDI+ in my application, and I need to use a rotated LinearGradientBrush to paint several rects in the exact same way. However, although I'm calling the same code to fill each rect, the results aren't what I expect. Here's the code to create the gradient fill, where rcDraw is the rect containing the area to paint for each rect. These coordinates are in the parent window's coordinates, so they are not identical for the 2 rects.

g_hbrLinear = new LinearGradientBrush( Rect( 0, rcDraw.top, 0, rcDraw.bottom - rcDraw.top ),
clrStart, clrEnd, (REAL) 80, FALSE );

What I see on screen looks like this (http://www.nnanime.com/bugs/LinGradBrush-rotate10.png). You can see that it's as if the fill from the first rect continues into the second one. What I really want is to have the 2 rects look identical. I think I can do that if I paint each rect separately using its own client coordinates, but for the purposes of my app, I need to use the parent window's coordinates.

I guess what I'm asking is, how does GDI+ calculate the "origin" of a fill? Is it always based on 0,0 in the coordinate system you use? Is there a way to shift it? I tried TranslateTransform, but it doesn't seem to shift the fill in a way that I find predictable or understandable.

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

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

发布评论

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

评论(2

孤星 2024-10-10 08:37:27

传递给线性渐变画笔的矩形确定左右颜色的位置,并且渐变将在该矩形内绘制。

因此,我认为您需要为您正在绘制的每个矩形创建一个画笔,其中您正在绘制的矩形也会传递给线性渐变画笔的构造函数。

The rect passed to the linear gradient brush determines the where the left and right colors will sit, and the gradient will be painted within this rectangle.

So, I think you need to create a brush for each rectangle you are painting, where the rectangle you are painting is also passed to the constructor for the linear gradient brush.

伏妖词 2024-10-10 08:37:27

我对线性渐变画笔“变换”的经验与您的经验相符;我一直无法理解它应该做什么。

您可以将 GDI+ 中的画笔视为将世界坐标映射到颜色的函数。画笔在给定点的外观不会根据填充的形状而改变。

它确实会随着您正在绘制的 Graphics 对象的变换而变化。因此,如果您不想更改画笔,则可以临时更改 Graphics 对象的变换,以便您正在绘制的矩形在世界坐标中具有特定的已知大小和位置。 BeginContainer 和 EndContainer 方法应该使这变得容易。

(还有 RenderingOrigin 属性,但它只影响填充画笔,奇怪的是,它不受世界变换的影响。)

My experience with the "transform" of linear gradient brushes matches yours; I haven't been able to understand what it's supposed to do.

You can think of a brush in GDI+ as a function mapping world co-ordinates to a color. What the brush looks like at a given point does not change based on the shape being filled.

It does change with the transform of the Graphics object you're drawing on. So, if you don't want to change the brush, you could temporarily change the transform of the Graphics object so that the rectangle you're drawing has a specific, known size and position in world coordinates. The BeginContainer and EndContainer methods should make this easy.

(There is also the RenderingOrigin property but it only affects hatch brushes, which oddly are unaffected by world transforms.)

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