How to color blend (colorize by specified alpha value) the canvas area using pure GDI?

后端 未结 1 1004
礼貌的吻别
礼貌的吻别 2021-02-08 11:44

I\'d like to color blend (colorize by specified alpha value) the area of a canvas using pure Windows GDI (so without GDI+, DirectX or similar, no OpenGL, no assembler or a 3rd p

1条回答
  •  囚心锁ツ
    2021-02-08 12:14

    Have you tried the Canvas Drawing with AlphaBlend?

    something like

    Canvas.Draw(Arect.Left, ARect.Top, ABitmap, AAlphaBlendValue);
    

    combined with a FillRect for the blend color

    Update: And here's some code, as close as possible to your interface, but pure VCL.
    Might not be as efficient, but much simpler (and somewhat portable).
    As Remy said, to paint on a Form in a pseudo persistent way, you'd have to use OnPaint...

    procedure ColorBlend(const ACanvas: TCanvas; const ARect: TRect;
      const ABlendColor: TColor; const ABlendValue: Integer);
    var
      bmp: TBitmap;
    begin
      bmp := TBitmap.Create;
      try
        bmp.Canvas.Brush.Color := ABlendColor;
        bmp.Width := ARect.Right - ARect.Left;
        bmp.Height := ARect.Bottom - ARect.Top;
        bmp.Canvas.FillRect(Rect(0,0,bmp.Width, bmp.Height));
        ACanvas.Draw(ARect.Left, ARect.Top, bmp, ABlendValue);
      finally
        bmp.Free;
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Image: TPNGImage;
    begin
      Image := TPNGImage.Create;
      try
        Image.LoadFromFile('d:\6G3Eg.png');
        ColorBlend(Image.Canvas, Image.Canvas.ClipRect, $0000FF80, 175);
        Canvas.Draw(0, 0, Image);
        // then for fun do it to the Form itself
        ColorBlend(Canvas, ClientRect, clYellow, 15);
      finally
        Image.Free;
      end;
    end;
    

    0 讨论(0)
提交回复
热议问题