# C#如何用GDI+实现对图形的多边形输出

53 篇文章 0 订阅

private void CreateMultiAnglePic(string sourceFile,string targetFile)
{
Bitmap image = new Bitmap(sourceFile);
Bitmap resultImage = new Bitmap(image.Width, image.Height);

//建立缓冲图片
Graphics gr = Graphics.FromImage(resultImage);
gr.Clear(Color.FromArgb(0, Color.Transparent));

Rectangle resultRectangle = new Rectangle(0, 0, image.Width, image.Height);
int width = image.Width;
int height = image.Height;
int yy = height / 2;
int xx = 0;
Point[] myArray =
{
new Point(xx, yy),
new Point(Convert.ToInt32(yy*0.75), xx),
new Point(width-Convert.ToInt32(yy*0.75),xx),
new Point(width, yy),
new Point(width-Convert.ToInt32(yy*0.75), height),
new Point(Convert.ToInt32(yy*0.75), height),
new Point(xx, yy),
};

Region reg = new Region();
reg.MakeEmpty();
GraphicsPath gp = new GraphicsPath();
reg.Union(gp);

gr.SetClip(reg, CombineMode.Replace);
gr.DrawImage(image, resultRectangle);

resultImage.Save(targetFile, ImageFormat.Png);

gp.Dispose();
reg.Dispose();
}

/// <summary>
/// 把六角图连接起来
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="targetFile"></param>
private void createComposePic(string sourceFile, string targetFile)
{
Bitmap image = new Bitmap(sourceFile);
Bitmap resultImage = new Bitmap(image.Width * 8, image.Height * 4);
int width = image.Width;
int height = image.Height;
//int xx = width /4;
//int yy = height/2;
int xx = 0;
int yy = 0;

float extWidth =(float) width * 3/4;
float extHeight = (float)(height * 0.433);

//建立缓冲图片
Graphics gr = Graphics.FromImage(resultImage);
gr.Clear(Color.FromArgb(255, Color.White));

int ii = 0;
List<g> lists = new List<g>();

g tmpg = new g();

int tmpXX = 0;
int tmpYY = 0;

while (true)
{
gr.ResetTransform();

if (ii%2==0)
{
gr.TranslateTransform(extWidth * ii, 0);
gr.DrawImage(image, xx, yy);
tmpXX = Convert.ToInt32(xx + extWidth * ii);
tmpYY = yy;
tmpg = GetCoordinate(image.Height, image.Width, tmpXX, tmpYY);

gr.DrawImage(image, xx, Convert.ToInt32(height * 0.866 + yy));
tmpXX = Convert.ToInt32(xx + extWidth * ii);
tmpYY = yy+Convert.ToInt32(height * 0.866 + yy);
tmpg = GetCoordinate(image.Height, image.Width, tmpXX, tmpYY);

gr.DrawImage(image, xx, Convert.ToInt32(height * 0.866 * 2 + yy));
tmpXX = Convert.ToInt32(xx + extWidth * ii);
tmpYY = yy+Convert.ToInt32(height * 0.866 * 2 + yy);
tmpg = GetCoordinate(image.Height, image.Width, tmpXX, tmpYY);
}
else
{
gr.TranslateTransform(extWidth * ii, extHeight);
gr.DrawImage(image, xx, yy);
tmpXX = Convert.ToInt32(xx + extWidth * ii);
tmpYY = Convert.ToInt32(yy + extHeight);
tmpg = GetCoordinate(image.Height, image.Width, tmpXX, tmpYY);

gr.DrawImage(image, xx, Convert.ToInt32(height * 0.866 + yy));
tmpXX = Convert.ToInt32(xx + extWidth * ii);
tmpYY = Convert.ToInt32(yy + extHeight + Convert.ToInt32(height * 0.866 + yy));
tmpg = GetCoordinate(image.Height, image.Width, tmpXX, tmpYY);
}
ii++;
if (extWidth*ii>width*7)
{
if (lists.Count>0)
{
string ss = string.Empty;
foreach (g gi in lists.ToArray())
{
ss += gi.ToString() + "/r/n";
}
this.textBox1.Text = ss;
DrawTestPic(lists.ToArray(), image);
}

break;
}

}
resultImage.Save(targetFile, ImageFormat.Jpeg);

}

/// <summary>
/// 测试画的图是否符合要求的连接起来
/// </summary>
/// <param name="pics"></param>
/// <param name="img"></param>
private void DrawTestPic(g[] pics,Bitmap img)
{
string targetf = AppDomain.CurrentDomain.BaseDirectory + @"/" + "bb2_Compose_test.jpg";
Bitmap image = img;
Bitmap resultImage = new Bitmap(image.Width * 8, image.Height * 4);
Graphics gr = Graphics.FromImage(resultImage);
gr.Clear(Color.FromArgb(255, Color.White));
Rectangle resultRectangle = new Rectangle(0, 0, resultImage.Width, resultImage.Height);
GraphicsPath gp = new GraphicsPath();
for (int i = 0; i < pics.Length; i++)
{
Point[] pp = new Point[] {
pics[i].One,
pics[i].Two,
pics[i].Three,
pics[i].Four,
pics[i].Five,
pics[i].Six,
pics[i].One
};

}
gr.DrawPath(Pens.Black, gp);
resultImage.Save(targetf, ImageFormat.Jpeg);
gp.Dispose();

}

private g GetCoordinate(int height, int width, int tmpx, int tmpy)
{
g tmpg = new g();
int tmpXX = tmpx;
int tmpYY = tmpy;

tmpg.One = new Point(tmpXX, tmpYY + height / 2);
tmpg.Two = new Point(tmpXX + width / 4, Convert.ToInt32(tmpYY + height * 0.067));
tmpg.Three = new Point(tmpXX + width * 3 / 4, Convert.ToInt32(tmpYY + height * 0.067));
tmpg.Four = new Point(tmpXX + width, tmpYY + height / 2);
tmpg.Five = new Point(tmpXX + width * 3 / 4, Convert.ToInt32(tmpYY + height * 0.933));
tmpg.Six = new Point(tmpXX + width / 4, Convert.ToInt32(tmpYY + height * 0.933));

return tmpg;
}

private void CreateMultiAnglePic(string sourceFile,string targetFile,int style)
{
Bitmap image = new Bitmap(sourceFile);
Bitmap resultImage = new Bitmap(image.Width, image.Height);

//建立缓冲图片
Graphics gr = Graphics.FromImage(resultImage);
//gr.Clear(ColorTranslator.FromHtml("#00FF00"));
gr.Clear(Color.FromArgb(0, Color.Transparent));

Rectangle resultRectangle = new Rectangle(0, 0, image.Width, image.Height);
//gr.FillRectangle(Brushes.Transparent, resultRectangle);

int width = image.Width;
int height = image.Height;
//int yy = height / 2;
//int xx = 0;
//Point[] myArray =
// {
//     new Point(xx, yy),
//     new Point(Convert.ToInt32(yy*0.75), xx),
//     new Point(width-Convert.ToInt32(yy*0.75),xx),
//     new Point(width, yy),
//     new Point(width-Convert.ToInt32(yy*0.75), height),
//     new Point(Convert.ToInt32(yy*0.75), height),
//     new Point(xx, yy),
// };

Region reg = new Region();
reg.MakeEmpty();
GraphicsPath gp = new GraphicsPath();
if (style!=Circle)
{
Point[] myArray = GetArrayForViewStyle(style, width, height);
}
else
{
}
reg.Union(gp);

gr.SetClip(reg, CombineMode.Replace);
gr.DrawImage(image, resultRectangle);

resultImage.Save(targetFile, ImageFormat.Png);

gp.Dispose();
reg.Dispose();
}

private Point[] GetArrayForViewStyle(int angleStyle,int width,int height)
{
Point[] tmp = new Point[] { };
int yy = height / 2;
int xx = 0;
switch (angleStyle)
{
case SixAngle:
tmp=new Point[] {
new Point(xx, yy),
new Point(Convert.ToInt32(width*0.25), Convert.ToInt32(yy*0.134)),
new Point(Convert.ToInt32(width*0.75),Convert.ToInt32(yy*0.134)),
new Point(width, yy),
new Point(Convert.ToInt32(width*0.75), Convert.ToInt32(yy*1.866)),
new Point(Convert.ToInt32(width*0.25), Convert.ToInt32(yy*1.866)),
new Point(xx, yy)
};
break;
case ThreeAngle:
tmp = new Point[] {
new Point(0, 0),
new Point(width, 0),
new Point(width/2,height),
new Point(0, 0)
};
break;
case FourAngle:
tmp = new Point[] {
new Point(xx, yy),
new Point(width/2, xx),
new Point(width,yy),
new Point(width/2, height),
new Point(xx, yy)
};
break;
}
return tmp;
}

• 0
点赞
• 2
收藏
觉得还不错? 一键收藏
• 打赏
• 3
评论
01-06
04-14 552
01-06 659
11-13
03-09 203
02-10 213
01-05 6613
12-07
10-09
08-11
07-07 1250
02-22 453

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

simonllf

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。