2017-10-12 151 views
0

我需要黑色圖像轉換爲灰度image.my代碼轉換彩色圖像爲灰度image.but不會轉換黑色圖像 爲灰度image.Even嘗試從計算器一個樣品 灰度轉換。這也不適用於黑色到灰度的 圖像轉換。該鏈接是https://stackoverflow.com/a/2265990/8569792黑色圖像轉換爲灰度圖像

public void GreyScaleConversion() 
{   
    //read image 
    Bitmap bmp = global::Ribbon.Properties.Resources.Device; 

    //load original image in picturebox1 
    pictureBox1.Image = global::Ribbon.Properties.Resources.Device; 

    //get image dimension 
    int width = bmp.Width; 
    int height = bmp.Height; 

    //color of pixel 
    Color p; 

    //grayscale 
    for (int y = 0; y < height; y++) 
    { 
     for (int x = 0; x < width; x++) 
     { 
      //get pixel value 
      p = bmp.GetPixel(x, y); 

      //extract pixel component ARGB 
      int a = p.A; 
      int r = p.R; 
      int g = p.G; 
      int b = p.B; 

      //find average 
      int avg = (r + g + b)/3; 

      //set new pixel value 
      bmp.SetPixel(x, y, Color.FromArgb(a, avg, avg, avg)); 
     } 
    } 

    //load grayscale image in picturebox2 
    pictureBox2.Image = bmp; 
} 

回答

0
public void GreyScaleConversion() 
    { 
     //read image 
     Bitmap bmp = global::Ribbon.Properties.Resources.Device; 

     //load original image in picturebox1 
     pictureBox1.Image = global::Ribbon.Properties.Resources.Device; 

     //get image dimension 
     int width = bmp.Width; 
     int height = bmp.Height; 

     //color of pixel 
     Color p; 

     //grayscale 
     for (int y = 0; y < height; y++) 
     { 
      for (int x = 0; x < width; x++) 
      { 
       //get pixel value 
       p = bmp.GetPixel(x, y); 

       //extract pixel component A 
       int a = p.A; 

       //set new pixel value 
       bmp.SetPixel(x, y, Color.FromArgb(a, 155, 155, 155)); 
      } 
     } 

     //load grayscale image in picturebox2 
     pictureBox2.Image = bmp; 
    }