开发者

DrawImage() function over WinForms does not work correctly

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;
      开发者_StackOverflow  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.


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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜