vb.net- 如何把图片框里的图片变成圆形

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

我正在 vb.net 开发一个具有良好用户界面的应用程序,包括带有图片框的您的帐户图标。我的问题是如何让图片框里的图片变成圆形?

像这样: https://drive.google.com/open?id=1rUBq68ULDkTiFFv2uEiV_oQIh3wQIfhd

我通常不会为没有尝试的问题提供代码,但答案中有几个步骤,而且自己编写代码比解释它们更容易。例如

'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

这将创建一个与原始图像宽度和高度相同的椭圆形图像。如果原件是方形的,那么最终的将是圆形的。如果你想要一个圆而不考虑原件的纵横比,那么你必须以适当的方式操纵它。

虽然我看到您已经找到了问题的解决方案,但是这里有一个非常简单的解决方法。

假设,您的项目在 WinForms 中,并且您正在使用 Picture Box 控件。实现圆形图像外观的最简单方法是将 PictureBoxImage 属性 设置为圆形图像,中间为 blank/transparent外面是白色的。然后,无论您将什么图像设置为 BackgroundImage ,它都会显示为圆形。

这里有一个示例圆形透明图像,您可以将其设置为 Image(确保将 SizeMode 设置为拉伸),然后将任意图片设置为 BackgroundImage