views:

294

answers:

1

What is the fastest way to draw an on an image in .net?

I'm trying to draw an image on top of another image that have in a windows form control. I know that when I try to draw it directly using loops it takes an Eon.

Options I have heard tossed around are GDI+, Direct Draw, or DX10. I start with a bitmap of a fixed size and after applying 3+ overlays, layers, to it before it is assigned to a form object.

Thanks,

+2  A: 

If your overlays are images (perhaps PNG images with transparency), then the general technique is to create a Graphics object from the image onto which you'd like to draw, then render the other images onto it thusly:

    Bitmap b1 = (Bitmap) Bitmap.FromFile("bitmap1.bmp");
    Bitmap b2 = (Bitmap)Bitmap.FromFile("bitmap2.bmp");
    Bitmap b3 = (Bitmap)Bitmap.FromFile("bitmap3.bmp");
    using (Graphics g = Graphics.FromImage(b1))
    {
        g.DrawImage(b2, new Point(0, 0));
        g.DrawImage(b3, new Point(50, 50));
    }

If your overlays are drawn object (text, lines, shapes, etc.), then you would create or obtain the appropriate brushes and pens and use the Graphics object to render what you want onto the image. Always make sure you dispose of any disposables when you're done with them. This is most handily done with the using statement.

Michael McCloskey