近日在項目中,一直被一個問題搞得頭大的很,美工要把按鈕圖片弄成不規則的,但是在winform里實現又不僅僅是使用簡單的png圖片而已。在網上找到一些方法,稍微改了一點加工成項目所需。
貼出解決方案,以供日后使用:
public class BitmapRegion { //創建支持位圖區域的控件(目前有button,form,imagebutton) public static void CreateControlRegion(Control control, Bitmap bitmap) { //判斷控件是否存在 if (control == null )//|| bitmap == null return; //控件大小設置為位圖大小 control.Width = bitmap.Width; control.Height = bitmap.Height; // 檔控件為form時 if (control is System.Windows.Forms.Form) { //強制轉化為form Form form = (Form)control; //當FORM的邊界FormBorderStyle不為NONE時,應將FORM的大小設置成比位圖大小稍大一點 form.Width += 15; form.Height += 35; //設置form為沒有邊界 form.FormBorderStyle = FormBorderStyle.None; //將位圖設置為控件背景圖 form.BackgroundImage = bitmap; //計算位圖中不透明的部分 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); //應用新的區域 form.Region = new Region(graphicsPath); } //當控件是panel時 else if (control is System.Windows.Forms.Panel) { //強制轉化為panel Panel form = control as Panel; //當FORM的邊界FormBorderStyle不為NONE時,應將FORM的大小設置成比位圖大小稍大一點 //form.Width += 15; //form.Height += 35; //form.Width += 15; //form.Height += 35; // 設置panel為沒有邊界 form.BorderStyle = BorderStyle.None; //將位圖設置為控件背景圖 form.BackgroundImage = bitmap; //計算位圖中不透明的部分 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); //應用新的區域 form.Region = new Region(graphicsPath); } //當控件是button時 else if (control is System.Windows.Forms.Button) { // 強制轉化為button Button button = (Button)control; // 不顯示button文字 button.Text = ""; // 改變cursor的style button.Cursor = Cursors.Hand; // 設置button的背景圖片 button.BackgroundImage = bitmap; // 計算圖中不透明部分 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); // 應用新的區域 button.Region = new Region(graphicsPath); } //當控件是imagebutton時 else if (control is M3Host.view.utils.ImageButton) { M3Host.view.utils.ImageButton button = control as M3Host.view.utils.ImageButton; // 不顯示文字 button.Text = ""; // 改變模式和設置正常狀態下的圖片為空 button.Cursor = Cursors.Hand; button.NormalImage = null; // 設置位圖為背景圖片 button.BackgroundImage = bitmap; // 計算圖中不透明部分 GraphicsPath graphicsPath = CalculateControlGraphicsPath(bitmap); // 應用新的區域 button.Region = new Region(graphicsPath); } } // 計算位圖中不透明部分 private static GraphicsPath CalculateControlGraphicsPath(Bitmap bitmap) { // 創建graphicsPath GraphicsPath graphicsPath = new GraphicsPath(); // 取得左上角的第一個點作為透明點 Color colorTransparent = bitmap.GetPixel(0, 0); // 第一個找到的點 int colOpaquePixel = 0; // 遍歷所有Y方向的點 for (int row = 0; row < bitmap.Height; row++) { // 重設 colOpaquePixel = 0; // 遍歷X方向的所有點 for (int col = 0; col < bitmap.Width; col++) { // 如果不是透明點,則繼續遍歷 if (bitmap.GetPixel(col, row) != colorTransparent) { // 記錄當前點 colOpaquePixel = col; // 新建變量記錄當前點 int colNext = col; // 從找到的不透明點開始,繼續尋找不透明點,一直到找到或則達到圖片寬度 for (colNext = colOpaquePixel; colNext < bitmap.Width; colNext++) if (bitmap.GetPixel(colNext, row) == colorTransparent) break; // 將不透明點加到graphics path graphicsPath.AddRectangle(new Rectangle(colOpaquePixel, row, colNext - colOpaquePixel, 1)); //覆蓋前一個點 col = colNext; } } } return graphicsPath; } }
使用方法:Bitmap bmp = new Bitmap("images\\move1.bmp");
BitmapRegion.CreateControlRegion(控件名, bmp);
再附上將控件制成bitmap的方法
Bitmap bmp = new Bitmap(Width, Height); Rectangle r = new Rectangle(0, 0, Width,Height);//width和height均為控件的寬和高 控件名.DrawToBitmap(bmp, r);
記錄以上。。