masks in GDI+

Hello,

I'm fairly new and would like some advice on using masks in GDI+.

I found in dev-archive an excellent article by Rehan Nadeem on flicker free drawing in C#, easely converted to C++.NET.

Much simpler (Graphics* DrawImage(BitMap*, int, int) than the MFC BitBlt stuff.

However... GDI and BitBLt used a parameter for the bitwise raster operations: SRCPAINT, SRCAND, SRCINVERT, etc. I need these for the animations I want to produce.

I could not find these operations in the helpfiles of C++.NET. Or did I not look well enough?

Thanks in advance,

Luke
[622 byte] By [luckyluke] at [2007-11-18 19:22:54]
# 1 Re: masks in GDI+
Does GDI+ give you a handle to an HDC? If yes then you can use BitBlt and use those bitwise operation. GDI+ is an extension to GDI therefore there shouldn't be anything wrong with using GDI api's with GDI+.
... or ...

Even better if you have access to the bits of the image you can change the image bits and use the bitwise operators and paste the image on another image :). Iam not an expert on GDI+.

If you plan on writing directly then consider ...
A usefull algorythm
Take the width of your image in pixels and express it in binary. Lets take a 640x480 image.

640 = 0010 1000 000

Bits 7 and 9 are 1 therefore you can express this as

(y<<9)+(y<<7) + x

where y is replace with the y coordinate of the pixel you are searching for. Where x is replace it with the x coordinate. This is faster then multiplying 640 by y.

This will work only if you have an 8bit bitmap what about 16bit or 24bit etc...

Lets use 640x480 as an example ... Lets assume that this is a 24bit bitmap whitch is 3bytes therefore we can say

b = ((y<<9)+(y<<7))*3

Lets replace y with 1. Then we get 1920 this can be expressed in binary as

0111 1000 0000

Therefore we get
(y<<7)+(y<<8)+(y<<9)+(y<<10)+x
This is faster than doing 640*y*3+x to get the byte location.
answer at 2007-11-10 3:52:00 >
# 2 Re: masks in GDI+
Thanks for your quick advice, and especially for the handy method for rapid access to the indivudual bits in a bitmap.

Maybe I have not been explicit enough about what I want to achieve.

I want to produce creatures on the screen who move continuously. As their bodies are not rectangular the best way (I have read) to display their movements is:
a) use a mask - a bitmap where every pixel corresponding to a body part is black, the other pixels are white.
b) AND this mask with the background: the result is a black hole in the place of the body part.
c) then OR the body part bitmap with the masked background: the result is our creature surrounded by background pixels.

AND and OR are the parameters SRCAND and SRCPAINT of the BitBlt function of GDI. Very well.

Double buffering in GDI involves several operations. In GDI+ it is much easier. But I do not find the AND & OR techniques in GDI+! Of course I could use GDI but I wondered if there is a GDI+ way ...
luckyluke at 2007-11-10 3:52:57 >
# 3 Re: masks in GDI+
I'v been browsing the Graphics class members at MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusreference/classes/graphicsclass/graphicsmethods.asp) , but coudn't find anything similer to what you are looking for. There is a function Graphics::GetHDC() you can use that with bitblt e.g.

HDC hdc = GraphicsObject.GetHDC();
Bitblt(hdc, x, y, width, height, maskHDC, maskx, masky, SRCAND);
//Do anything :). But do not call any GDI+ functions.
GraphicsObject.ReleaseHDC(hdc);
//Now you may call GDI+ functions.

Hope this helps!
answer at 2007-11-10 3:53:53 >
# 4 Re: masks in GDI+
thanks for your advice

I give it a try!

luke
luckyluke at 2007-11-10 3:54:58 >