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 9031899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:50:44+00:00 2026-06-16T07:50:44+00:00

Any library/code to fade the edges of a bitmap in a gradient manner? Something

  • 0

Any library/code to fade the edges of a bitmap in a gradient manner?

Something like this:

enter image description here

Edit: final code

Ok came up with this code after your example, it’s ~10 times faster after optimization with scanlines. Ideally I think I should convert it to use a 32bit bitmap instead and modify the actual alpha layer, but this works for now, ty!

procedure FadeEdges(b: TBitmap; Depth, Start, Col: TColor);
Var f, x, y, i: Integer;
    w,h: Integer;
    pArrays: Array of pRGBArray;
    xAlpha: Array of byte;
    sR, sG, sB: Byte;
    a,a2: Double;
    r1,g1,b1: Double;
    Lx,Lx2: Integer;
procedure AlphaBlendPixel(X, Y: Integer);
begin
  pArrays[y,x].rgbtRed   := Round(r1 + pArrays[y,x].rgbtRed   * a2);
  pArrays[y,x].rgbtGreen := Round(g1 + pArrays[y,x].rgbtGreen * a2);
  pArrays[y,x].rgbtBlue  := Round(b1 + pArrays[y,x].rgbtBlue  * a2);
end;
procedure AlphaBlendRow(Row: Integer; Alpha: Byte);
Var bR, bG, bB, xA: Byte;
    t: Integer;
    s,s2: Double;
begin
  s  := alpha / 255;
  s2 := (255 - Alpha) / 255;

  for t := 0 to b.Width-1 do begin
    bR := pArrays[Row,t].rgbtRed;
    bG := pArrays[Row,t].rgbtGreen;
    bB := pArrays[Row,t].rgbtBlue;
    pArrays[Row,t].rgbtRed   := Round(sR*s + bR*s2);
    pArrays[Row,t].rgbtGreen := Round(sG*s + bG*s2);
    pArrays[Row,t].rgbtBlue  := Round(sB*s + bB*s2);
  end;
end;
begin
  b.PixelFormat := pf24bit;

  // cache scanlines
  SetLength(pArrays,b.Height);
  for y := 0 to b.Height-1 do
   pArrays[y] := pRGBArray(b.ScanLine[y]);

  // pre-calc Alpha
  SetLength(xAlpha,Depth);
  for y := 0 to (Depth-1) do
   xAlpha[y] := Round(Start + (255 - Start)*y/(Depth-1));

  // pre-calc bg color
  sR := GetRValue(Col);
  sG := GetGValue(Col);
  sB := GetBValue(Col);

  // offsets
  w := b.Width-Depth;
  h := b.Height-Depth;

  for i := 0 to (Depth-1) do begin
    a  := xAlpha[i] / 255;
    a2 := (255 - xAlpha[i]) / 255;
    r1 := sR * a;
    g1 := sG * a;
    b1 := sB * a;
    Lx  := (Depth-1)-i;
    Lx2 := i+w;
    for y := 0 to b.Height - 1 do begin
      AlphaBlendPixel(Lx, y); // Left
      AlphaBlendPixel(Lx2, y); // right
    end;
  end;

  for i := 0 to (Depth-1) do begin
    AlphaBlendRow((Depth-1)-i, xAlpha[i]); // top
    AlphaBlendRow(i+(h), xAlpha[i]); // bottom
  end;

  SetLength(xAlpha,0);
  SetLength(pArrays,0);
end;

Final result: (left = original, right = blended on hovering with a ListView)

enter image description here

edit: further speed improvements, twice as fast as original proc.

  • 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-16T07:50:45+00:00Added an answer on June 16, 2026 at 7:50 am

    I can give you some code I wrote a couple of years ago to achieve this. It might be useful as a guide. The code is part of a class that manipulates a bitmap and this is the part that fades the left edge of the bitmap into a white background:

    procedure TScreenShotEnhancer.FadeOutLeft(Position, Start: Integer);
    var
      X, Y: Integer;
      F, N: Integer;
      I: Integer;
    begin
      BeginUpdate;
      try
        N := Position;
        for I := 0 to N - 1 do begin
          X := Position - I - 1;
          F := Round(Start + (255 - Start)*I/N);
          for Y := 0 to Height - 1 do
            AlphaBlendPixel(X, Y, clWhite, F);
        end;
      finally
        EndUpdate;
      end;
    end;
    

    The actual work is done in this method:

    procedure TScreenShotEnhancer.AlphaBlendPixel(X, Y: Integer; Color: TColor;
        Alpha: Byte);
    
    var
      backgroundColor: TColor;
      displayColor: TColor;
      dR, dG, dB: Byte;
      bR, bG, bB: Byte;
      sR, sG, sB: Byte;
    begin
      backgroundColor := Bitmap.Canvas.Pixels[X, Y];
      bR := GetRValue(backgroundColor);
      bG := GetGValue(backgroundColor);
      bB := GetBValue(backgroundColor);
      sR := GetRValue(Color);
      sG := GetGValue(Color);
      sB := GetBValue(Color);
    
      dR := Round(sR * alpha / 255 + bR * (255 - alpha) / 255);
      dG := Round(sG * alpha / 255 + bG * (255 - alpha) / 255);
      dB := Round(sB * alpha / 255 + bB * (255 - alpha) / 255);
      displayColor := RGB(dR, dG, dB);
      Bitmap.Canvas.Pixels[X, Y] := displayColor;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you code a distributed algorithm, do you use any library to model abstract
is there any library that parse a source code of C++ to produce lets
Note : if you know of any (non-elaborate) library code that does what I
Is there any library that can be used to convert Android java code to
I've come across the excellent PDF Library code SynPDF and would like to use
Is there any library in python for duplicate code checker? I use python IDE
Is there any library available that can tokenize source code written in different programming
Does someone know any library or ready source code of parallel implementation of quick
Is there any library available to query Btrieve databases without buying something from Pervasive?
So I have this library code, see... class Thing { public: class Obj {

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.