现在的位置: 首页 > 综合 > 正文

窗体异形重绘

2012年12月21日 ⁄ 综合 ⁄ 共 848字 ⁄ 字号 评论关闭

 

//调用

private void Form2_Load(object sender, EventArgs e)
{
  this.Region= GetFormRegoin(this,Resource2.img4, Resource2.img4.GetPixel(0, 0));
}

 

//重绘主方法

private Region GetFormRegoin(Form form,Bitmap img,Color bgColor)
{
  Bitmap bitImg = img;
  form.Width = bitImg.Width;
  form.Height = bitImg.Height;
  form.BackgroundImage = bitImg;
  GraphicsPath graphicsPath = new GraphicsPath();

  for (int y = 0; y < bitImg.Height; y++)
  {
    int start = 0;
    for (int x = 0; x < bitImg.Width; x++)
    {
      if (bitImg.GetPixel(x, y) != bgColor)
      {
        start = x;
        int end = 0;
        for (end = start; end < bitImg.Width; end++)
        {
          if (bitImg.GetPixel(end, y) == bgColor)
          {
            break;
          }
        }
        x = end;
        //添加有效区域到graphicsPath
        graphicsPath.AddRectangle(new Rectangle(start, y, end - start, 1));
      }
    }
  }
  //根据graphicsPath重绘窗体区域
  return new Region(graphicsPath);
}

抱歉!评论已关闭.