如何在C#中使用graphics.DrawImage从多边形裁剪中删除黑色区域

时间:2019-07-11 09:00:42

标签: c# graphics

 GraphicsPath gp = new GraphicsPath();   // a Graphicspath
                gp.AddPolygon(points.ToArray());        // with one Polygon
                RectangleF rectangle = gp.GetBounds();//selected rectangle
                byte[] imageByte = Convert.FromBase64String(ImageString);//image bytes
                Bitmap result = new Bitmap(1600, 1200, PixelFormat.Format32bppPArgb);//Graphics image to draw

                using (MemoryStream mStream = new MemoryStream())
                {
                    mStream.Write(imageByte, 0, Convert.ToInt32(imageByte.Length));//main image 

                    using (Bitmap bmp = new Bitmap(mStream, false))
                    {
                        using (Graphics graphics = Graphics.FromImage(result))
                        {
                            graphics.Clip = new System.Drawing.Region(gp);//setclip GraphicsPath
                            graphics.DrawImage(bmp,0,0);//draw image

                            Bitmap cropBmp = result.Clone(rectangle, PixelFormat.Format16bppArgb1555);//clone selcted image to small size
                            cropBmp.Save("C:\\QualityTest\\switchIcon.jpg", ImageFormat.Jpeg);//save image
                            //GetImageStringFromBitmap(cropBmp);
                            gp.Dispose();
                            return cropBmp;
                        }
                    }
                }

在多边形裁剪之后,附加到具有矩形大小的新位图(cropBmp),但多余区域显示为黑色。

0 个答案:

没有答案