颜色到位图

本文关键字:位图 颜色 | 更新日期: 2023-09-27 18:02:49

如何设置具有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设置为通过程序运行的像素。

颜色到位图

找到答案了。我需要把像素调回去

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