Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Working with Bitmap at Run-time (Help)

TiefTief Posts: 179Member
Hi,

Here's my code :

I am trying to change all the blue pixels in a bitmap to
a color I choose.

2 questions

1) I would like to know how to compare a Bitmap byte color
with a TColor Value ?
2) Is there a better way of doing this ?

[code]
var
liY, liX : Integer;
loGusBitmap : TBitmap;
loBytes : PByteArray;
...
For liY := 0 to loGusBitmap.Height- 1 do
begin
loBytes := loGusBitmap.ScanLine[liY];
For liX := 0 to loGusBitmap.Width - 1 do
begin
if (loBytes[liX] = clBlue) // Problem Here !!!!!
then loBytes[liX] := ReportColor;
end;
end;
ImaGustaveBleu.Canvas.Draw(0,0,loGusBitmap);
loGusBitmap.Free;
...
[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi,
    :
    : Here's my code :
    :
    : I am trying to change all the blue pixels in a bitmap to
    : a color I choose.
    :
    : 2 questions
    :
    : 1) I would like to know how to compare a Bitmap byte color
    : with a TColor Value ?
    : 2) Is there a better way of doing this ?
    :
    : [code]
    : var
    : liY, liX : Integer;
    : loGusBitmap : TBitmap;
    : loBytes : PByteArray;
    : ...
    : For liY := 0 to loGusBitmap.Height- 1 do
    : begin
    : loBytes := loGusBitmap.ScanLine[liY];
    : For liX := 0 to loGusBitmap.Width - 1 do
    : begin
    : if (loBytes[liX] = clBlue) // Problem Here !!!!!
    : then loBytes[liX] := ReportColor;
    : end;
    : end;
    : ImaGustaveBleu.Canvas.Draw(0,0,loGusBitmap);
    : loGusBitmap.Free;
    : ...
    : [/code]
    :
    I would choose to use the TBitmap.Canvas.Pixels() property instead of the ScanLine.
    The error is generated not in the comparison itself, but in the fact that loBytes is a pointer, which points to an array of byte. In your case you need to change the code to get the array itself. Here is how:
    [code]
    if (loBytes^[liX] = integer(clBlue))
    [/code]
    The typecast of clBlue into an integer is not really necessary I believe, but I included it to be on the save side.
  • TiefTief Posts: 179Member
    Thanks,

    I decided to work with this code which is very simple and it works

    [code]
    procedure AdjustBitmap(var poBitmap: TBitmap; pclOld, pclNew: TColor);
    var
    liX, liY : Integer;
    begin
    for liX := 0 to poBitmap.Height - 1 do
    begin
    for liY := 0 to poBitmap.Width - 1 do
    begin
    if (poBitmap.Canvas.Pixels[i,j] = pclOld)
    then poBitmap.Canvas.Pixels[i,j] := pclNew;
    end;
    end;
    end;
    [/code]
Sign In or Register to comment.