I need to draw parts of a bitmap (not rectangular) using Direct2D like images below. It is like how in Photoshop user can create a new layer based on the selected region (HRGN
) and move it around. The closest example that I have been able to find was here, but I have not been able to make it work. Does anyone know how to do this?
Thank you
Edit
It looks like people have misunderstood what I was trying to achieve. This new animated GIF should explain it better.
2
Answers
After what felt like a million try and fail, I found the answer. Microsoft's instruction for how to create a Bitmap Opacity Mask in here and here are very confusing and makes it sound like you need a black and white image as a mask, but that is far from the truth. What we really need is an image that only has alpha value information. Also I was not able to find any instruction for converting a
HRGN
to anID2D1Bitmap
. So I had to come up with my own which I am going to share here for anyone who might run into the same problem.I have omitted most of the error checking for simplicity.
When the function returns, the last parameter should have a valid bitmap mask (if everything had worked correctly) to be used by
FillOpacityMask
function ofID2D1RenderTarget
.Now we need to turn our
ID2D1Bitmap
into aID2D1BitmapBrush
.pOrigBmp
is the image that we want to move around andbkBrush
is a pointer toID2D1BitmapBrush
.If you have a
HBITMAP
or in case of C++ Builder guys aTBitmap
, you can find the instruction for converting a bitmap toID2D1Bitmap
here.Now everytime you want to paint your window, you can simply do this:
For moving the image around we have to use
SetTransform
.pBmpMask
is aID2D1Bitmap
that was created usingHRGN_to_D2Bitmap
. ie:Hope this is helpful.
Sam
Ok, I got it. Do you want me to email you the project source? It’s C++ Builder 6, so your version should be able to convert it. I was right about the FullRepaint needing to be set to false for the TPanel.
The basic idea is to put a TImage on a TPanel, then “cut out” the hRGN of the panel based on the transparent color of the image you want to use. Also, be sure to set the “FullRepaint” property of the TPanel to false, or else you’ll get that flickering you noted. This is how I did the moving animation for a couple games I made.