0

特定の位置と特定のポリゴンの内部に 1 つの画像を別の画像に追加するという要求があります。

次のコードを使用しました:

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 に答える