2014-05-10 3 views
0

픽셀의 Color으로 비트 맵을 설정하는 방법은 무엇입니까? 나는 LockBits와 함께 프로그램을 만들어 그것은 매우 빠르고하지만 지금은 내가 SetPixels 내 현재 코드를 사용하지 않으려는 내가 LockBits를 통해 실행하는 이미지로 PictureBox을 설정하는 것입니다 필요비트 맵의 ​​색상

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; 
관련 문제