最近客户提出个小需求,把输入的图片截成六角形输出,于是上网找了下资料结合自己弄过的一些例子,做了出来.顺便放在这里.以备以后方便查阅.如果刚好可以帮到谁的.那就最好了,好了,废话少说,贴代码:(需要源码的可以到我的资源里找)
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();
gp.AddPolygon(myArray);
reg.Union(gp);
gr.SetClip(reg, CombineMode.Replace);
gr.DrawImage(image, resultRectangle);
resultImage.Save(targetFile, ImageFormat.Png);
gp.Dispose();
reg.Dispose();
}
比较乱.不过只是做个参考,修修改改的自己来吧.这里有个技巧,当输出是PNG格式的时候背景透明的效果才能在PHOTOSHOP看的出来,如果是JPG的.会发现背景是黑色的.
修改:
现在需求又变了(咋总给我碰到=,=),需要把六角型的图连起来,恩..其实也有点意思.废话不说.上码:
/// <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