Mega Code Archive

 
Categories / Delphi / Examples
 

Bitmapasvariant

************************************************** [with Google search for "delphi image bitmap variant"] [and you find this on efg's Delphi Graphics site] ************************************************** Subject: Re: Help with DIB's [PUT BITMAP INTO VARIANT AND PULL IT OUT AGAIN] Date: 12/22/1999 Author: Bob Villiers <100522.66@COMPUSERVE.COM> Sal, Take a look at the code below. I have used the function VarArrayLoadFile to load a bitmap file into a variant array which is the state I believe you are in with the return from your ocx. I then load the array into a memory stream, as a TBitmap has a LoadFromStream method. In this case I had an array containing the whole bitmap ie. header and pixel data. If your variant array contains just the pixel data you will need to do two writes to the stream, the first to write the bitmap header and the second (as below) to write the pixel data. Bob function VarArrayLoadFile(const FileName: string): Variant; var F: file; Size: Integer; Data: Pointer; begin AssignFile(F, FileName); Reset(F, 1); try Size := FileSize(F); Result := VarArrayCreate([0, Size - 1], varByte); Data := VarArrayLock(Result); try BlockRead(F, Data^, Size); finally VarArrayUnlock(Result); end; finally CloseFile(F); end; end; procedure TForm1.Button1Click(Sender: TObject); var vArray: Variant; iSize: LongInt; mStream: TMemoryStream; Bmp: TBitmap; buffer: Pointer; begin {Load bitmap into variant array.} vArray:= VarArrayLoadFile('CHEMICAL.BMP'); {Calculate size of array} iSize:= VarArrayHighBound(vArray, 1) - VarArrayLowBound(vArray, 1)+1; {Get a pointer to the array buffer} buffer:= VarArrayLock(vArray); try {Create stream} mStream:= TMemoryStream.Create; try {Preset stream size} mStream.SetSize(iSize); {Write bitmap buffer to the stream} mStream.Write(buffer^ ,iSize); {Reset the stream to offset 0} mStream.Seek(0, 0); {Create the TBitmap and load the bitmap from the MemoryStream} Bmp := TBitmap.Create; try Bmp.LoadFromStream(mStream); {Draw the bitmap on the form canvas} Canvas.Draw(10, 10, Bmp); finally Bmp.Free; end; finally mStream.Free; end; finally {Remove the lock} VarArrayUnlock(vArray); end; end; Subscribe to borland.public.delphi.graphics **************************************************