c# - 我可以在没有这些工件的情况下使用 graphics.RotateTransform() 吗?

标签 c# graphics bitmap rotation gdi+

我正在实现一个颜色选择器组件,如 seminal article 中所述.

如您所见,我已经整理好基础知识:

Working colour wheel

然而,其中一个要求是能够让色轮旋转任意量。认为这很容易,我对鼠标位置 -> 颜色值代码进行了一些运算,并将以下代码用于实际绘制轮子的位:

newGraphics.TranslateTransform((float)this.Radius, (float)this.Radius);
newGraphics.RotateTransform((float)this.offset);
newGraphics.TranslateTransform((float)this.Radius * -1, (float)this.Radius * -1);

不幸的是,像这样旋转位图实际上会产生这样的结果:

Broken colour wheel after rotation

注意出现在中心两侧的人工制品。

我是否使用了错误的方法?或者有没有办法摆脱这些讨厌的撕裂?

最佳答案

查看该 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/

相关文章:

java - 如何在android中将位图转换为字节数组,将字节数组转换为位图?

c# - StructureMap - 根据具体请求实例化接口(interface) x 实例的属性

C# For 循环索引返回 ascii 而不是当前迭代

c# - .Net 程序集加载问题期间出现 BadImageFormatException

iPhone - 翻转 UIImage

android - 在 SurfaceView 中显示 ImageView

c# - 对 Microsoft.IdentityModel.Clients.ActiveDirectory 3.14.2 的硬依赖

graphics - 为什么使用 JPEG 图片时必须使用 2.2 的 gamma?

python - Collada (*.dae) 文件结构和 Pycollada

c# - 调整位图大小以绘制字符串