I have a source bitmap that is 1×1 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, bmpTest.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.
Nonetheless, the problem is likely the
InterpolationMode, as this is what’s used when scaling images. Try usingLowistead ofHighQualityBicubic.