AVIDeveloper
AVIDeveloper

Reputation: 3496

Graphics.DrawImage produces alpha-channel gradient in C# WinForms 2.0

I'm facing a really perplexing problem..

I have a .Net 2.0 C# WinForms project. I'm trying to stretch a bitmap onto a drawing area, but for some reason it is not stretched properly - I get alpha channel gradient on the right and bottom margins of my drawing area.

It took me quite a while to isolate this problem. I create a few lines of code that reproduce the problem (see code snippet and screenshot below).

Can anyone please shed some light over this matter?

Thanks in advance.

--

private void Form1_Paint( object sender, PaintEventArgs e )
{
    // Create a black bitmap resource sized 10x10
    Image resourceImg = new Bitmap( 10, 10 );
    Graphics g = Graphics.FromImage( resourceImg );
    g.FillRectangle( Brushes.Black, 0, 0, resourceImg.Width, resourceImg.Height );

    Rectangle drawingArea = new Rectangle( 0, 0, 200, 200 ); // Set the size of the drawing area
    e.Graphics.FillRectangle( Brushes.Aqua, drawingArea );   // Fill an aqua colored rectangle
    e.Graphics.DrawImage( resourceImg, drawingArea );        // Stretch the resource image

    // Expected result: The resource image should completely cover the aqua rectangle.
    // Actual Result:   The right and bottom edges become gradiently transparent (revealing the aqua rectangle under it)
}

DrawImage Problem

Upvotes: 2

Views: 2356

Answers (1)

rsbarro
rsbarro

Reputation: 27339

The behavior has to do with how GDI+ handles edges. In this case, you're scaling a very small image over a large area, and you haven't told GDI+ how to handle the edge. If you use the ImageAttributes class and set the WrapMode appropriately, you can get around this issue.

For example:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    using (var resourceImg = new Bitmap(10, 10))
    {
        using (var g = Graphics.FromImage(resourceImg))
        {
            g.FillRectangle(Brushes.Black, 0, 0, 
                resourceImg.Width, resourceImg.Height);
        }

        var drawingArea = new Rectangle(0, 0, 200, 200);
        e.Graphics.FillRectangle(Brushes.Aqua, drawingArea);

        using (var attribs = new ImageAttributes())
        {
            attribs.SetWrapMode(WrapMode.TileFlipXY);
            e.Graphics.DrawImage(resourceImg, drawingArea, 
                    0, 0, resourceImg.Width, resourceImg.Height, 
                    GraphicsUnit.Pixel, attribs);
        }
    }    
}

The above code should produce an all black image. If you comment out the attribs.SetWrapMode(WrapMode.TileFlipXY); statement, you should see the blue gradient. With the wrap mode set, you're telling GDI+ to flip the image at the edges, so it will pick up more black and not fade things out at the edge when it scales the image.

Upvotes: 5

Related Questions