Search code examples
vb.netroundingpicturebox

vb.net- How to make any picture in a picture box round in shape


I am making an application in vb.net that has a good user interface, including the your account icon that includes a picture box. My question is that how can i make that image in the picture box round in shape?

Like This: https://drive.google.com/open?id=1rUBq68ULDkTiFFv2uEiV_oQIh3wQIfhd


Solution

  • I wouldn't normally provide code for a question that doesn't show an attempt but there are a few steps in the answer and it was easier to just write the code myself than explain them. E.g.

    'Get the original image.
    Dim originalImage = PictureBox1.Image
    
    'Create a new, blank image with the same dimensions.
    Dim croppedImage As New Bitmap(originalImage.Width, originalImage.Height)
    
    'Prepare to draw on the new image.
    Using g = Graphics.FromImage(croppedImage)
        Dim path As New GraphicsPath
    
        'Create an ellipse that fills the image in both directions.
        path.AddEllipse(0, 0, croppedImage.Width, croppedImage.Height)
    
        Dim reg As New Region(path)
    
        'Draw only within the specified ellipse.
        g.Clip = reg
        g.DrawImage(originalImage, Point.Empty)
    End Using
    
    'Display the new image.
    PictureBox2.Image = croppedImage
    

    That will create an elliptical image with the same width and height as the original. If the original is square then the final will be circular. If you want a circle regardless of the aspect ratio of the original then you'll have to manipulate that in the appropriate manner.