如何使用图像和纹理蒙版平铺形状?

时间:2018-05-28 23:43:31

标签: c# winforms drawing

如何通过使用纹理蒙版将纹理画笔的FillRectangle应用于源图像?

类似的东西......但是如何使用纹理蒙版?

    Bitmap textile = new Bitmap("textile.png");
    TextureBrush textileBrush = new TextureBrush(textile);
    Bitmap outfit = new Bitmap("outfit.png");
    Bitmap masksource = new Bitmap("mask.png");

    Color bodyColorKey = Color.FromArgb(0, 255, 0);

    //what i should to do next and how to apply FillRectangle?
    using (Graphics g = Graphics.FromImage(outfit))
    {
        g.FillRectangle(textileBrush, new Rectangle(0, 0, ???, ???));
    }

装: enter image description here 他的面具: enter image description here 纺织纹理:enter image description here 它应该如何照顾,输出:enter image description here

任何想法应该如何查找System.Drawing?

1 个答案:

答案 0 :(得分:1)

使用蒙版图像,使给定颜色透明并按如下方式组合图像。

Bitmap textile = new Bitmap("textile.png");
        TextureBrush textileBrush = new TextureBrush(textile);
        Bitmap outfit = new Bitmap("outfit.png");
        Bitmap masksource = new Bitmap("mask.png");

        Bitmap transparentImage = new Bitmap(outfit.Width, outfit.Height);

        masksource.MakeTransparent(Color.FromArgb(255, 0, 255, 0));
        using (Graphics g = Graphics.FromImage(transparentImage))
        {
            g.DrawImage(outfit, new Point(0, 0));
            //g.DrawImage(textile, new Point(0, 0)); // use texture image
            g.FillRectangle(textileBrush, g.ClipBounds); // use texture image as Tile mode
            g.DrawImage(masksource, new Point(0, 0));
            transparentImage.MakeTransparent(Color.FromArgb(255, 255, 0, 0));
            transparentImage.MakeTransparent(Color.FromArgb(255, 0, 0, 255));
        }
        Bitmap outputImage = new Bitmap(outfit.Width, outfit.Height);
        using (Graphics g = Graphics.FromImage(outputImage))
        {
            g.DrawImage(outfit, new Point(0, 0));
            g.DrawImage(transparentImage, new Point(0, 0));
        }

只需要用纹理图像填充矩形

//what i should to do next and how to apply FillRectangle?
using (Graphics g = Graphics.FromImage(outfit))
{
    g.FillRectangle(textileBrush,g.ClipBounds);
}