2013-08-16 18 views
-1

我喜歡使白色背景透明。但是有些白人不會被刪除。我如何去除其他類似的白色?如何使圖像背景透明並可以調整模糊係數?

我的代碼 -

string currentPath = Environment.CurrentDirectory; 
int width = pictureBox1.Width; 
int height = pictureBox1.Height; 
Bitmap bm = new Bitmap(width, height); 
pictureBox1.DrawToBitmap(bm, new System.Drawing.Rectangle(0, 0, width, height)); 

bm.MakeTransparent(System.Drawing.Color.White); 
System.Drawing.Image img = (System.Drawing.Image)bm; 
img.Save(currentPath + "\\temp\\logo.png", ImageFormat.Png); 
+2

聽起來像是你想Alpha透明度。 – leppie

回答

1

你可以使用Bitmap.GetPixel()Bitmap.SetPixel(),使非白色的顏色是透明的。例如:

for (int x = 0; x < bm.Width; x++) 
{ 
    for (int y = 0; y < bm.Height; y++) 
    { 
     Color c = bm.GetPixel(x, y); 
     if ((c.B + c.R + c.G > 660)) 
      c = Color.FromArgb(0, c.R, c.G, c.B); 
     bm.SetPixel(x, y, c); 
    } 
} 

這將循環遍歷所述位圖中的每個像素和所有的略微灰白色顏色阿爾法設置爲0,這將使該像素是透明的。您可以改變像素的R,G和B值必須加起來並使其更高以使像素必須更白,或使該值更低,這將導致更多的灰白像素變得透明。不知道這個代碼有多高效,但希望它會有幫助。您也可以使用bitmap.GetBrightness和替代if ((c.B + c.R + c.G > 660)) c = Color.FromArgb(0, c.R, c.G, c.B);你可以嘗試像if (c.GetBrightness() > 240) c = Color.FromArgb(0, c.R, c.G, c.B);

HTH