0

要求在某个位置和其他图像的特定多边形内部添加一个图像。

我使用了以下代码:

private static void MergeBitmaps(string ImageFore, string ImageBack)
{
    try
    {
        Point point1 = new Point(833, 278);
        Point point2 = new Point(1876, 525);
        Point point3 = new Point(1876, 837);
        Point point4 = new Point(833, 830);

        Point[] curvePoints = { point1, point2, point3, point4 };

        Bitmap imgB = new Bitmap(ImageBack);
        Bitmap imgF = new Bitmap(ImageFore);
        Bitmap m = new Bitmap(ImageBack);
        System.Drawing.Graphics myGraphic = System.Drawing.Graphics.FromImage(m);

        myGraphic.SmoothingMode = SmoothingMode.HighQuality;
        myGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        myGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
        TextureBrush brush = new TextureBrush(imgF);
        brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Clamp;

        myGraphic.FillPolygon(brush, curvePoints);
        myGraphic.Save();
        m.Save(@"new location", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.StackTrace);
    }
}

因此,imgF应放置在由 上的点确定的多边形内imgBimgF应该在该多边形内拉伸以适应。阅读文档我发现应该这样设置:

 brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Clamp;

但它不起作用。当它打开时,imgF根本不绘制。如果该行被删除或设置如下:

 brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;

然后imgF重复几次。

那么如何imgF进行上浆、贴合、重新调整大小和放置在多边形内呢?

我不需要矩形,因为它不是由相同的底部和向下边缘或左右边缘确定的形状。

4

1 回答 1

1

Adriano 为您提供了如何执行此操作的线索。使用多边形的点在目标图像上定义一个剪切区域,然后使用适当的拉伸绘制位图以填充目标区域。

试试这个例如:

private static void MergeBitmaps(string ImageFore, string ImageBack)
{
    // Define output polygon and coverage rectangle
    Point[] curvePoints = new Point[] {
        new Point(833, 278), new Point(1876, 525), 
        new Point(1876, 837), new Point(833, 830)
    };
    Rectangle outRect = new Rectangle(833, 278, 1043, 559);

    // Create clipping region from points
    GraphicsPath clipPath = new GraphicsPath();
    clipPath.AddPolygon(curvePoints);

    try
    {
        Bitmap imgB = new Bitmap(ImageBack);
        Bitmap imgF = new Bitmap(ImageFore);
        Bitmap m = new Bitmap(ImageBack);
        System.Drawing.Graphics myGraphic = System.Drawing.Graphics.FromImage(m);

        myGraphic.SmoothingMode = SmoothingMode.HighQuality;
        myGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        myGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

        // Draw foreground image into clipping region
        myGraphic.SetClip(clipPath, CombineMode.Replace);
        myGraphic.DrawImage(imgF, outRect);
        myGraphic.ResetClip();

        myGraphic.Save();
        m.Save(@"new location", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.StackTrace);
    }
}
于 2013-11-04T01:12:56.083 回答