• Status: Solved
• Priority: Medium
• Security: Public
• Views: 198

# Transparent parts for Bitmaps in D1

I'm using Delphi 1 and I was wondering if anyone could supply
my with some source code that would show me how to bring
a bitmap onto my form and not display one of the colors in the
bitmap, so that I can see what would normally be hidden
by that color.  It also needs to be done quickly.

Thanks,
JBreits
0
JBreits
• 2
1 Solution

Commented:
Well, it can be done very easily, but the speed is important... Here is something to try...

function DoTrans(DestForm : TForm; Bmp : TBitmap; Col : TColor) : TBitmap;
var
x,y : Integer;
B : TBitmap;
begin
B := TBitmap.Create;
B.Assign(Bmp);
try
for y := 0 to Bmp.Height - 1 do
for x := 0 to Bmp.Width - 1 do
if Bmp.Canvas.Pixels[x,y] = Col then
B.Canvas.Pixels[x,y] := DestForm.Color
else
B.Canvas.Pixels[x,y] := Bmp.Canvas.Pixels[x,y];
Result.Assign(B);
finally
B.Free;
end;
end;

Let's say you want to have the color red transparent...

var
bmp : TBitmap;
begin
//Create the bitmap and all other stuff you want...
bmp := DoTrans(Form1, bmp, clRed);
Form1.Canvas.Draw(100,100, bmp);
//Free the bitmap and all the stuff...
end;

Try it out and tell me how it works...

Cheers,
Viktor
0

Commented:
Here is an example... I cahgned the function and a bit of the code...

var
bmp : TBitmap;
procedure DoTrans(DestForm : TForm; var Bmp : TBitmap; Col : TColor);
var
x,y : Integer;
begin
for y := 0 to Bmp.Height - 1 do
for x := 0 to Bmp.Width - 1 do
if Bmp.Canvas.Pixels[x,y] = Col then
Bmp.Canvas.Pixels[x,y] := DestForm.Color;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
DoTrans(Form1, bmp, clBlack);
Canvas.Draw(100,100, bmp);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
bmp := TBitmap.Create;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
bmp.Free;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Edit1.Text := Format('%d',[Canvas.Pixels[x,y]]);
end;

Hope this helps...

Cheers,
Viktor
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.