2014-05-10 87 views
0

我該如何着手設置像素的Color的位圖。我創建了一個程序LockBits,這是非常快的,但現在我需要設置一個PictureBox與圖像我通過LockBits跑了,我不希望使用的setPixels我當前的代碼是:顏色到位圖

Bitmap imageFile = new Bitmap(bmpPath); 

BitmapData imageData = imageFile.LockBits(new Rectangle(0, 0, imageFile.Width, imageFile.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); 

IntPtr Pointer = imageData.Scan0; 

int ArraySize = Math.Abs(imageData.Stride) * imageFile.Height; 

byte[] PixelArray = new byte[ArraySize]; 

Marshal.Copy(Pointer, PixelArray, 0, ArraySize); 

int PixelAmount = 4; //ArGb 

Color ArGBformat; 

Bitmap RenderedImage = new Bitmap(imageFile.Width, imageFile.Height); 

byte NewAlpha; 
byte NewRed; 
byte NewGreen; 
byte NewBlue; 
unsafe 
{ 
    for (int y = 0; y < imageData.Height; y++) 
    { 
     byte* row = (byte*)imageData.Scan0 + (y * imageData.Stride); 

     for (int x = 0; x < imageData.Width; x++) 
     { 
      int offSet = x * PixelAmount; 
      // read pixels 
      byte blue = row[offSet]; 

      byte green = row[offSet + 1]; 

      byte red = row[offSet + 2]; 

      byte alpha = row[offSet + 3]; 

      //Manipulates pixels 
      NewAlpha = Convert.ToByte(Math.Abs(alpha - _Alpha)); 
      NewRed = Convert.ToByte(Math.Abs(red - _Red)); 
      NewBlue = Convert.ToByte(Math.Abs(blue - _Blue)); 
      NewGreen = Convert.ToByte(Math.Abs(green - _Green)); 


      ArGBformat = Color.FromArgb(NewAlpha, NewRed, NewGreen, NewBlue); 

      RenderedImage.SetPixel(x, y, ArGBformat); //Slow and want something else 
     } 
    } 
} 

我想將我的PictureBox1設置爲通過程序運行的像素。

回答

2

找到了答案。我需要將像素設置回來。

//Sets image 
row[offSet] = NewBlue; 
row[offSet + 1] = NewGreen; 
row[offSet + 2] = NewRed; 
row[offSet + 3] = NewAlpha;