Jason_liu

导航

窗体异形重绘

 

//调用

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);
}

posted on 2012-11-27 19:08  Jason_liu  阅读(218)  评论(0编辑  收藏  举报