On Firemonkey, how do I draw a masked bitmap on a canvas?


username

I have a bitmap and a mask (also a bitmap). I want to draw a bitmap on the mask (as shown in the image below)

draw bitmap on mask

How can I do this on Delphi using Firemonkey ?

Tom Bromberg

useTBitmap.CreateFromBitmapAndMask()

constructor CreateFromBitmapAndMask(const Bitmap, Mask: TBitmap);

The document says:

The value of the alpha channel of each color pixel of the created TBitmap is equal to the value of the red channel in the "mask".

and further:

Tip: For better results, use a grayscale image for the Mask. It has equal amounts of green, red and blue.

Tip: The mask and base bitmap must have the same dimensions. Otherwise, the size of the new TBitmap will be equal to 0.

In a simple test like:

procedure TForm19.Button1Click(Sender: TObject);
var
  bmp, msk: TBitmap;
begin
  bmp := nil;
  msk := nil;
  try
    bmp := TBitmap.Create;
    msk := TBitmap.Create;
    bmp.LoadFromFile('C:\tmp\Imgs\4.bmp');
    msk.LoadFromFile('C:\tmp\Imgs\TestImage04.bmp');
    Image1.Bitmap := bmp;
    Image2.Bitmap := msk;
    Image3.Bitmap.CreateFromBitmapAndMask(bmp, msk);
  finally
    bmp.Free;
    msk.Free;
  end;
end;

The result looks like this:

enter image description here

edit

In order to draw the result CreateFromBitmapAndMask(bmp, msk);transparently on the form, it must premultipliedfirst be assigned to Image3. We need the following steps,

procedure PremultiplyBitmapAlpha(bmp:TBitmap);
var
  X, Y: Integer;
  M: TBitmapData;
  C: PAlphaColorRec;
begin
  if bmp.Map(TMapAccess.ReadWrite, M) then
  try
    for Y := 0 to bmp.Height - 1 do
      for X := 0 to bmp.Width - 1 do
      begin
        C := @PAlphaColorArray(M.Data)[Y * (M.Pitch div 4) + X];
        C^.Color := PremultiplyAlpha(C^.Color);
      end;
  finally
    bmp.Unmap(M);
  end;
end;

and another temporary bitmap resfor this purpose . Now, the test code looks like this:

procedure TForm14.Button1Click(Sender: TObject);
var
  bmp, msk, res: TBitmap;
begin
  bmp := nil;
  msk := nil;
  res := nil;
  try
    bmp := TBitmap.Create;
    msk := TBitmap.Create;
    bmp.LoadFromFile('C:\tmp\Imgs\4.bmp');
    msk.LoadFromFile('C:\tmp\Imgs\TestImage04.bmp');

    Image1.Bitmap := bmp;
    Image2.Bitmap := msk;

    res := TBitmap.Create;
    res.CreateFromBitmapAndMask(bmp, msk);

    PremultiplyBitmapAlpha(res);
    Image3.Bitmap := res;
  finally
    bmp.Free;
    msk.Free;
    res.Free;
  end;
end;

and the picture (with modified bg colors for better presentation):

enter image description here

Related


On Firemonkey, how do I draw a masked bitmap on a canvas?

username I have a bitmap and a mask (also a bitmap). I want to draw a bitmap on the mask (as shown in the image below) How can I do this on Delphi using Firemonkey ? Tom Bromberg useTBitmap.CreateFromBitmapAndMask() constructor CreateFromBitmapAndMask(const Bi

On Firemonkey, how do I draw a masked bitmap on a canvas?

username I have a bitmap and a mask (also a bitmap). I want to draw a bitmap on the mask (as shown in the image below) How can I do this on Delphi using Firemonkey ? Tom Bromberg useTBitmap.CreateFromBitmapAndMask() constructor CreateFromBitmapAndMask(const Bi

On Firemonkey, how do I draw a masked bitmap on a canvas?

username I have a bitmap and a mask (also a bitmap). I want to draw a bitmap on the mask (as shown in the image below) How can I do this on Delphi using Firemonkey ? Tom Bromberg useTBitmap.CreateFromBitmapAndMask() constructor CreateFromBitmapAndMask(const Bi

How to draw on canvas and convert to bitmap?

Matt Matt I want to draw some lines and shapes on canvas and then convert it to bitmap on ImageView. I am extending "View" in a custom class, and on the "OnDraw method" I am drawing lines. Here is my code (this class only draws simple lines): public class fina

How to draw on canvas and convert to bitmap?

Matt Matt I want to draw some lines and shapes on canvas and then convert it to bitmap on ImageView. I am extending "View" in a custom class, and on the "OnDraw method" I am drawing lines. Here is my code (this class only draws simple lines): public class fina

How to draw 3 bitmap on canvas

Christopher M. I'm trying to combine 3 imageViews into one bitmap, and I'm using canvas, here is the function private Bitmap createSingleImageFromMultipleImages() { Bitmap formBitmap = getBitmapFromImageView(formView); Bitmap idFrontBitmap = getBitmap

How to draw on canvas and convert to bitmap?

Matt Matt I want to draw some lines and shapes on canvas and then convert it to bitmap on ImageView. I am extending "View" in a custom class, and on the "OnDraw method" I am drawing lines. Here is my code (this class only draws simple lines): public class fina

How to draw part of bitmap through canvas DrawBitmap

XTL I have one : FrameLayout (marked in red) Source ImageView (black) Object (image view) with OnTouchListener (orange) I want to display part of bitmap which is filled on imageview ( source imageview ) through Object with OnTouchListener . So that's not the p

How to draw bitmap on top right of canvas

User 1204501 I am trying top right hand cornertoCanvas So far I have done the following: //100x40 dimensions for the bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.backbutton); Rect source = new Rect(0, 0, bitm

How to draw bitmap on top right of canvas

User 1204501 I am trying top right hand cornertoCanvas So far I have done the following: //100x40 dimensions for the bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.backbutton); Rect source = new Rect(0, 0, bitm

How to draw bitmap file on HTML canvas?

username How to draw .bmp file using canvas tag in html. I found a way to draw png and jpeg instead of bmp files. Can someone suggest me these methods and are bmp files supported by every browser and platform? please help. hyphn knight Drawing .bmp files are n

How to draw bitmap file on HTML canvas?

username How to draw .bmp file using canvas tag in HTML. I found a way to draw png and jpeg instead of bmp files. Can someone suggest me these methods and are bmp files supported by every browser and platform? please help. hyphn knight Drawing .bmp files are n

How to draw bitmap on top right of canvas

User 1204501 I am trying top right hand cornertoCanvas So far I have done the following: //100x40 dimensions for the bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.backbutton); Rect source = new Rect(0, 0, bitm

How to draw bitmap file on HTML canvas?

username How to draw .bmp file using canvas tag in HTML. I found a way to draw png and jpeg instead of bmp files. Can someone suggest me these methods and are bmp files supported by every browser and platform? please help. hyphn knight Drawing .bmp files are n

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic

Draw bitmap as line on canvas

Vasily Kuragov Right now, I'm writing my small drawing application, and one of its features is custom brushes from bitmaps. I thought it wouldn't be a big problem that onTouchEvent() calls every action and draws the bitmap on every pixel, but when I swipe quic