Question/Problem/Abstract:
This demo shows how to convert a Bitmap into a variant and vice
versa.
Answer:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls, StdCtrls,ActiveX, AxCtrls;
type
TForm2 = class(TForm)
Image1: TImage;
Button1: TButton;
Image2: TImage;
procedure Button1Click(Sender: TObject);
private
procedure BMPToVariant(aBmp: TBitmap;
var aVariant:OleVariant);
procedure StreamToVariant(Stream: TMemoryStream;
var v: OleVariant);
procedure VariantToBMP(aValue: OleVariant;
var aBmp: TBitmap);
procedure VariantToStream(const v: olevariant;
Stream: TMemoryStream);
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.VariantToStream (const v : olevariant;
Stream : TMemoryStream);
var
p : pointer;
begin
Stream.Position := 0;
Stream.Size := VarArrayHighBound (v, 1) - VarArrayLowBound(v, 1) + 1;
p := VarArrayLock (v);
Stream.Write (p^, Stream.Size);
VarArrayUnlock (v);
Stream.Position := 0;
end;
procedure TForm2.StreamToVariant (Stream : TMemoryStream; var v : OleVariant);
var
p : pointer;
begin
v := VarArrayCreate ([0, Stream.Size - 1], varByte);
p := VarArrayLock (v);
Stream.Position := 0;
Stream.Read (p^, Stream.Size);
VarArrayUnlock (v);
end;
procedure TForm2.BMPToVariant(aBmp : TBitmap; var aVariant:OleVariant);
var
Stream : TMemoryStream;
begin
try
Stream := TMemoryStream.Create;
Image1.Picture.Bitmap.SaveToStream(Stream);
StreamToVariant(Stream, aVariant);
finally
Stream.Free;
end;
end;
procedure TForm2.VariantToBMP(aValue : OleVariant;var aBmp:TBitmap);
var
Stream : TMemoryStream;
begin
try
Stream := TMemoryStream.Create;
VariantToStream (aValue,Stream);
aBmp.LoadfromStream(Stream);
finally
VariantClear(aValue);
Stream.free;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
aBmp :TBitmap;
aVariant : OleVariant;
begin
try
aBmp:=TBitmap.Create;
//Bitmap To Variant
aBmp.Assign(Image1.Picture.Bitmap);
BMPToVariant(aBmp,aVariant);
//Variant To Bitmap
VariantToBMP(aVariant,aBmp);
Image2.Picture.assign(aBmp);
finally
aBmp.free;
end;
end;