我有一个1×1的源位图,我试图拍摄该图像并将其绘制到一个新的位图.源位图全部为红色,但由于某种原因,新位图以渐变结束(参见图像).使用下面的代码,新的位图不应该是完全红色的吗?从哪里获得白色/ 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;
}
解决方法:
这不是用颜色填充区域的好方法.更好的方法是确定源图像中像素的颜色,并使用该颜色填充目标.
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;