Board index » delphi » can't paint TPaintBox??

can't paint TPaintBox??

Below is the OnPaint event handler I am using for a TPaintBox component.
I'm trying to create a spectrum of colors similar to those in TColorDialog.
However, my procedure only paints about the first 1/5th of the TPaintBox and
it is always a grayscale color.  What am I doing wrong?  The byte array of
TScanLine is not very well defined.  Any help would be appreciated.  Thanks!

Mark Huebner

*********************
// RedValue,GreenValue,BlueValue,LumValue,HueValue,SatValue are global
variables

procedure PaintBoxPaint(Sender: TObject);
var
  ix,iy,SpectrumWidth,SpectrumHeight: integer;
  bmpSpectrum: TBitmap;
  SLine: PByteArray;
begin
  inherited;
  SpectrumWidth := PaintBox.Width;
  SpectrumHeight := PaintBox.Height;
  LumValue := 0;
  bmpSpectrum := TBitmap.Create;
  bmpSpectrum.PixelFormat := pf32bit;
  bmpSpectrum.Width := SpectrumWidth;
  bmpSpectrum.Height := SpectrumHeight;
  for iy := 1 to SpectrumHeight do
  begin
    SLine := bmpSpectrum.ScanLine[iy-1];
    for ix := 1 to SpectrumWidth do
    begin
      HueValue := round(ix*239.0/SpectrumWidth);
      SatValue := round(iy*240.0/SpectrumHeight);
      UpdateRGB;
      SLine[ix-1] := RGBtoTColor(RedValue,GreenValue,BlueValue);  // returns
a TColor value
    end;
  end;
  PaintBox.Canvas.Draw(0,0,bmpSpectrum);
  bmpSpectrum.Free;
end;

 

Re:can't paint TPaintBox??


Hi, I think you have got a problem with the way you are acessinf yout
itmap by scanline.

try the following:

type TPixelValue=record //32Bit per Pixel
  B: byte;
  G: byte;
  R: byte;
  alpha: byte;
end;

...
var
 pixel: ^TPixelValue;
...

Pseudo-Code:

for y ....
  Pixel:=MyBitmap.scanline;
  for x ...
   [...]
   Pixel.R:=RedValue;
   Pixel.G:=GreenValue;
   Pixel.B:=BlueValue;
   Pixel.alpha:=AValue;
   inc(pixel); //!!!

  end x loop
end y loop

(I Think) That will do.
Joann

Re:can't paint TPaintBox??


Additionally to Joann's response,  I would put the bitmap drawing code in a
separate routine which is only called when the image needs to be updated.
The OnPaint handler only needs to blit the bitmap then - which is much less
work for the handler when a simple window update is all that is needed.

Regards

Re:can't paint TPaintBox??


Thanks - that worked!

Other Threads