2012-12-06 273 views
0

我正在傳遞一個圖像到這個方法中,並淡化它的右邊緣,它正在工作,並且突然不再有了。它會返回我傳遞給它的位圖,但不保留修改的像素,它們會返回原始位置。我哪裏錯了?爲什麼我的返回位圖不能正確返回?

private Bitmap fadedEdge(Bitmap bmp) 
{ 
    //img.Save("i.bmp"); 
    //Bitmap bmp = (Bitmap)img; 
    int howfartofade = bmp.Width/4; 
    int i = 0; 
    if (howfartofade > 255) i = howfartofade/255; 
    else i = 255/howfartofade; 
    if (i == 0) i = 1; 
    int alp = 255; 
    int counter = 0; 
    for (int x = bmp.Width - howfartofade; x < bmp.Width; x++) 
    { 
     if (howfartofade > 255) 
     { 
      counter++; 
      if (counter == i + 1) 
      { 
       alp -= 1; 
       counter = 0; 
      } 
     } 
     else 
     { 
      alp -= i; 
     } 
     for (int y = 0; y < bmp.Height; y++) 
     { 
      if (alp >= 0) 
      { 
       Color clr = bmp.GetPixel(x, y); 
       clr = Color.FromArgb(alp, clr.R, clr.G, clr.B); 
       bmp.SetPixel(x, y, clr); 
      } 
      else 
      { 
       Color clr = bmp.GetPixel(x, y); 
       clr = Color.FromArgb(0, clr.R, clr.G, clr.B); 
       bmp.SetPixel(x, y, clr); 
      } 
     } 
    } 
    return bmp; 
} 
+1

你爲什麼要返回'bmp'?它作爲參數傳入,'Bitmap'是一個引用類型(一個類)。 – Dai

+2

您的位圖對象是否有Alpha通道?檢查bmp.PixelFormat。 –

回答

0

我想通了。愚蠢的我。我改變它傳遞一個圖像並返回一個圖像,並從傳遞給它的圖像創建一個新的位圖。

private Image fadedEdge(Image input) 
    { 
     //img.Save("i.bmp"); 
     //Bitmap bmp = (Bitmap)img; 
     Bitmap output = new Bitmap(input); 
     int howfartofade = input.Width/8; 
     int i = 0; 
     if (howfartofade > 255) i = howfartofade/255; 
     else i = (255/howfartofade) + 1; 
     if (i == 0) i = 1; 
     int alp = 255; 
     int counter = 0; 
     for (int x = input.Width - howfartofade; x < input.Width; x++) 
     { 
      if (howfartofade > 255) 
      { 
       counter++; 
       if (counter == i + 1) 
       { 
        alp -= 1; 
        counter = 0; 
       } 
      } 
      else 
      { 
       alp -= (i); 
      } 
      for (int y = 0; y < input.Height; y++) 
      { 
       if (alp >= 0) 
       { 
        Color clr = output.GetPixel(x, y); 
        clr = Color.FromArgb(alp, clr.R, clr.G, clr.B); 
        output.SetPixel(x, y, clr); 
       } 
       else 
       { 
        Color clr = output.GetPixel(x, y); 
        clr = Color.FromArgb(0, clr.R, clr.G, clr.B); 
        output.SetPixel(x, y, clr); 
       } 
      } 
     } 

     return output; 
    } 
相關問題