1. 程式人生 > 其它 >關於兩個PictureBox重疊後透明的問題

關於兩個PictureBox重疊後透明的問題

我的系統之前就有這麼個問題,當兩個圖片重疊的時候,上面的圖片總是將下面的圖片覆蓋,之前因為忙一直沒有騰出時間處理一下

 

 

今天實在看不下去了,然後本來想自己重新寫一個,但是問了技術群的大佬,給重寫了一個PictureBoxDE 類,效果如下

 

 

 完美的解決了圖片的透明,下面我將把這個重寫的類發出來,供大家使用參考,也順便做一下記錄

 1 public class ReiongCtr : PictureBox
 2     {
 3         protected override void OnSizeChanged(EventArgs e)
 4         {
 5
base.OnSizeChanged(e); 6 if (BackgroundImage != null) 7 { 8 Bitmap bit = (Bitmap)BackgroundImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero); 9 Region = ImageToRegion(bit, Color.FromArgb(0, 0, 0, 0)); 10 } 11 }
12 protected override void OnBackgroundImageChanged(EventArgs e) 13 { 14 base.OnBackgroundImageChanged(e); 15 if (BackgroundImage != null) 16 { 17 Bitmap bit = (Bitmap)BackgroundImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
18 Region = ImageToRegion(bit, Color.FromArgb(0, 0, 0, 0)); 19 } 20 } 21 22 /// <summary> 23 /// 根據圖片計算Region路徑(低效率) 24 /// </summary> 25 /// <param name="img">影象資源</param> 26 /// <param name="TranColor">欲透明掉的顏色</param> 27 /// <returns>一個離散的路徑資訊</returns> 28 public static Region ImageToRegion(Image imgx, Color TranColor) 29 { 30 if (imgx == null) return null; 31 Region rRegion = new Region(); 32 rRegion.MakeEmpty(); 33 34 35 int ImWidth = imgx.Width; 36 int ImHeight = imgx.Height; 37 Color curColor; 38 Rectangle curRect = new Rectangle(); 39 curRect.Height = 1; 40 bool isTransRgn; 41 42 for (int y = 0; y < ImHeight; y++) 43 { 44 isTransRgn = true; 45 for (int x = 0; x < ImWidth; x++) 46 { 47 curColor = ((Bitmap)imgx).GetPixel(x, y); 48 if (curColor == TranColor || x == ImWidth - 1)//如果遇到透明色或行尾 49 { 50 if (isTransRgn == false)//退出有效區 51 { 52 curRect.Width = x - curRect.X; 53 rRegion.Union(curRect); 54 } 55 } 56 else//非透明色 57 { 58 if (isTransRgn == true)//進入有效區 59 { 60 curRect.X = x; 61 curRect.Y = y; 62 } 63 }//if curColor 64 isTransRgn = curColor == TranColor; 65 } 66 } 67 return rRegion; 68 } 69 }