如何创建位图???(50分)

  • 主题发起人 ETimeFly
  • 开始时间
E

ETimeFly

Unregistered / Unconfirmed
GUEST, unregistred user!
CreateBitmap()怎么用??
如何读入一个位图文件,并创建为位图?返回其句柄供其他调用??
 
这里有一个特大位图的快速显示的例子,或许有些帮助;
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
ScrollBar1: TScrollBar;
ScrollBar2: TScrollBar;
procedure Button1Click(Sender: TObject);
procedure ScrollBar1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ScrollBar2Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
ByteCount =3;
//pixelsFormat if 24bit then the value is 3;32bit is 4;8bit is 1

var
Form1: TForm1;
FileHeader: TBitmapFileHeader;
InfoHeader: TBitmapInfoHeader;
Stream: TFileStream;
LocalBmp: TBitmap;
LineLen: Integer;
ClipLeft, //left of display region
ClipTop, //top of display region
ClipWidth, //width of display region
ClipHeight: integer; //height of display region
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
// the width of a line and the width must be the multiple of 4
for i := ClipTop to ClipTop + ClipHeight - 1 do
// the required height of the picture
begin
Stream.Position := integer(FileHeader.bfOffBits) + LineLen *
(InfoHeader.biHeight
- 1 - i) + ByteCount * ClipLeft;
//the specified position of the point of stream
Stream.Read(LocalBmp.ScanLine[i - ClipTop]^, clipwidth * ByteCount);
end;
Image1.Picture.Bitmap.Assign(LocalBmp);
end;

procedure TForm1.ScrollBar1Change(Sender: TObject); //水平方向
var
i: integer;
begin
// the width of a line and the width must be the multiple of 4
for i := ClipTop to ClipTop + ClipHeight - 1 do
// the required height of the picture
begin
Stream.Position := integer(FileHeader.bfOffBits) + LineLen *
(InfoHeader.biHeight- 1 - i) + (ByteCount * scrollbar1.Position);
//the specified position of the point of stream
Stream.Read(LocalBmp.ScanLine[i - ClipTop]^, clipwidth * ByteCount);
end;
Image1.Picture.Bitmap.Assign(LocalBmp);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
self.DoubleBuffered := true;
ClipLeft := ScrollBar1.Position;
ClipTop := 0;
ClipWidth := image1.Width;
ClipHeight := image1.Height;
Stream := TFileStream.Create('2.bmp', fmOpenRead or fmShareDenyWrite);
LocalBmp := TBitmap.Create; //the bitmap will be displayed
//read the filehead
Stream.Read(FileHeader, SizeOf(TBitmapFileHeader));
Stream.Read(InfoHeader, SizeOf(TBitmapInfoHeader));
//initialize
LocalBmp.Width := ClipWidth;
LocalBmp.Height := ClipHeight;
LocalBmp.PixelFormat := pf24Bit; //24bit
//read bitmap info
LineLen := (ByteCount * InfoHeader.biWidth + 3) shr 2 shl 2;
scrollbar1.Max := InfoHeader.biWidth - image1.Width;
scrollbar2.Max := InfoHeader.biHeight - image1.Height;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
LocalBmp.Free;
Stream.Free;
end;

procedure TForm1.ScrollBar2Change(Sender: TObject); //垂直方向
var
i: integer;
begin
ClipTop := ScrollBar2.Position;
// the width of a line and the width must be the multiple of 4
for i := ClipTop to ClipTop + ClipHeight - 1 do
// the required height of the picture
begin
Stream.Position := integer(FileHeader.bfOffBits) + LineLen *
(InfoHeader.biHeight
- 1 - i) + (ByteCount * scrollbar1.Position);
//the specified position of the point of stream
Stream.Read(LocalBmp.ScanLine[i - ClipTop]^, clipwidth * ByteCount);
end;
Image1.Picture.Bitmap.Assign(LocalBmp);
end;

end.
 
顶部