How to create image with rounded corners in C#?

前端 未结 5 1386
耶瑟儿~
耶瑟儿~ 2020-12-09 21:01

I\'d like to create image (from another one) with rounded corners with GDI+. What\'s the best way to do this?

PS: it\'s not for web, so I cannot make use of client C

5条回答
  •  醉话见心
    2020-12-09 21:06

    Using the Graphics.SetClip() method is the best way. For example:

        public static Image OvalImage(Image img) {
            Bitmap bmp = new Bitmap(img.Width, img.Height);
            using (GraphicsPath gp = new GraphicsPath()) {
                gp.AddEllipse(0, 0, img.Width, img.Height);
                using (Graphics gr = Graphics.FromImage(bmp)) {
                    gr.SetClip(gp);
                    gr.DrawImage(img, Point.Empty);
                }
            }
            return bmp;
        }
    

提交回复
热议问题