Forum > Graphics
Working with Bitmaps and PNGs
Devin:
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
--- Code: ---
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;
--- End code ---
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?
tony72:
I was playing with some Delphi code a while back, and found that Canvas.Pixels did not work in Lazarus, I can't remember if it always returned 0, but I think so. I don't think TCanvas is fully implemented yet, basically. Nor did Bitmap.Scanlines[] work (my plan B for accessing pixel values). This was in Linux, V9.12 or 13, but might well be what you are experiencing.
You can work around it by using a TLazIntfImage instead, it's Colors[x,y] property will return the pixel value.
Also in Delphi, you could use the Transparent & TransparentColor properties of TImage to acheive what you're trying to do without writing any code. No idea if that will work in Lazarus, worth checking though.
felipemdc:
> I was playing with some Delphi code a while back, and found that Canvas.Pixels did not work in Lazarus,
You mean it didn´t work =)
It was implemented very recently, I think it already works on 0.9.16
http://www.freepascal.org/mantis/view.php?id=1661
It works on Windows, Gtk 1 and Gtk 2
Devin:
Thank you both for the responses... I will try out some of those ideas and see if I can get it working the way I want it to.
Any idea about converting to PNG?
Devin:
--- Quote from: "sekel" ---> I was playing with some Delphi code a while back, and found that Canvas.Pixels did not work in Lazarus,
You mean it didn´t work =)
It was implemented very recently, I think it already works on 0.9.16
http://www.freepascal.org/mantis/view.php?id=1661
It works on Windows, Gtk 1 and Gtk 2
--- End quote ---
Okay, I updated to 0.9.16 and it works perfectly. Thank you very much for the help. It's also reassuring to know that it wasn't just my incompitence. :D
Navigation
[0] Message Index
[#] Next page