0

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

tomatocake
  • 61
  • 1
  • 3
  • This is too broad for Stack Overflow. For starters, please ask one question per question. Your first two are alright (any code samples you currently have would be nice). The third is *definitely* too broad. – BradleyDotNET Nov 10 '14 at 16:53

2 Answers2

2

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.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
0

Many answers that will detail these points:

  1. Once you have a Region it will limit where pixels are drawn. Use Graphics.DrawImage then
  2. No way to read and only a perverted way to set a Pixel by Graphics.FillRectangle(br, x,y,1,1); The reason behind this is probably that Graphics can not only operate in a Pixel mode but also with various other Units from points to inches and mm..
    • Use Lockbits. Just one example using one Bitmap. Other common jobs demand locking two (one input one output) or or even three (two inputs and one calculated output) bitmaps..
    • Know what you invalidate, often only a small part really needs it..
    • Learn about ImageList, it can't do much but is useful for what it does, that is cache images of one size and color depth
    • Learn when to use a Panel and when a Picturebox
Community
  • 1
  • 1
TaW
  • 48,779
  • 8
  • 56
  • 89