C# image drawing colours are incorrect
I have a source bitmap that is 1x1 and I am trying to take that image and draw it to a new bitmap. The source bitmap is all red, but for some reason the new bitmap ends up with a gradient (see image). Using the code below, shouldn't the new bitmap be completely red? Where is it getting the white/alpha from?
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, b开发者_JAVA技巧mpTest.Width, bmpTest.Height);
Rectangle rectSource = new Rectangle(0, 0, 1, 1);
g.DrawImage(bmpSOURCE, rectDest, rectSource, GraphicsUnit.Pixel);
}
pictureBox1.Image = bmpTest;
}
This is not a good way to fill an area with a color. A better approach would be to determine the color of the pixel in the source image and use that color to fill the target.
Bitmap source = // get the source
Color color = source.GetPixel(1, 1);
Bitmap target = // get the target
target.Clear(color);
Nonetheless, the problem is likely the InterpolationMode
, as this is what's used when scaling images. Try using Low
istead of HighQualityBicubic
.
g.InterpolationMode = InterpolationMode.Low;
I found this solution for your problem:
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
精彩评论