c# - 使用图像而不是平铺填充纹理画笔

标签 c# winforms textures imagebrush

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

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);
    }
}

这是它使用的图像:

Image

结果是这样的:

Initial

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

Final

如有任何帮助,我们将不胜感激。

最佳答案

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

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

 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 ,尤其是在尾部旋转图像以消除任何伪影时。

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

如果你想让图像跟随鼠标,你需要绘制它。

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

enter image description here

    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。

关于c# - 使用图像而不是平铺填充纹理画笔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105910/

相关文章:

c# - 使用 Nhibernate 获取表锁 (MyIsam)

C# 将事件目录用户添加到组

C# 不写入设置

c++ - OpenGL 中天空盒的程序/动态着色

swift - 如何在 SceneKit 中反转球体的法线?

android - 视频纹理 : Augmented Reality Framework for Android

c# - 加参数后的SQL语句

c# - 无法打开下载保存对话框

c# - 将数据库中的数据绘制图表并合并具有相同值的行

c# - 如何在Windows窗体应用程序中创建表格?