0

Am a newbie when it comes to c#, I was wondering if it's possible to make a circular shaped pictureBox in c# winforms. I am making a simple Software where people could choose pictures and set it to a picturebox. So I can't ask every user to go on Photoshop and make their image circular. Is it possible to make a Circular pictureBox which Crop the picture and update the picture file which should be in PNG Format with transparent background?

My current code to set the image in the pictureBox1 on button1 Click Event:

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog OFD = new OpenFileDialog();
        if (OFD.ShowDialog() == DialogResult.OK)
        {
            Bitmap Image = new Bitmap(OFD.FileName);
            pictureBox1.Image = Image;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }
    }

I have no Idea on how I could do this or where to start searching! Some help will be grateful.

The Little Cousin
  • 422
  • 2
  • 7
  • 20
  • What's your question exactly? **1-** Create a circular `PictireBox`? **2-** Show an image in a circular shape (and the original image remains untouched, but shows circular) **3-** Edit an image programmatically and make it circular? – Reza Aghaei Dec 27 '15 at 13:17
  • If your requirement is showing an image in a circular shape, you don't need edit and save original image to be circular. You can simply show the image in a circular shape in your `PictureBox`. Also making the `PictureBox` circular may satisfy the requirement. – Reza Aghaei Dec 27 '15 at 13:20
  • 3. Edit the image programmatically, change it's format to PNG with transparent background on circular shape. @Reze Aghaei – The Little Cousin Dec 27 '15 at 13:21
  • @Reze Aghaei , I'll need it edited to round and PNG format for other uses in the software later on that's y! – The Little Cousin Dec 27 '15 at 13:26
  • So you should edit the question and remove those part that you said you need circular `PictureBox` and remain only those parts that talking about editing the picture and making it circular. – Reza Aghaei Dec 27 '15 at 13:28
  • Extensive editing leads to questions and answers being replaced and therefor hard to follow and find. This is why SO puts so much emphasis on asking well formulated questions and answers. Please, next time, put more work into writing the question and answering. In the end this will lead to a post that is useful to a larger audience. – Emond Dec 27 '15 at 15:24

1 Answers1

2

UPDATE thanks to @TaW I've updated g.SetClip(path) instead of new region

  1. Make a new Bitmap that matches the original in size and pixel format.
  2. Create a graphics from that new Bitmap.
  3. Set the graphics

  4. clip to a new circle

  5. Draw the original image onto the new graphics.

Here is an example:

public Bitmap ClipToCircle(Bitmap original, PointF center, float radius)
{
    Bitmap copy = new Bitmap(original);
    using (Graphics g = Graphics.FromImage(copy)) {
        RectangleF r = new RectangleF(center.X - radius, center.Y - radius,
                                      radius * 2, radius * 2);
        GraphicsPath path = new GraphicsPath();
        path.AddEllipse(r);
        g.SetClip(path)
        g.DrawImage(original, 0, 0);
        return copy;
    }
}

I hope it solves your issue.

DeJaVo
  • 3,091
  • 2
  • 17
  • 32
  • 2
    Please, next time, do not copy another answer just flag the question as a duplicate. – Emond Dec 27 '15 at 13:13
  • @The_Little_Cousin please provide additional details if my answer is not suffice. – DeJaVo Dec 27 '15 at 13:15
  • @DeJaVo Have a look I was updating it while u answered sorry. – The Little Cousin Dec 27 '15 at 13:17
  • @ErnodeWeerd based on OP comment, it seems the question is not a duplicate of [Rounded edges in picturebox C#](http://stackoverflow.com/questions/7731855/rounded-edges-in-picturebox-c-sharp) – Reza Aghaei Dec 27 '15 at 13:25
  • @DeJav, Am a newbie, what you just said is like non-human to me, could u explain in another way with Codes examples ect? – The Little Cousin Dec 27 '15 at 13:33
  • 2
    You need to add either text or a line '--------' after each list before you can format code. (An old MarkUp bug) - Also: Instead of a Region simply use G.SetClip(path) 1 – TaW Dec 27 '15 at 13:37
  • @The_Little_Cousin Does my update solution address your issue? if yes, please mark it a a solution :) – DeJaVo Dec 27 '15 at 13:49
  • @ErnodeWeerd please un-flag it since the question was unclear and after editing it my first answer was irrelevant. – DeJaVo Dec 27 '15 at 13:50
  • @ErnodeWeerd , After some edits, it seem the question is not a duplicate and I manage to get the correct answer. I think the question will help others as well in the future. Thanks – The Little Cousin Dec 27 '15 at 13:59
  • 1
    Extensive editing leads to questions and answers being replaced and therefor hard to follow and find. This is why SO puts so much emphasis on asking well formulated questions and answers. Please, next time, put more work into writing the question and answering. In the end this will lead to a post that is useful to a larger audience. – Emond Dec 27 '15 at 15:24