110,545
社区成员
发帖
与我相关
我的任务
分享
public Form1()
{
InitializeComponent();
//pictureBox1在设计界面添加
System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();
shape.AddEllipse(0, 0, pictureBox1.Width, pictureBox1.Height);
pictureBox1.Region = new System.Drawing.Region(shape);
}
// <summary>
/// 根据背景图像设置窗体的不规则形状
/// </summary>
/// <param name="bitmap">背景图像</param>
/// <param name="transColor">指定颜色为透明的</param>
/// <returns></returns>
protected Region SetMainFormRegionFormBitmap(Bitmap bitmap, Color transColor)
{
GraphicsPath FormPath = new GraphicsPath();
try
{
for (int y = 0; y < this.Height; y++)
{
for (int x = 0; x < this.Width; x++)
{
if (bitmap.GetPixel(x, y) == transColor)
continue;
int x0 = x;
while ((x < this.Width) && (bitmap.GetPixel(x, y) != transColor))
x++;
FormPath.AddRectangle(new Rectangle(x0, y, x - x0, 1));
}
}
}
catch
{
}
Region regi = new Region(FormPath);
FormPath.Dispose();
return regi;
}
//在构造函数里调用即可
this.Region = this.SetMainFormRegionFormBitmap((Bitmap)BackgroundImage, Color.FromArgb(0, 0, 255));