我正在实现一个颜色选择器组件,如 seminal article 中所述.
如您所见,我已经整理好基础知识:
然而,其中一个要求是能够让色轮旋转任意量。认为这很容易,我对鼠标位置 -> 颜色值代码进行了一些运算,并将以下代码用于实际绘制轮子的位:
newGraphics.TranslateTransform((float)this.Radius, (float)this.Radius);
newGraphics.RotateTransform((float)this.offset);
newGraphics.TranslateTransform((float)this.Radius * -1, (float)this.Radius * -1);
不幸的是,像这样旋转位图实际上会产生这样的结果:
注意出现在中心两侧的人工制品。
我是否使用了错误的方法?或者有没有办法摆脱这些讨厌的撕裂?
最佳答案
查看该 Microsoft 示例的源代码,我通过添加矩阵和设置 RotateAt
方法对 UpdateDisplay
方法进行了以下更改。
private void UpdateDisplay() {
// Update the gradients, and place the
// pointers correctly based on colors and
// brightness.
using (Brush selectedBrush = new SolidBrush(selectedColor)) {
using (Matrix m = new Matrix()) {
m.RotateAt(35f, centerPoint);
g.Transform = m;
// Draw the saved color wheel image.
g.DrawImage(colorImage, colorRectangle);
g.ResetTransform();
}
// Draw the "selected color" rectangle.
g.FillRectangle(selectedBrush, selectedColorRectangle);
// Draw the "brightness" rectangle.
DrawLinearGradient(fullColor);
// Draw the two pointers.
DrawColorPointer(colorPoint);
DrawBrightnessPointer(brightnessPoint);
}
}
它将滚轮旋转了 35 度(虽然颜色选择现在关闭了 35 度,因为我没有弄乱所有代码)并且没有产生任何撕裂。
不是 100% 确定这就是答案(但对于评论来说太长了),所以这可能会有所帮助。
关于c# - 我可以在没有这些工件的情况下使用 graphics.RotateTransform() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117591/