Wei Sun wrote:
TPicture pic[5];
You can't construct an array of TPicture objects on the stack like
that. TPicture derives from TObject, and TObject
MUST be allocated
on the heap using the 'new' operator. So you need an array of
TPicture* pointers instead, eg:
TPicture* pic[5];
pic[0] = new TPicture;
...
delete pic[0];
Or, you use a smart pointer class like std::auto_ptr (pre-C++11) or
std::unique_ptr (C++11 and later) instead of using 'new'/'delete'
directly.
I can't access implementation files of TPicture class
Yes, you can. TPicture is implemented in Vcl.Graphics.pas in the
$(BDS)\Source\vcl folder. If you want to step into its source code at
runtime, you have to enable Debug DCUs in your project options.
And after some reading, some says there is no such LoadFromStream from
TPicture, but it is documented at
http://docwiki.embarcadero.com/Libraries/Tokyo/en/Vcl.Graphics.TPicture.LoadFromStream
The Berlin documentation shows TPicture::LoadFromStream() was still
'protected' and not directly accessible. The Tokyo documentation shows
it is 'public'. I don't have Berlin or Tokyo installed to verify, but
as of Seattle, it was indeed 'protected'.
However, TPicture does implement the IStreamPersist interface, which
has a LoadFromStream() method. So you can query TPicture for its
IStreamPersist interface (you can use Sysutils::Supports() for that) to
access LoadFromStream() in any version.
Not that it really matters, though, because TPicture::LoadFromStream()
simply calls TPicture::Bitmap::LoadFromStream() (at least up to
Seattle, anyway) , which is 'public', so you could use
pic[0]->Bitmap->LoadFromStream() instead of pic[0]->LoadFromStream().
But obviously, that cannot load non-BMP images.
The
correct way to load a TStream into a TPicture in most versions is
to:
- detect the stream image type
- construct an object of the appropriate TGraphic-derived class for
that image type (TBitmap, TGifImage, TJpegImage, TPngImage, etc, or use
TWICImage)
- load the stream into that object
- Assign() the object to the TPicture
- destroy the object.
TPicture::LoadFromFile() does all of that, but
TPicture::LoadFromStream() does not (at least up to Seattle, anyway).
--
Remy Lebeau (TeamB)
Connect with Us