Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Scanlines

FBitmap := TBitmap.Create;
FBitmap.Width := 512;
FBitmap.Height := 128;
.
.
.
for y := 0 to 127 do begin
  LinePtr := FBitmap.Scanlines[y];



I noticed that Scanlines doesn't work on a bitmap you create on the fly.  Apparently, you need to load a bitmap from a file first.  How do I get around this?  

The following are not options
-----------------------------
1.  Loading a dummy bitmap.
2.  Using DelphiX, or other third party routines.

I'm using Delphi 4.
0
scrapdog
Asked:
scrapdog
1 Solution
 
EpsylonCommented:
Try this:

type
  P32bit = ^T32bit;
  T32bit = array[0..16383] of Longword;

procedure TForm1.Button1Click(Sender: TObject);
var
  x,y : Integer;
  BitMap : TBitMap;
  P : P32bit;
begin
  BitMap := TBitMap.create;
  try
    BitMap.Width := 200;
    BitMap.Height := 100;
    BitMap.PixelFormat := pf32bit;
    for y := 0 to BitMap.height -1 do
    begin
      P := BitMap.ScanLine[y];
      for x := 0 to BitMap.width -1 do
        P[x] := $FF0000;
    end;
  Image1.Picture.Assign(BitMap);
  finally
    BitMap.free;
  end;
end;
0
 
EpsylonCommented:
Or this  :o)

type
  P32bit = ^T32bit;
  T32bit = array[0..16383] of Longword;

procedure TForm1.Button1Click(Sender: TObject);
var
  x,y : Integer;
  BitMap : TBitMap;
  P : P32bit;
begin
  BitMap := TBitMap.create;
  try
    BitMap.Width := 255;
    BitMap.Height := 255;
    BitMap.PixelFormat := pf32bit;
    for y := 0 to BitMap.height -1 do
    begin
      P := BitMap.ScanLine[y];
      for x := 0 to BitMap.width -1 do
        P[x] := $FF0000 + y * 256 + x;
    end;
  Image1.Picture.Assign(BitMap);
  finally
    BitMap.free;
  end;
end;
0
 
TheNeilCommented:
Scanlines certainly DO work on bitmaps that you create on the fly but make sure that they have the correct colour depth (use the PixelFormat property). Typically you need either 24 or 32 bit. Epsylon's code does exactly that

The Neil =:)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
EpsylonCommented:
And here's a 24-bit example:


type
  TRGB = packed record
           r, g, b: Byte;
         end;
  T24bit = array[0..16383] of TRGB;
  P24bit = ^T24bit;

procedure TForm1.Button1Click(Sender: TObject);
var
  x,y : Integer;
  BitMap : TBitMap;
  P : P24bit;
begin
  Caption := IntToStr(sizeof(TRGB));
  BitMap := TBitMap.create;
  try
    BitMap.Width := 256;
    BitMap.Height := 256;
    BitMap.PixelFormat := pf24bit;
    for y := 0 to BitMap.height - 1 do
    begin
      P := BitMap.ScanLine[y];
      for x := 0 to BitMap.width - 1 do
      begin
        P[x].r := $FF;
        P[x].g := x;
        P[x].b := y;
      end;
    end;
  Image1.Picture.Assign(BitMap);
  finally
    BitMap.free;
  end;
end;
0
 
EpsylonCommented:
Oops, I swapped the colors  :o)

  TRGB = packed record
           b, g, r: Byte;
         end;
0
 
DelFreakCommented:
Listening...
0
 
scrapdogAuthor Commented:
Well, I'll be...

All I needed to do was set the PixelFormat.  It works!

Thanks for your help Epsylon, you will be awarded points.

But if you don't mind, since this is a 300-point question, I will leave it open just a bit longer.  I may run into some more issues in what I am trying to do, and might need more of your help.  I am fairly green when it comes to the Windows GDI.

Thanks again.
0
 
MadshiCommented:
Hey scrapdog,

if you're a beginner with the Scanline stuff you should really read this one:

http://www.efg2.com/Lab/ImageProcessing/Scanline.htm

It's all in there...

Regards, Madshi.
0
 
GwenaCommented:
listening :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now