Search code examples
delphigditbitmap

Increasing the size or depth of a TBitmap


When I increase the size of a bitmap by (for example) ...

bitmap.Width := bitmap.Width + 30;

... do I have any control of what the right-hand 30 pixels get filled with, or do I just assume they're uninitialized

Similarly if I change PixelFormat from pf24bit to pf32bit, can I control what the alpha bytes are initialized with?

Embarrasingly newbie question, but my google-fu has failed me. :)


Solution

  • Since it isn't defined in the documentation what happens in this instance, you cannot rely on anything. Most likely the new pixels will be 0 (black), but if it is important to you what they are, you should explicitly fill them:

    FUNCTION ExpandBitMap(BMP : TBitMap ; AddW,AddH : Cardinal ; FillColor : TColor = clBlack) : TBitMap;
      VAR
        OrgW,OrgH : Cardinal;
    
      BEGIN
        OrgW:=BMP.Width; OrgH:=BMP.Height;
        BMP.Width:=OrgW+AddW; BMP.Height:=OrgH+AddH;
        BMP.Canvas.Brush.Color:=FillColor;
        BMP.Canvas.Brush.Style:=bsSolid;
        BMP.Canvas.FillRect(Rect(OrgW,0,BMP.Width,BMP.Height));
        BMP.Canvas.FillRect(Rect(0,OrgH,OrgW,BMP.Height));
        Result:=BMP
      END;
    

    Likewise with the Alpha channel - I'll leave it as an exercise to the user to make a similar function :-).