简体   繁体   English

如何将 fMX.TImage 分配给 VCL.TBitmap?

[英]How to assign s a FMX.TImage to a VCL.TBitmap?

I try to do this:我尝试这样做:

BMP.Assign(Image1.Bitmap);

Image1 is a FMX object. Image1 是一个 FMX 对象。
BMP is a standard (VCL.Graphics) bitmap. BMP 是标准 (VCL.Graphics) 位图。

The error I get is:我得到的错误是:

Project Project1.exe raised exception class EConvertError with message
'Cannot assign a TBitmapOfItem to a TBitmap'.

You cannot assign an FMX TBitmap to a VCL TBitmap .您不能将 FMX TBitmap分配给 VCL TBitmap They are not compatible with each other (you shouldn't even be mixing VCL and FMX in the same project to begin with, they are not designed to be used together).它们彼此不兼容(您甚至不应该在同一个项目中混合使用 VCL 和 FMX,它们不是为了一起使用而设计的)。

You will have to save the FMX TBitmap to a BMP-formatted stream/file and then load that into the VCL TBitmap .您必须将 FMX TBitmap保存到 BMP 格式的流/文件,然后将其加载到 VCL TBitmap

Using a file is straight forward:使用文件很简单:

Image1.Bitmap.SaveToFile('file.bmp');
BMP.LoadFromFile('file.bmp');

However, when using a stream instead, FMX's TBitmap.SaveToStream() saves in PNG format only, so you have to use TBitmapCodecManager.SaveToStream() to save in BMP format, eg:但是,当使用流代替时,FMX 的TBitmap.SaveToStream()仅以 PNG 格式保存,因此您必须使用TBitmapCodecManager.SaveToStream()以 BMP 格式保存,例如:

Strm := TMemoryStream.Create;
try
  Surface := TBitmapSurface.Create;
  try
    Surface.Assign(Image1.Bitmap);
    TBitmapCodecManager.SaveToStream(Strm, Surface, '.bmp');
  finally
    Surface.Free;
  end;
  Strm.Position := 0;
  BMP.LoadFromStream(Strm);
finally
  Strm.Free;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM