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

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

 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 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);
                    lists.Add(tmpg);

                    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);
                    lists.Add(tmpg);

                    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);
                    lists.Add(tmpg);
                }
                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);
                    lists.Add(tmpg);

                    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);
                    lists.Add(tmpg);
                }
                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
                    };
                gp.AddPolygon(pp);

            }
            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);
                gp.AddPolygon(myArray);
            }
            else
            {
                gp.AddEllipse(0, 0, width, height);
            }
            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;
        }

 

 

基本符合要求,而且还附加了切割成三角形和菱形的.

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页