Click here to Skip to main content
15,887,861 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear sir,
I want to create circular picture box in c#.Can it be possible to draw picture box.
how can i do.
Posted
Updated 28-Feb-17 1:44am

1 solution

The easiest way is to set the Region:
C#
myPictureBox.Image = Image.FromFile(@"D:\Temp\MyPic.jpg");
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(myPictureBox.DisplayRectangle);
myPictureBox.Region = new Region(gp);


I did this control to remove the pixels on the edges
C#
public class CirclePictureBox : PictureBox
        {
            public CirclePictureBox ()
            {
            }

            protected override void OnPaint(PaintEventArgs e)
            {
                System.Drawing.Brush brushImege;
                try
                {
                    Bitmap Imagem = new Bitmap(this.Image);
                    //get images of the same size as control
                    Imagem = new Bitmap(Imagem, new Size(this.Width - 1, this.Height - 1));
                    brushImege = new TextureBrush(Imagem);
                }
                catch
                {
                    Bitmap Imagem = new Bitmap(this.Width - 1, this.Height - 1, PixelFormat.Format24bppRgb);
                    using (Graphics grp = Graphics.FromImage(Imagem))
                    {
                        grp.FillRectangle(
                            Brushes.White, 0, 0, this.Width - 1, this.Height - 1);
                        Imagem = new Bitmap(this.Width - 1, this.Height - 1, grp);
                    }
                    brushImege = new TextureBrush(Imagem);
                }
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                GraphicsPath path = new GraphicsPath();
                path.AddEllipse(0, 0, this.Width - 1, this.Height - 1);
                e.Graphics.FillPath(brushImege, path);
                e.Graphics.DrawPath(Pens.Black, path);
            }
        }
 
Share this answer
 
v3
Comments
tusharkaushik 29-Jul-12 6:17am    
Very very thanks in advance...! where i have to write this code i mean in which event of which control.
OriginalGriff 29-Jul-12 7:23am    
That is entirely up to you. Form load perhaps?
po1725 13-Nov-13 9:10am    
i got a problem there is some pixilation on the borders which
looks very bad I can not add a antialiased. can help?
Sergey Alexandrovich Kryukov 29-Jul-12 23:43pm    
Why "in advance"? As if the answer was not yet given to you...
--SA
tusharkaushik 30-Jul-12 10:29am    
Tnx in advance! u gave me a valuable hint.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900