2017-04-12 37 views
2

我試圖將窗體窗體轉換爲透明窗體,並使其只顯示一個對象。但它仍然在我的物體周圍有一條線(中風),它並不是我想要的那麼完美。我如何取出線(中風)? (附照片比較。)從透明窗體上的圖像中刪除輪廓

enter image description here

這裏是我的代碼:

private void Form1_Load(object sender, EventArgs e) 
{ 
    this.FormBorderStyle = FormBorderStyle.None; 
    this.Width = this.pictureBox1.Width; 
    this.Height = this.pictureBox1.Height; 
    SetStyle(ControlStyles.SupportsTransparentBackColor, true); 
    this.BackColor = Color.Black; 
    this.TransparencyKey = this.pictureBox1.BackColor; 
} 
+1

很確定這與圖像有關,而不是代碼,圖像本身的輪廓 –

+0

您可以發佈有關圖像格式的細節嗎?所以我們可以知道它是否支持alpha通道,這可能是這張圖片邊緣問題最可能的原因,沒有像png文件那樣的適當的alpha通道。消除鋸齒將永遠不會正確渲染。反鋸齒就是你需要在背景透明的情況下將曲面邊緣與背景混合在一起,需要透明背景的Alpha需要在邊緣具有半透明的透明度,以達到這一目的,因爲你無法知道背景是什麼。 – Lorien

+0

是的,格式文件是.png。我從互聯網上拿走了它。 http://www.pngmart.com/image/16212 – Wesley

回答

1

您的圖像具有半透明像素。 TransparencyKey只會使一個顏色透明。所以邊緣的像素將顯示圖像的色彩和Parent控件或窗體的顏色的混合..

這裏是一個函數,使它們完全透明消除了所有的半透明像素:

using System.Runtime.InteropServices; 
.. 

public static void UnSemi(Bitmap bmp) 
{ 
    Size s = bmp.Size; 
    PixelFormat fmt = bmp.PixelFormat; 
    Rectangle rect = new Rectangle(Point.Empty, s); 
    BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, fmt); 
    int size1 = bmpData.Stride * bmpData.Height; 
    byte[] data = new byte[size1]; 
    System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size1); 
    for (int y = 0; y < s.Height; y++) 
    { 
     for (int x = 0; x < s.Width; x++) 
     { 
      int index = y * bmpData.Stride + x * 4; 
      // alpha, threshold = 255 
      data[index + 3] = (data[index + 3] < 255) ? (byte)0 : (byte)255; 
     } 
    } 
    System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length); 
    bmp.UnlockBits(bmpData); 
} 

請注意,這也意味着漂亮的消除鋸齒的外觀會變得有些粗糙。

另請注意,該例程採用32位ARGB像素格式,因爲PNGs通常會有。

最後請注意,由於圖像有lotBlack您應該選擇不同的顏色。 Fuchsia是在野外龍的世界,而罕見的,但也許不是,你想挑一些隨機的顏色..

另外:你要設置的pictureBox1.BackColor = Color.Transparent ..

最後:有時它是有道理的到threshold參數添加到函數簽名來設置從開啓阿爾法所有或關閉一個水平..

下面是一個使用例如:

this.BackColor = Color.FromArgb(1,2,3,4); 
this.TransparencyKey = this.BackColor; 
UnSemi((Bitmap)this.pictureBox1.Image); 

enter image description here

+0

仍然不行。我甚至嘗試了透明的圓形,而且沒有任何變化。 :( – Wesley

+0

如果你不介意,你可以在c#中快速創建一個,然後發給我那個文件?所以我可以看到它,謝謝。 – Wesley

+0

請參閱我的更新! – TaW

0

使用PNG圖像實現的具有透明背景
設置您的控制/表格背景色和透明按鍵顏色這在圖像中不存在

+0

我做到了,但這是同樣的問題。沒有改變。 – Wesley