要求在其他图像上的特定多边形的特定位置和内部添加一个图像。
我使用了以下代码:
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
应放在由imgB
上的点确定的多边形内。 imgF
应在该多边形内拉伸,以适应。阅读文档我发现应该设置它:
brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Clamp;
但它不起作用。当它打开时,根本不会绘制imgF
。如果该行被删除或设置如下:
brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
然后imgF
重复几次。
那么如何让imgF
进行加标,适合,重新调整尺寸并放置在多边形内?
我不需要矩形,因为它不是由相同的底部和底部边缘或左右边缘确定的形状。
答案 0 :(得分:1)
阿德里亚诺为您提供了如何做到这一点的线索。使用多边形的点在目标图像上定义剪切区域,然后使用适当的拉伸绘制位图以填充目标区域。
试试这个例子:
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);
}
}