【问题标题】:Color to Bitmap颜色到位图
【发布时间】:2014-05-10 01:50:19
【问题描述】:

如何设置具有Color 像素的位图。我用LockBits 创建了一个程序,它非常快,但现在我需要用我通过LockBits 运行的那个图像设置一个PictureBox 我不想使用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 设置为通过程序运行的像素。

【问题讨论】:

    标签: c# image graphics bitmap pixels


    【解决方案1】:

    找到了答案。我需要重新设置像素。

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

    【讨论】:

      猜你喜欢
      • 2016-03-31
      • 1970-01-01
      • 2011-09-19
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2018-12-28
      相关资源
      最近更新 更多