ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Draw Polygon to TIEBitmap w/ Alpha = 0?
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

Fellafoo

USA
52 Posts

Posted - Apr 04 2022 :  16:16:27  Show Profile  Reply
Is is possible to draw a polygon to a transparent bitmap?

For example:

ieBmp := TIEBitmap.Create(Width, Height, clBlack, 0);
ieCnv := TIECanvas.Create(ieBmp.Canvas);
ieCnv.Brush.Create(ieBrush);
ieCnv.Brush.Color := clYellow;
ieCnv.Brush.Transparency := 128;

To get a result, I must set the alpha to 255. I can set the alpha to 128 and the brush transparency to 255, but then the entire canvas has an alpha of 128. I want the non-filled areas to have an alpha of zero.

xequte

38610 Posts

Posted - Apr 04 2022 :  16:51:50  Show Profile  Reply
Hi

If the bitmap is transparent you are best to draw to both the bitmap and the alpha channel at the levels you want.

Another option is to create a ie32RGB bitmap, draw to the IECanvas and then synchronize the alpha channel (i.e. convert the A of RGBA to ImageEn's alpha channel).

https://www.imageen.com/help/TIEBitmap.SynchronizeRGBA.html

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

Fellafoo

USA
52 Posts

Posted - Apr 05 2022 :  16:37:58  Show Profile  Reply
Thanks Nigel,

Here's what I came up with...


procedure PolyPolygonForIE_Voids(aDC: HDC; aPtArr: PPoint; aPtCount: PInteger; aPlyCount: Integer; BrClr: TColor = 0; BrOp: Byte = 255; BmpBrush: Str255 = '');
{ Draw transparent polygons WITH voids using Windows PolyPolygon procedure & TIEBitmap AlphaChannel }
var
  TmpCnv: TCanvas;
  aBmp: TBitmap;
  ieBmp: TIEBitmap;

  DrawFormWidth, DrawFormHeight: Integer;

begin
  if (aPlyCount = 0) then Exit { Nothing to do }
  else if (aPtCount^ < 3) then Exit; { Not a valid polygon }

  { Create temporary canvas from Device Context }
  TmpCnv := TCanvas.Create;
  TmpCnv.Handle := aDC;
  DrawFormWidth := TmpCnv.ClipRect.Right;
  DrawFormHeight := TmpCnv.ClipRect.Top;

  { Create Bitmap for Alpha Channel (Mask) }
  aBmp := TBitmap.Create;
  aBmp.Canvas.Brush.Style := bsSolid;
  aBmp.Canvas.Brush.Color := clBlack; { Set color here before width / height }
  aBmp.Width := DrawFormWidth;
  aBmp.Height := DrawFormHeight;

  { Set brush color to grey value from user-defined opacity value }
  aBmp.Canvas.Brush.Color := RGB(BrOp, BrOp, BrOp);
  { Draw polygon with voids using Windows.PolyPolygon (Alpha Image) }
  PolyPolygon(aBmp.Canvas.Handle, aPtArr^, aPtCount^, aPlyCount);

  { Create 'ie' bitmap, same size as Device Context }
  ieBmp := TIEBitmap.Create(DrawFormWidth, DrawFormHeight, ie32RGB); { This method seems fastest }
  { Set compositing mode }
  ieBmp.IECanvas.SetCompositingMode(ieCompositingModeSourceOver, ieCompositingQualityDefault);

  { Assign Mask to ieBmp's Alpha Channel }
  ieBmp.AlphaChannel.Assign(aBmp);
  ieBmp.SyncAlphaChannel(True);

  { Set brush color to user-defined value }
  ieBmp.Canvas.Brush.Color := BrClr;
  { Draw polygon with voids using Windows.PolyPolygon (Source Image) }
  PolyPolygon(ieBmp.Canvas.Handle, aPtArr^, aPtCount^, aPlyCount);

  { Merge result onto our DrawForm canvas }
  ieBmp.DrawToCanvasWithAlpha(TmpCnv, 0, 0);

  aBmp.Free;
  ieBmp.Free;
  TmpCnv.Free;
end; { PolyPolygonForIE_Voids }


If I want to make ieBmp global and 'recycle' it, how do I go about reassigning the Alpha Channel? I've tried a few things, but I can't get the canvas / alpha channel to reset properly.
Go to Top of Page

xequte

38610 Posts

Posted - Apr 05 2022 :  17:17:59  Show Profile  Reply
Hi

What problems are you having with it?

The AlphaChannel is itself just another TIEBitmap, so you can use all the same methods to reset it (allocating its size, filling, etc).

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

Fellafoo

USA
52 Posts

Posted - Apr 06 2022 :  10:27:41  Show Profile  Reply
I think my 'extra' canvas was the problem and with your new logic where I'm now drawing directly to the AlphaChannel canvas and calling AlphaChannel.Fill(0) at the beginning of each pass is working now. So thank you for that. ;-)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: