2013-12-20 37 views
0

我正在創建一個非常基本的圖像編輯器,並嘗試以數值方式設置顏色(R,G,B)的值。例如,這裏是我的代碼的一個片段:更改論壇應用程序中像素的顏色C#

for (int row = 0; row < thePicture.Width; row = row + 1) 
{ 
    for (int col = 0; col < thePicture.Height; col = col + 1) 
    { 
     Color pixel = thePicture.GetPixel(row, col); 

     pixel = Color.FromArgb(5 + pixel.R, 5 + pixel.G, 5 + pixel.B); 
     //+5 is making the image darker... I think 
     if (pixel.R > 255)//This is used to prevent the program from crashing 
     { 
      pixel.R = //is this possible? or another way? I am intending 
     }    //Make this 255      
     thePicture.SetPixel(row, col, pixel); 
    } 
} 

請注意,它是在Windows論壇。 請高人指點,對C#很基本瞭解。謝謝

回答

1

從MSDN文章System.Drawing.Color.R屬性。

屬性R是隻讀的(只有getter被定義)。 因此,必須創建一種新顏色。

嘗試pixel = Color.FromArgb(pixel.A, Math.Min(255, pixel.R + 5), pixel.G, pixel.B);

釋:

我們正在創建使用以前的顏色的α(阿爾法),R(紅),G(綠),B(藍)的屬性值了新的色彩。

但是,在R的情況下,我們通過調整後的R值,而不是通過前面的R值。您可以使用Math.Min(x,y)以確保「明亮」R值不超過最大255值

+0

非常有幫助!謝謝 – user2970816