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

  • SEARCH
  • Home
  • 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 7849847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:39:47+00:00 2026-06-02T18:39:47+00:00

Overview From the GR32 library, I am using TImgView32 to render a grid which

  • 0

Overview

From the GR32 library, I am using TImgView32 to render a grid which will be my transparent background like so:

enter image description here

Placed inside the TImgView32 I have a regular TImage, where I will be drawing onto the canvas, something like this:

enter image description here

Task

What I would like to achieve is the ability to set the opacity of the brush, allowing further possibilities for image editing in my program. Rather than have one flat color been drawn, setting the opacity of the brush allows different levels of color depth etc.

I found this question earlier whilst searching around: Draw opacity ellipse in Delphi 2010 – Andreas Rejbrand has provided a few examples in his answer for that question.

I have looked at what Andreas did, and came up with my own simplified attempt but I am stuck with a problem. Take a look at these next two images, the first is with the transparent background and the second with a black background to show the problem more clearer:

enter image description here
enter image description here

As you can see, around the brush (circle) is a really annoying square I cannot get rid of. All that should be visible is the brush. This is my code used to produce those results:

procedure DrawOpacityBrush(ACanvasBitmap: TBitmap; X, Y: Integer;
  AColor: TColor; ASize: Integer; Opacity: Integer);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.SetSize(ASize, ASize);
    Bmp.Transparent := False;

    with Bmp.Canvas do
    begin
      Pen.Color := AColor;
      Pen.Style := psSolid;
      Pen.Width := ASize;
      MoveTo(ASize div 2, ASize div 2);
      LineTo(ASize div 2, ASize div 2);
    end;

    ACanvasBitmap.Canvas.Draw(X, Y, Bmp, Opacity);
  finally
    Bmp.Free;
  end;
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  DrawOpacityBrush(Image1.Picture.Bitmap, X, Y, clRed, 50, 85);
end;

which produces this on a regular bitmap:

enter image description here

The idea I had (based on Andreas way of creating an ellipse with opacity) was to render a typical brush on the canvas, assign it to a offscreen bitmap then redraw it onto the main bitmap with the opacity. Which works, except that annoying square around the edge.

How can I render a brush with opacity as illustrated in the screenshots, but without that square around the brush circle?

If I set Bmp.Transparent := True there is still a white box, but no opacity anymore. Just a solid white square and solid filled red circle.

  • 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-02T18:39:49+00:00Added an answer on June 2, 2026 at 6:39 pm

    The Opacity feature of TCanvas.Draw() does not support what you are trying to do, at least not the way you are trying to use it. To accomplish the affect you want, you need to create a 32-bit TBitmap so you have a per-pixel alpha channel, then fill in the alpha for each pixel, setting the alpha to 0 for pixels you don’t want, and setting the alpha to the desired opacity for the pixels you do want. Then, when calling TCanvas.Draw(), set its opacity to 255 to tell it to use just the per-pixel opacities.

    procedure DrawOpacityBrush(ACanvas: TCanvas; X, Y: Integer; AColor: TColor; ASize: Integer; Opacity: Byte);
    var
      Bmp: TBitmap;
      I, J: Integer;
      Pixels: PRGBQuad;
      ColorRgb: Integer;
      ColorR, ColorG, ColorB: Byte;
    begin
      Bmp := TBitmap.Create;
      try
        Bmp.PixelFormat := pf32Bit; // needed for an alpha channel
        Bmp.SetSize(ASize, ASize);
    
        with Bmp.Canvas do
        begin
          Brush.Color := clFuchsia; // background color to mask out
          ColorRgb := ColorToRGB(Brush.Color);
          FillRect(Rect(0, 0, ASize, ASize));
          Pen.Color := AColor;
          Pen.Style := psSolid;
          Pen.Width := ASize;
          MoveTo(ASize div 2, ASize div 2);
          LineTo(ASize div 2, ASize div 2);
        end;
    
        ColorR := GetRValue(ColorRgb);
        ColorG := GetGValue(ColorRgb);
        ColorB := GetBValue(ColorRgb);
    
        for I := 0 to Bmp.Height-1 do
        begin
          Pixels := PRGBQuad(Bmp.ScanLine[I]);
          for J := 0 to Bmp.Width-1 do
          begin
            with Pixels^ do
            begin
              if (rgbRed = ColorR) and (rgbGreen = ColorG) and (rgbBlue = ColorB) then
                rgbReserved := 0
              else
                rgbReserved := Opacity; 
              // must pre-multiply the pixel with its alpha channel before drawing
              rgbRed := (rgbRed * rgbReserved) div $FF;
              rgbGreen := (rgbGreen * rgbReserved) div $FF;
              rgbBlue := (rgbBlue * rgbReserved) div $FF;
            end;
            Inc(Pixels);
          end;
        end;
    
        ACanvas.Draw(X, Y, Bmp, 255);
      finally
        Bmp.Free;
      end;
    end;
    
    procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;  
      Shift: TShiftState; X, Y: Integer);  
    begin  
      DrawOpacityBrush(Image1.Picture.Bitmap.Canvas, X, Y, clRed, 50, 85);  
    end;  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a overview page containing a list with some links from which multiple
Overview I am using urlopen from the Python 2.7.1 urllib2 package to do a
Quick overview: trying to build a gallery with a string from $_GET ('foo'), which
I would like to pull stories from Agile Zen using their REST API. I
for an author overview we are looking for a query which will show all
Overview So I have pulled out a document from my database. Inside is a
From the An Overview of the Scala Programming Language, Second Edition : // Scala
Overview I am using CompositeWPF to create an app using C#. This really should
Overview I am trying to get a photo feed on to my site using
Overview I have an iOS project which contains 2 navigation controllers as shown in

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.