DrawImage() function over WinForms does not work correctly

拥有回忆 提交于 2019-12-08 00:33:18

问题


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);
    }           
}

回答1:


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.




回答2:


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 your snippet. and you insist that should draw bitmap as 2 pixels wide. sorry, but this is so wrong. I'll explain why. let's look how this loop works.

  • x=0

  • you are setting upper left x coord to zero. rectf.X = x*factor;

  • gr.DrawImage(bitmap, rectf); you are drawing 1 pixel wide bitmap on rectangle, starting at x coord equal to 0.

  • loop ends, x becomes 1.

  • upper left x coord is now 2.

  • drawing 1 pixel wide bitmap on rectangle, starting at x coord equal to 2. (as you see no bitmap @ x = 1)

do I have to continue, or is it clear why do the white stripes come, and where from?

to fix it use this snippet

for (int x = 0; x < 500; x++)
{
    float factor = 2;
    rectf.X = x * factor; // x coord loops only through even numbers, thus there are white stripes
    rectf.Y = 0;
    rectf.Width = factor;
    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);
    rectf.X = x * factor + 1; // now x coord also loops through odd numbers, and combined with even coords there will be no white stripes.
    gr.DrawImage(bitmap, rectf);    
}

P.S. What are you trying to achieve? And have you heard about Graphics.FillRectangle() method?




回答3:


bitmap.SetPixel(1,y,Color.Red) should do it and rectf.X should not extend rectf.Width.



来源:https://stackoverflow.com/questions/6744067/drawimage-function-over-winforms-does-not-work-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!