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;
        }

 

 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章