Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:22:01+00:00 2026-06-12T07:22:01+00:00

I am trying to blend two canvases together using the windows alphablend API call.

  • 0

I am trying to blend two canvases together using the windows alphablend API call. First I draw some thing on the main canvas (destination), then instantiate another canvas using TBitmap, draw onto that, and then blend the two together (following an answer here on SO).

However, I am finding that it always returns false, at first I thought it had something to do with passing the wrong handles for source and destination, but I cannot figure it out. what could it be?

unit MainWnd;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ControlsEx;

type
{------------------------------------------------------------------------------}
  TfrmMain = class(TForm)
    PaintBox1: TPaintBox;
    procedure PaintBox1Paint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

{..............................................................................}
procedure alphaBlendf(
        const in_target       : TCanvas;
        const in_transperancy : integer;
        const in_color        : TColor;
        const in_rect         : TRect;
        const in_width        : integer;
        const in_height       : integer);
var
  w          : integer;
  h          : integer;
  bitmap     : TBitmap;
  blendFn    : BLENDFUNCTION;
  ret        : boolean;
begin
  blendFn.BlendOp             := AC_SRC_OVER;
  blendFn.SourceConstantAlpha := 80;

 try
   w := in_rect.Right - in_rect.Left - 1;
   h := in_rect.Bottom - in_rect.Top - 1;

   bitmap                    := TBitmap.Create;
   bitmap.PixelFormat        := pf32bit;
   bitmap.Width              := w;
   bitmap.Height             := h;
   bitmap.Canvas.Brush.Color := in_color;

   bitmap.Canvas.Rectangle(in_rect);

   ret := Windows.AlphaBlend(
        in_target.Handle,
        0,
        0,
        in_width,
        in_height,
        bitmap.Canvas.Handle,
        0,
        0,
        in_width,
        in_height,
        blendFn);

   if ret then in_target.TextOut(0, 0, 'ok')
          else in_target.TextOut(0, 0, 'fail');
  finally
   bitmap.Free;
  end;
end;

{..............................................................................}
procedure TfrmMain.PaintBox1Paint(Sender: TObject);
var
  r: TRect;
begin
  PaintBox1.Canvas.Brush.Color := $FCFFB5;
  PaintBox1.Canvas.FillRect(r);

  r := Rect(0, 0, 100, 100);
  alphaBlendf(PaintBox1.Canvas, 0, clLime, r, PaintBox1.ClientWidth, PaintBox1.ClientHeight);
end;

end.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:22:02+00:00Added an answer on June 12, 2026 at 7:22 am

    You have various errors in your code.

    You’re not filling some members of your BLENDFUNCTION. They are not optional, supply their values.

    Your bitmap object creation should be before the try statement (this is not related with why AlphaBlend fails).

    You are requesting the AlphaBlend function to blend from the source more than it has, i.e. your bitmap is 99×99 but you want the api to blend 105×105 from it.

    Also note that in the paint handler of the paintbox you’re filling an arbitrary rectangle (your r is not initialized).

    procedure alphaBlendf(
            const in_target       : TCanvas;
            const in_transperancy : integer;
            const in_color        : TColor;
            const in_rect         : TRect;
            const in_width        : integer;
            const in_height       : integer);
    var
      w          : integer;
      h          : integer;
      bitmap     : TBitmap;
      blendFn    : BLENDFUNCTION;
      ret        : boolean;
    begin
      blendFn.BlendOp             := AC_SRC_OVER;
      blendFn.BlendFlags          := 0;
      blendFn.SourceConstantAlpha := 80;
      blendFn.AlphaFormat         := 0;
    
      bitmap                    := TBitmap.Create;
      try
       w := in_rect.Right - in_rect.Left - 1;
       h := in_rect.Bottom - in_rect.Top - 1;
    
       bitmap.PixelFormat        := pf32bit;
       bitmap.Width              := w;
       bitmap.Height             := h;
       bitmap.Canvas.Brush.Color := in_color;
    
       bitmap.Canvas.Rectangle(in_rect);
    
       ret := Windows.AlphaBlend(
            in_target.Handle,
            0,
            0,
            in_width,
            in_height,
            bitmap.Canvas.Handle,
            0,
            0,
            bitmap.width,
            bitmap.height,
            blendFn);
    
       if ret then in_target.TextOut(0, 0, 'ok')
              else in_target.TextOut(0, 0, 'fail');
      finally
       bitmap.Free;
      end;
    end;
    
    {..............................................................................}
    procedure TfrmMain.PaintBox1Paint(Sender: TObject);
    var
      r: TRect;
    begin
      PaintBox1.Canvas.Brush.Color := $FCFFB5;
      r := Rect(0, 0, 100, 100);
      PaintBox1.Canvas.FillRect(r);
    
      alphaBlendf(PaintBox1.Canvas, 0, clLime, r,
          PaintBox1.ClientWidth, PaintBox1.ClientHeight);
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to blend two images together with Android, using a Multiply-like blending mode.
What I'm trying to do is blend two canvases onto a single canvas for
I'm trying to add two images together using NumPy and PIL. The way I
I am trying to achieve this using Expression Blend 3. I need that every
I'm using OpenGL to draw in 2D. I'm trying to overlay textures with alpha.
I am trying to use Blend 3.0 to edit a project that contains some
I'm trying to set up an Accordion that has three items. The first two
Using CoreGraphics (inside my drawRect method), I'm trying to apply a blend mode to
I've defined the following two states in Expression Blend. I've been trying to follow
I am trying to figure out how to grab a value from Expression.Blend.SampleData. If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.