If you work with Bitmaps you should use the 'Scanline- Method
' of the TBimap- class because its much faster than TCanvas.Pixels.
The following example is taken from the "Delphi 4 Kochbuch" (Delphi 4 Cookbook).
I don't know if there exists an English translation.
In Germany we the "Delphi 7 Kochbuch" meanwhile.
It shows you how to use the scanline method.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, ToolWin, StdCtrls, ImgList;
type
TForm1 = class(TForm)
ToolBar1: TToolBar;
ScrollBox1: TScrollBox;
Image1: TImage;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ImageList1: TImageList;
ToolButton3: TToolButton;
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure ToolButton3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// Here you define a Pixel for 24-bit Bitmaps.
// For 32-bit Bitmap you have to add a byte for the alpha
// channel
TYPE
TRGBValue = packed record
Blue : Byte;
Green: Byte;
Red : Byte;
end;
// ***************************************************
// This procedure fading a Bitmap into grey (grau).
// ***************************************************
procedure TForm1.ToolButton1Click(Sender: TObject);
var x,y : integer;
Pixel : ^TRGBValue;
grau : byte;
begin
If image1.Picture.Bitmap.PixelFormat <> pf24bit then begin
showmessage('Bild hat keine 24-Bitfarben');
exit;
end;
//
//Here comes the interesting part.
//Notice that Pixel is a pointer which size of three
//Bytes
for y := 0 to image1.Picture.Bitmap.Height-1 do begin
Pixel := image1.Picture.Bitmap.Scanline[y];
for x := 0 to image1.Picture.Bitmap.Width-1 do begin
grau := HiByte(Pixel.Red*77+Pixel.Green*151+pixel.Blue*28);
Pixel.Red := grau; // This way you can set colour's Values.
Pixel.blue := grau;
Pixel.green := grau;
inc(Pixel);// the next Pixel
end;
end;
image1.Refresh;
end;
//***************************************************
//This procedure brightens up the bitmap.
//**************************************************
procedure TForm1.ToolButton2Click(Sender: TObject);
var x,y : integer;
Pixel : PByte;
begin
//
// Notice that here Pixel is a pointer on one Byte.
// Each RGB Value will be treated the same way.
//
for y := 0 to image1.Picture.Bitmap.Height-1 do begin
Pixel := image1.Picture.Bitmap.Scanline[y];
for x := 0 to (image1.Picture.Bitmap.Width-1)*3 do begin
if (Pixel^ + 5) > 255 then Pixel^ := 255 else Pixel^ := Pixel^ + 5;
inc(Pixel);
end;
end;
image1.Refresh;
end;
//****************************************
//This procedure darkens the Bitmap
//****************************************
procedure TForm1.ToolButton3Click(Sender: TObject);
var x,y : integer;
Pixel : PByte;
begin
for y := 0 to image1.Picture.Bitmap.Height-1 do begin
Pixel := image1.Picture.Bitmap.Scanline[y];
for x := 0 to (image1.Picture.Bitmap.Width-1)*3 do begin
if (Pixel^ - 5) > Pixel^ then Pixel^ := 0 else Pixel^ := Pixel^ - 5;
inc(Pixel);
end;
end;
image1.Refresh;
end;
end.
I hope this example will help you.
In order to save it to file I would use the Picture.SavetoFile Method or the TFileStream -class.