C#图像绘制颜色不正确

时间:2009-12-07 15:20:14

标签: c# winforms gdi+ drawing

我有一个1x1的源位图,我试图拍摄该图像并将其绘制到新的位图。源位图全部为红色,但由于某种原因,新位图以渐变结束(参见图像)。使用下面的代码,新的位图不应该是完全红色的吗?从哪里获得白色/ alpha?

alt text http://www.binaryfortress.com/Temp/Error.jpg

private void DrawImage()
{
    Bitmap bmpSOURCE = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
    using (Graphics g = Graphics.FromImage(bmpSOURCE))
    {
        g.Clear(Color.Red);
    }

    Bitmap bmpTest = new Bitmap(300, 100, PixelFormat.Format32bppArgb);
    using (Graphics g = Graphics.FromImage(bmpTest))
    {
        g.CompositingMode = CompositingMode.SourceCopy;
        g.CompositingQuality = CompositingQuality.AssumeLinear;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PageUnit = GraphicsUnit.Pixel;
        g.PixelOffsetMode = PixelOffsetMode.None;
        g.SmoothingMode = SmoothingMode.None;

        Rectangle rectDest = new Rectangle(0, 0, bmpTest.Width, bmpTest.Height);
        Rectangle rectSource = new Rectangle(0, 0, 1, 1);
        g.DrawImage(bmpSOURCE, rectDest, rectSource, GraphicsUnit.Pixel);
    }

    pictureBox1.Image = bmpTest;
}

2 个答案:

答案 0 :(得分:7)

这不是用颜色填充区域的好方法。更好的方法是确定源图像中像素的颜色,并使用该颜色填充目标。

Bitmap source = // get the source

Color color = source.GetPixel(1, 1);

Bitmap target = // get the target    

target.Clear(color);

尽管如此,问题可能是InterpolationMode,因为这是缩放图像时使用的。尝试使用Low而不是HighQualityBicubic

g.InterpolationMode = InterpolationMode.Low;

答案 1 :(得分:0)

我找到了解决问题的方法:

g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;