【问题标题】:Fill texture brush using image, not tile使用图像填充纹理画笔,而不是平铺
【发布时间】:2016-10-19 03:52:08
【问题描述】:

我有一个纹理画笔,它使用某个图像来使纹理显示如下:

Image image = new Bitmap("Untitled.png");
for (int i = 0; i < points.Count; i++)
{
    using (TextureBrush tbr = new TextureBrush(image))
    {
          tbr.RotateTransform(i * 4);
          var p = PointToClient(Cursor.Position);
          tbr.Transform = new Matrix(
          75.0f / 640.0f,
          0.0f,
          0.0f,
          75.0f / 480.0f,
          0.0f,
          0.0f);
          e.Graphics.FillEllipse(tbr, p.X - 50, p.Y - 50, 100, 100);
          Pen p3 = new Pen(tbr);
          e.Graphics.DrawEllipse(Pens.DeepSkyBlue, p.X - 50, p.Y - 50, 100, 100);
    }
}

这是它使用的图像:

结果是这样的:

我希望图像填充圆圈,使其看起来像这样(编辑后的图像):

任何帮助将不胜感激。

【问题讨论】:

标签: c# winforms textures imagebrush


【解决方案1】:

您需要使用正确的数字进行缩放。

如果你想要一个尺寸 = 宽度 * 高度像素的图像来填充一个直径圆,你应该像这样缩放:

 int diameter = 100;
 Image image = new Bitmap(yourImage);
 float scaleX = 1f * diameter / image.Size.Width;
 float scaleY = 1f * diameter / image.Size.Height;

但请注意,您的TextureBrush 将始终显示由您的图像制成的平铺。这对您的 original question 来说似乎没问题,尤其是在旋转尾部图像以消除任何伪影时。

但这里可能根本不是你想要的。

如果您希望图像跟随鼠标,则需要绘制它。

这是一个示例,它使用复选框在平铺绘图之间切换。动画只使用一帧:

    for (int i = 0; i < points.Count; i++)
    {
        using (TextureBrush tbr = new TextureBrush(image))
        {
            tbr.RotateTransform(i * 4);   // optional
            var p = PointToClient(Cursor.Position);
            tbr.Transform = new Matrix(
                scaleX,
                0.0f,
                0.0f,
                scaleY,
                0.0f,
                0.0f);
            // any tile mode will work, though not all the same way
            tbr.WrapMode = WrapMode.TileFlipXY;
            if (cbx_tileFunny.Checked)
                e.Graphics.FillEllipse(tbr, p.X - diameter/2, 
                                            p.Y - diameter/2, diameter, diameter);
            else
            {
               ((Bitmap)image).SetResolution(e.Graphics.DpiX, e.Graphics.DpiY);   // (**)
                e.Graphics.ScaleTransform(scaleX, scaleY);
                e.Graphics.DrawImage( image, (p.X - diameter/2) / scaleX,
                                             (p.Y - diameter/2 ) / scaleY);
                e.Graphics.ResetTransform();

            }
                /// ? Pen p3 = new Pen(tbr);
                e.Graphics.DrawEllipse(Pens.DeepSkyBlue, p.X - diameter/2,
                                       p.Y - diameter/2, diameter, diameter);
        }
    }

如果图像的 dpi 设置与您的屏幕不同,请注意此处所需的额外缩放 (**)。

另外:虽然快速创建和处理钢笔和画笔通常是一个好主意,但当花费如此多的精力来创建画笔和/或图像缓存它们或什至一系列它们时,imo 似乎更可取。

【讨论】:

  • 非常感谢您的回答。它帮助很大,我只有一个问题要问你,你是如何让图像变得圆润的。我的图像上仍然有白色的角。
  • 或者你只是使用白色背景?
  • 别担心,我整理好了,再次感谢一百万:)
  • 我在 Photoshop 中将边角设置为透明 :-)
  • 您查看我们的聊天记录了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多