游子日月长

笑渐不闻声渐悄,多情却被无情恼!

导航

bmp和Variant的转换

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.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;

rocedure 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; 

posted on 2017-02-07 16:52  游子日月长  阅读(171)  评论(0编辑  收藏  举报