Working with Bitmap at Run-time (Help) - Programmers Heaven

Howdy, Stranger!

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

Categories

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.