Ok, so I am completely new at this, so any help you can give me is much appreciated.
I have a bitmap displayed on the screen, 125 x 90 pixels. The user clicks a button, picks another 125 X 90 bitmap. The program is supposed to overlay the selected image onto the first, interpreting black as transparent, and then replace the displayed image with the resulting image.
I wrote some code to do this, where you basically loop through each pixel of the overlay image, and if it's black you use the pixel from the original image, else you use the pixel from the overlay to create the new image. However, it's not working correctly.
The program seems to interpret any solid colour as black, so a value of rgb(255,0,0) would be interpreted as transparent, rather than red.
Here's the code I'm using
procedure TForm1.Button1Click(Sender: TObject);
var
LoadImage: TBitmap;
SwapImage: TBitmap;
BackColour, OverColour, SwapColour: TColor;
px, py: Integer;
begin
OpenPictureDialog1.Execute;
//this is where the magic happens.
LoadImage:=TBitmap.Create;
LoadImage.LoadFromFile(OpenPictureDialog1.FileName);
LoadImage.Transparent:=False;
SwapImage:=TBitmap.Create;
SwapImage.Width:=90;
SwapImage.Height:=135;
SwapImage.Transparent:=False;
//go pixel by pixel to create SwapImage
for px:= 0 to 89 do begin
for py:= 0 to 134 do begin
//get overlay colour
OverColour:=LoadImage.Canvas.Pixels[px,py]; //from overlay image, overlay colour
if (OverColour=$000000) then begin //if overlay pixel is black
BackColour:=Image1.Canvas.Pixels[px,py]; //a fancy way of doing absolutely nothing
SwapColour:=BackColour;
end else begin
SwapColour:=OverColour; //if the overlay pixel is not black, put that pixel in the swap value
end;
SwapImage.Canvas.Pixels[px,py]:=SwapColour; //put the swap colour in the swap image
end;
end;
//Swap Image becomes the displayed image
Image1.Picture.Bitmap:=SwapImage;
//kill the rest and let God sort 'em out
LoadImage.Destroy;
SwapImage.Destroy;
end;
I've never really worked much with graphics before, so I don't really know what I'm doing.
As a second question: is there an easy way to convert & save the resulting bitmap in PNG format?