Tạo hình dáng của form theo ý muốn từ các file ảnh

Bạn muốn tạo ra ứng dụng với các form với hình dáng đẹp, không phải dạng hình chữ nhật như bình thường. Bài viết này sẽ giúp bạn làm điều đó.

Có 2 cách để làm việc này:

Cách 1: Bạn dùng Window Region để tạo hình dáng form theo ý muốn.

Cụ thể hơn, bạn có một hình bất kỳ theo ý muốn, các phần không muốn hiển thị sẽ có một màu đặc biệt nào đó (Key color, Transparent coloer), dùng thuật toán tạo Region từ hình đó, set giá trị Region cho form.

Code:

public static Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)

{

if (bitmap == null)

       throw new ArgumentNullException("Bitmap", Strings.BitmapCannotBeNull);

int height = bitmap.Height;

int width = bitmap.Width;

GraphicsPath path = new GraphicsPath();

for (int j=0; j<height; j++ )

     for (int i=0; i<width; i++)

     {

             if (bitmap.GetPixel(i, j) == transparencyColor)

                     continue;

             int x0 = i;

             while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor))

             i++;

             path.AddRectangle(new Rectangle(x0, j, i-x0, 1));

       }

Region region = new Region(path);

path.Dispose();

return region;

}

}

Cách dùng:

Code:

Bitmap background = new Bitmap("background.gif");

this.Region = GraphicsUtility.BitmapToRegion(background, Color.Green);

Cách 2: Dùng Layered windows. Bạn tham khảo cách này ở bài viết này:

http://www.codeproject.com/cs/media/perpxalpha_sharp.asp

About ""

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus suscipit, augue quis mattis gravida, est dolor elementum felis, sed vehicula metus quam a mi. Praesent dolor felis, consectetur nec convallis vitae.

Post a Comment

Breaking

 
Copyright © 2013 Binhbk230