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

这篇博客介绍了如何使用C#的GDI+库将图片裁剪成六角形,并展示了如何将多个六角形图像拼接起来。通过GraphicsPath和Region对象,实现了图形的裁剪和组合,最终保存为PNG或JPEG格式。代码中还提供了生成三角形和菱形图形的辅助方法。
摘要由CSDN通过智能技术生成

最近客户提出个小需求,把输入的图片截成六角形输出,于是上网找了下资料结合自己弄过的一些例子,做了出来.顺便放在这里.以备以后方便查阅.如果刚好可以帮到谁的.那就最好了,好了,废话少说,贴代码:(需要源码的可以到我的资源里找)

 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值