getbitmapbitsu.pas

来自「Delphi Win32核心API参考光盘源码 本书包含了常用的Windows」· PAS 代码 · 共 59 行

PAS
59
字号
unit GetBitmapBitsU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Image1: TImage;
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
type
  TBitmapBits = array[0..0] of Byte;
var
  BitmapBits: ^TBitmapBits;         // holds the bytes from the bitmap
  LoopRow, LoopCol: Integer;        // loop control variables
begin
  {set the string grid to the dimensions of the bitmap}
  StringGrid1.ColCount:=Image1.Picture.Bitmap.Width;
  StringGrid1.RowCount:=Image1.Picture.Bitmap.Height;

  {dynamically allocate the needed space for the bitmap color data}
  GetMem(BitmapBits,StringGrid1.RowCount*StringGrid1.ColCount);

  {retrieve the color data from the bitmap}
  GetBitmapBits(Image1.Picture.Bitmap.Handle, StringGrid1.RowCount*
                StringGrid1.ColCount, BitmapBits);

  {display the values that define the bitmap in the string grid. since this
   is a 256 color bitmap, these values represent indexes into the bitmap's
   color palette.}
  for LoopRow:=0 to Image1.Height-1 do
    for LoopCol:=0 to Image1.Width-1 do
      StringGrid1.Cells[LoopCol, LoopRow]:=IntToStr(BitmapBits[LoopRow*Image1.Width+LoopCol]);

  {free the allocated memory}
  FreeMem(BitmapBits);    
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?