2016-10-27 74 views
-1

我想要做的是發生事件後,我將所有的箭頭圖片設置爲紫色。着色位圖不準確(我已經看過:如何在C#.NET中更改圖像的像素顏色)

這裏是我的箭圖片看起來像繪畫前: Arrow to the left

這裏是什麼樣子衝之後: Arrow left after collored

首先,我讓所有帶箭頭的pictureboxes的列表:

List<PictureBox> arrows = new List<PictureBox>(); 
foreach (var item in Controls.OfType<PictureBox>()) 
     { 
      if (item.Name.StartsWith("arrow")) 
      { 
       arrows.Add(item); 
      }  
     } 

這是我使用的彩色圖片代碼:

System.Drawing.Color purple = System.Drawing.Color.Purple; 

     foreach (var item in arrows) 
     { 
      Bitmap bmp = new Bitmap(item.Image, item.Height, item.Width); 
      for (int i = 0; i < item.Height; i++) 
      { 
       for (int j = 0; j < item.Width; j++) 
       { 
        var actualColor = bmp.GetPixel(i, j).ToArgb(); 
        var purpleA = bmp.GetPixel(i, j).A; 
        if (actualColor != System.Drawing.Color.White.ToArgb()) 
        { 
         bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(purpleA, purple)); 
        } else 
        { 
         bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(actualColor)); 
        } 
       } 
      } 
      item.Image = bmp; 
     } 

如何準確着色圖像?此刻,紫色真的很糟糕。我需要它與黑色箭頭完全相同,而不是黑色,我需要它是紫色的。

注意:當我將它放入圖片框時,我調整了黑色箭頭圖像的大小,因此在黑色和紫色箭頭的形狀上大小相同。我上傳了帶有屏幕截圖的紫色箭頭,黑色箭頭來自我的電腦。這就是爲什麼他們的尺寸不一樣。

+0

你是什麼意思?實際問題在哪裏? – ViRuSTriNiTy

+0

我發佈了圖片。看看紫色畫得有多糟糕。我需要箭頭完全相同,但不是黑色,我需要它是紫色的。 – Mathijs

+0

Alpha通道必須存在問題。嘗試將紅色綠色和藍色部分與255比較,然後是白色。 – ViRuSTriNiTy

回答

0

你可以使用彩色地圖被另一個更換顏色。它比循環每個像素要快得多。

Graphics g = pictureBox.GetGraphics; // get the picture box graphics (i doubt this line compile but you get the idea of the object you need)  
    using (Bitmap bmp = new Bitmap("img.bmp")) // or the image currently in the picture box 
    {   
     // create the color map 
     var colorMap = new ColorMap[1]; 
     colorMap[0] = new ColorMap(); 

     // old color 
     colorMap[0].OldColor = Color.Black; 

     // replaced by this color 
     colorMap[0].NewColor = Color.Purple; 

     // attribute to remap the table of colors 
     var att = new ImageAttributes(); 
     att.SetRemapTable(colorMap); 

     // draw result 
     Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); 
     g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, att); 
    } 
0

控件的尺寸可能與圖片不同。

代替item.Width使用item.Image.Width

EX:

var itemImage = item.Image; 
Bitmap bmp = new Bitmap(itemImage, itemImage.Height, itemImage.Width);