Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read pixels from a bitmap 1

Status
Not open for further replies.

iolus

Programmer
Jun 14, 2005
1
TR
please help me to do this subject
i want to read pixels of bitmap file (only pixels)
it can be on the picture or can be in file
can say me how can i do both of them (reading pixels)
and one more problem...
if i learn to how to doing it
how can i writing value of pixels to the new file?
thank you
 
TBitmap has a Canvas property.
TCanvas has a Pixels property.
TPixels[x,y] is a TColor

So:
[tt]
var
MyBitMap:TBitMap;
:
:
MyBitMap.Canvas.Pixels[3,7];
[/tt]
gives the color value at pixel coordinate [3,7].

You will need to create the TBitmap and then load it with your bitmap (Assign, or one of the LoadFrom... methods -- see the help file.)

 


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.
 
Just a tip for the future too: please give each thread you start a meaningful title. "How can I do?" doesn't really give anyone a clue as to what your question is, so many people may skip over it. Something like "How do I read pixels from a bitmap?" would be better.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top