Jose Cancel
Jose Cancel

Reputation: 151

How to create a smooth circle picture box in C#?

I'm currently designing a program in "Visual Studio 2015 C#" and would like to implement circular picture boxes. In the program there are picture boxes which source image is downloaded from the web. In it's default state, the "picturebox" is square. I would like to know how I could change the shape of the "picturebox" to a circle. That way the image when loaded will be circular instead of squared or rectangular.

I already figured out how to downscale the image and keep it's quality and center it, but I do not know how to change the "picturebox" into a circle.

I found a couple ways to do it, but unfortunately with this methods the circle is not smooth, instead it is pixelated.

This is an example of what I would like: https://i.sstatic.net/ecjAq.png

Upvotes: 3

Views: 7792

Answers (1)

Baronz
Baronz

Reputation: 677

You create a path (that is a circle, or whatever shape you want), and set the picturebox region to that path. Here's an example with a circle:

 public partial class Form1 : Form {
     public Form1() {
       InitializeComponent();
       System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
       path.AddEllipse(0, 0, pictureBox1.Width, pictureBox1.Height);
       pictureBox1.Region = new Region(path);
     }
   }

This StackExchange post has how to make any path smooth for a picturebox:

Possible to have anti-aliasing when drawing a clipped image?

Upvotes: 5

Related Questions