在Windows窗体中将带有Alpha通道的一种颜色位图保存为另一种(错误的)颜色

问题描述:

在C#、. NET 2.0,Windows窗体,Visual Studio Express 2010中,我要保存由相同颜色组成的图像:

In C#, .NET 2.0, Windows Forms, Visual Studio Express 2010, I'm saving an image made of the same color:

  Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  using (Graphics graphics = Graphics.FromImage(bitmap))
  {
      Brush brush = new SolidBrush(color);
      graphics.FillRectangle(brush, 0, 0, width, height);
      brush.Dispose();
  }

  bitmap.Save("test.png");
  bitmap.Save("test.bmp");

例如,如果我正在使用

颜色[A = 153,R = 193,G = 204,B = 17]或#C1CC11

Color [A=153, R=193, G=204, B=17] or #C1CC11

保存图像并在外部查看器(例如Paint.NET,IrfanView,XNView等)中打开它后,会被告知图像的颜色实际上是:

after I'm saving the image and open it in an external viewer such as Paint.NET, IrfanView, XNView, etc. I am told that the color of the image is actually:

颜色[A = 153,R = 193,G = 203,B = 16]或#C1CB10

Color [A=153, R=193, G=203, B=16] or #C1CB10

所以它是相似的颜色,但是不一样!

so it's a similar color, but not the same!

我尝试同时保存PNG和BMP.

I tried with both PNG and BMP saving.

当涉及透明度(alpha)时,.NET保存不同的颜色! 当Alpha为255(无透明度)时,它会保存相应的颜色.

When transparency (alpha) is involved, .NET saves a different color! When the alpha is 255 (no transparency), it saves the corrent color.

谢谢乔和汉斯·帕森特的评论.

Thank you, Joe and Hans Passant for your comments.

是的,正如乔所说,问题就在这里:

Yes, as Joe said, the problem is on the line:

graphics.FillRectangle(brush, 0, 0, width, height);

此处GDI +会将颜色修改为相似的颜色,但不是精确的颜色.

Here GDI+ modifies the color with a similar color, but not the exact one.

解决方案似乎是使用Bitmap.LockBits和Marshal.Copy直接在像素中写入颜色值:

It seems that the solution is to write the color values directly in the pixels, using Bitmap.LockBits and Marshal.Copy:

        Bitmap bitmap = new Bitmap(this.currentSampleWidth, this.currentSampleHeight, PixelFormat.Format32bppArgb);

        // Lock the bitmap's bits.  
        Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
        BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap (32 bits per pixel)
        int pixelsCount = bitmap.Width * bitmap.Height;
        int[] argbValues = new int[pixelsCount];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, pixelsCount);

        // Set the color value for each pixel.
        for (int counter = 0; counter < argbValues.Length; counter++)
            argbValues[counter] = color.ToArgb();

        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, pixelsCount);

        // Unlock the bits.
        bitmap.UnlockBits(bmpData);

        return bitmap;