954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get and Set color on bitmap using ScanLine

Just wondering if someone can tell me what I'm doing wrong here, I just can't seem to work this out, I keep getting errors.

function GetColorFromBmp(XX, YY: Integer; Bmp: TBitmap): TColor;
var
  Line: PRGB32Array;
begin
  Line := Bmp.ScanLine[YY];
  Result := RGBtoColor(Line^[XX].R, Line^[XX].G, Line^[XX].B);
end;

procedure SetColorOnBmp(XX, YY: Integer; Color: TColor; Bmp: TBitmap);
var
  Line: PRGB32Array;
begin
  Line := Bmp.ScanLine[YY];
  Line^[XX].R := GetRValue(Color);
  Line^[XX].G := GetGValue(Color);
  Line^[XX].B := GetBValue(Color);
end;
JacobBruce
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

What are the errors ?

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 
What are the errors ?


Access violation errors. I don't really understand pointers all that well, I don't think I'm doing it properly.

JacobBruce
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

I've figured it out, no need to worry, it was due to me not having declared the TRGB32Array variable type properly. Instead, now I'm using the TRGBTripleArray and pRGBTripleArray types, which are declared like so:

const
  PixelCountMax = 32768;

type
  TRGBTripleArray = array[0..PixelCountMax-1] of TRGBTriple;
  pRGBTripleArray = ^TRGBTripleArray;


I also needed to set the Bitmap PixelFormat's properly before trying to use ScanLine. I also found that I don't need to use the ^ character to refer to what the pointer points to, Delphi seems to do it automatically.

JacobBruce
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Double post, sorry.

JacobBruce
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You