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.
UPDATE thanks to @TaW I've updated g.SetClip(path)
instead of new region
Make a new Bitmap that matches the original in size and pixel format.
Create a graphics from that new Bitmap.
Set the graphics
Clip to a new circle
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;
}
}