Search code examples
c#gdiregion

Some specific questions to GDI+ in C#


Im trying to create a little game with C# and GDI+. For learning purposes I'm trying to avoid as much frameworks etc. as possible. So I have some specific questions to GDI+

Is it possible to fill a region object in GDI with an image? -If not, is there a manual way for it?

Can you read and set single pixels in a graphics object (not a bitmap)?

Have you got any tips for me to increase overall performance in GDI?

Thanks for any help


Solution

  • Is it possible to fill a region object in GDI with an image?

    A region can't be filled, it doesn't store pixels. What you are almost certainly looking for here is the Graphics.Clip property. Assign the region to it and draw the image, it will be clipped by the region.

    Can you read and set single pixels in a graphics object (not a bitmap)?

    No, the Graphics object doesn't store any pixels itself, it only keeps track of where you draw to. The "device context" in Windows speak. Which can be a bitmap, the screen, a printer, a metafile. Not all of these device contexts let you read a pixel back after drawing (not a printer and not a metafile for example). But no problem of course when you draw to a bitmap.

    Have you got any tips for me to increase overall performance in GDI?

    There is one crucial one, .NET makes it very easy to overlook. The pixel format of the bitmap you draw to is super-duper important. The most expensive thing you'll ever do with a bitmap is copying it, from CPU memory to the video-adapter's memory. That copy is only fast if the format of the pixels exactly match the format the video-adapter uses. On all modern machines that's PixelFormat.Format32bppPArgb. The difference is huge, it is ten times faster than all the other ones.