在的WinForms的DrawImage()函数无法正常工作 [英] DrawImage() function over WinForms does not work correctly
问题描述
我已经创建了一个位图1个像素宽和放大器; 256像素的高度,当我尝试画这个图作为2个像素宽的使用:
公共无效的DrawImage(图片形象,的RectangleF矩形)
位图是不正确绘制,因为有每个位图列的白色条纹渺茫。
见下文
私人无效Form1_Paint(对象发件人,PaintEventArgs的E)
{$ B的简单代码$ b图形克= e.Graphics;
位图位图=新位图(1,256);
为(INT Y = 0; Y< 256; Y ++)
{
bitmap.SetPixel(0,Y,Color.Red);
}
的RectangleF rectf =新的RectangleF();
的for(int x = 0; X< 500; X ++)
{
浮动系数= 2;
rectf.X = X *因素;
rectf.Y = 0;
rectf.Width =事实;
rectf.Height = 500;
//应该吸取位图作为2个像素宽,但在每一个位图colomn
gr.DrawImage(位图,rectf)之间有白色条纹纤细绘制它;
}
}
这是Graphics.InterpolationMode的副作用,位图缩放产生伪像,当它在中位图的边缘耗尽像素。而且有很多用位图,这只是一个像素宽耗尽像素的。你可以将其设置为最近邻,并通过设置PixelOffsetMode为None取得更好的成绩。这仍然是它的外观产生伪影,虽然,一些内部舍入误差。不知道,我在的事实的价值猜测。
避免结垢小位图。
i've created a BitMap 1 pixel wide & 256 pixel height when i try to draw this bitmap as 2 pixels wide using:
public void DrawImage(Image image,RectangleF rect)
the bitmap is not drawn correctly because there are white slim stripes between each bitmap column. see the simple code below
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gr = e.Graphics;
Bitmap bitmap = new Bitmap(1, 256);
for (int y = 0; y < 256; y++)
{
bitmap.SetPixel(0, y, Color.Red);
}
RectangleF rectf = new RectangleF();
for (int x = 0; x < 500; x++)
{
float factor = 2;
rectf.X = x*factor;
rectf.Y = 0;
rectf.Width = fact;
rectf.Height = 500;
// should draw bitmap as 2 pixels wide but draws it with white slim stripes in between each bitmap colomn
gr.DrawImage(bitmap, rectf);
}
}
This is a side effect of Graphics.InterpolationMode, bitmap scaling produces artifacts when it runs out of pixels at the edge of the bitmap. And there's a lot of running out of pixels with a bitmap that's only one pixel wide. You can get better results by setting it to NearestNeighbor and by setting PixelOffsetMode to None. This still produces artifacts though, some internal round-off error by the looks of it. Not sure, I had to guess at the value of "fact".
Avoid scaling small bitmaps.
这篇关于在的WinForms的DrawImage()函数无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!