I want to create a general purpose function that loads 'any' type of image (gif, jpg, bmp, png, etc) from disk and returns a bitmap.
function LoadGraph(FileName: string): TBitmap; {pseudocode}
begin
if FileIsJpeg then
jpeg.LoadFromFile;
Bitmap.Exif.Assign(Jpeg.Exif);
end;
The thing is that I need to have access to the Exif data when the input type is Jpeg.
So, I wanted to create a class helper like this:
TYPE
TBitmapHelper = class helper for TBitmap
public
FExifData: TExif;
end;
However, it seems that the Delphi compiler does not have this capability (yet?) as I get this compile error:
E2599 Field definition not allowed in helper type
How to achieve this?
There is quite complex hierarchy of graphic objects in Delphi intended to work with various image formats: TGraphic is abstract class for some image you can load from file, save to file and draw on canvas. TPicture is container for TGraphic which allows you to write just one line of code:
Picture.LoadFromFile(Filename);
and it will look up correct graphic class, create it and load your image.
So one of solutions for you would be to use TPicture instead of TBitmap. For TBitmap and its descendants TPicture will contain Bitmap property, while for the others you can draw on Canvas or assign Picture.Graphic to your TBitmap (works for TJPEGImage, TPNGImage, but still fails with TIcon).
If it's too bulky, for example, you need not just show image on screen but modify it in some way and not think each time how it's actually represented, I'd recommend to create descendant from TBitmap, not just helper, it's quite possible, you can pass such an object everywhere in program where TBitmap is expected, but now you can extend it with new fields. I'm going to do similar thing to add ICC profiles capability (supported in BitmapHeaderV5).
Related
I have made a C++ program that uses ofstream to create 2 files in my disk: one is called details.obf and the other is records.txt. The file details has only 1 line inside (1 integer) and the file records.txt has a non fixed number of lines (they are all strings).
With the code below I can get the value inside the file details. It's pretty simple and I am using a MemoryStream.
m := TMemoryStream.Create;
try
try
m.LoadFromFile(TPath.Combine(TPath.GetHomePath, 'details.obf'));
m.Read(i, sizeOf(i));
//other stuff...
except
//...
end;
finally
m.Free;
end;
With the code below instead I am reading the content of the records file:
a := TStreamReader.Create('C:\Users\betom\Desktop\records.txt');
try
while not(a.EndOfStream) do
begin
Memo1.Lines.Add(a.ReadLine);
end;
finally
a.Free;
end;
In the second block of code I have used a different class (TStreamReader) and I have written that code looking at embarcadero's documentation. I had to use the while not(a.EndOfStream) do because the lenght of records.txt is unknown.
I have seen that MemoryStream (and other classes) are all subclasses of TStream. Why I cannot call something like while not(m.EndOfStream) do with m a TMemoryStream?
I cannot understand the difference between a MemoryStream and a StreamReader. From what I have understood the latter can read automatically all the values in a given range while the first cannot.
Note: I have read on the docs that I can have a TStreamReader and a TStreamWriter and both are fine when I need to create a file that contains some data. I just cannot understand what are memorystream used for if I have the same behavior with a StreamReader.
TStreamReader is a general purpose class for reading text/character data from any stream. It does not support any other form of data in a stream and is not intended for use with any other form of data.
A stream itself might be a file on disk or data on the network or data in memory. Different stream classes exist to provide stream-access to data from those different sources.
TMemoryStream exists specifically to provide access to data in memory as a sequence of bytes, which may be binary data or text/character data or a mixture of both.
To answer your actual question:
I have seen that MemoryStream (and other classes) are all subclasses
of TStream. Why I cannot call something like while not(m.EndOfStream) do
with m a TMemoryStream?
First a correction. It is correct that TMemoryStream and some other stream manipulating classes (e.g. TFileStream) inherit from TStream. That is however not the case with TStreamReader (and TStringReader). These inherit from TTextReader, which together with TTextWriter and its descendents TStreamWriter and TStringWriter mainly exist to provide familiar classes for .Net users.
Here's the hierarchy of some of the discussed classes:
TObject
TStream
TCustomMemoryStream
TMemoryStream
TBytesStream
TStringStream
THandleStream
TFileStream
TWinSocketStream
TOleStream
TTextReader
TStreamReader
TStringReader
TBinaryReader
The answer is that the property EndOfStream is declared in TStreamReader, iow in a different branch than TMemoryStream.
In TStream descendents you can use e.g. the Position and Size properties to determine if you are at the end of the stream.
So I want to add a Copy/Paste function to my Delphi application that draws different shapes and does stuff with them.
Here's the on even handler for the Copy menu item:
procedure TForm1.Copy1Click(Sender: TObject);
begin
Clipboard.Open;
if SelectShape <> nil then
clipboard.SetComponent(SelectShape);
Clipboard.Close;
end;
And I get the error:
Incompatible types: 'TComponent' and 'TBaseShape'
TBaseShape is the ancestor class for all shapes in my application
I have no idea why it doesn't work...
Another approach would be to use a private data format, and serialize your object to XML or another easy-to-debug text-based structure, and put that onto the clipboard. It would also be useful/polite to render your object (assuming it's a graphic of some sort) onto a Bitmap, and place that on the clipboard as well (clipboard can hold multiple/many formats simultaneously) so that the user can paste into paint, word, etc., and get "something".
Here's a question that does something along these lines, using GPX data
How to paste a custom format clipboard data into a TMemo?
Delphi XE2 Update 4
I have loaded several PNG files 128x128 on TPngImageList and I was to pick one, or either by index number or name, and assign to the TuniImage.Picture
TUniImage is from UniGUi framework. However it is a TPicture class. I guess should be the same of other TPictures that use it.
If you have a TPicture, as you say, then you assign its Graphic property like this:
Picture.Graphic := SomeGraphic;
So, where does SomeGraphic come from. I'm not clear which TPngImageList you are using. So, the code to extract a TGraphic from the TPngImageList might look like this:
SomeGraphic := PngImageList.PngImages[0].PngImage;
I want to create a DLL plugins to use with Delphi and other languages (mostly C++).
How can I pass bitmaps in a C++ and Delphi-friendly way? Can it just be a handle to the Delphi TBitmap? C++ program should be able to decode it using WinApi, right?
You cannot pass a Delphi TBitmap object since that is only meaningful to Delphi code. What you need to pass is an HBITMAP, a handle to a Windows bitmap.
The Delphi TBitmap class is just a wrapper around the Windows bitmap and can provide HBITMAP handles. The thing you need to watch out for is the ownership of those handles.
If you have a Delphi TBitmap you can get an HBITMAP by calling the ReleaseHandle method of a TBitmap. The handle returned by ReleaseHandle is no longer owned and managed by the TBitmap object which is exactly what you want. You pass that handle to the C++ code and let it become the owner. It is responsible for disposing of that handle.
The documentation for ReleaseHandle says:
Returns the handle to the bitmap so that the TBitmap object no longer
knows about the handle.
Use ReleaseHandle to disassociate the bitmap from the bitmap handle.
Use it when you need to give a bitmap handle to a routine or object
that will assume ownership (or destroy) the bitmap handle.
In the other direction your Delphi code would receive an HBITMAP from the C++ code and take on ownership. Do that by assigning to the Handle property of a TBitmap instance.
The details will vary from language to language, but no matter what, all will be able to deal with an HBITMAP.
How can I best determine the Size of a Class from Memory?
Here is a basic sample class to work with - Note the variables serve no purpose other than for the example:
type
TMyClass = class
public
fString1: string;
fString2: string;
fInteger1: Integer;
fInteger2: Integer;
constructor Create;
destructor Destroy; override;
end;
What I would like to do is return the same size as if it were a file on disk.
So if I saved TMyClass to File and the Size of the File was 2.4kb for example, I would like to get that value without even needing the File to be on disk (getting the size from Memory).
I have been searching and reading before asking here, this is what I have tried so far with mixed results:
InstanceSize
Using TMyClass.InstanceSize on my class only ever returns the value 12.
SizeOf
Using SizeOf(TMyClass) on my class only ever returns the value 4.
Whatever the values return I will be formatting to FileSize format such as kilobytes and megabytes etc.
I must be doing something wrong because I know for a fact there is more than 12 (or 4) bytes of data being used on my class. I also know I am referencing the correct class, it is the same class I am creating and freeing in the Form Create and Destroy events - as well as using at runtime.
Is what I am trying to do the correct way to go about it, and if so should my examples work? If they should work, I know then I need to look closer at my actual classes and objects.
The dirty way to achieve this would be for me to save my object classes to file, read the file size and then delete the temp file. But I don't like doing approaches like that, I think they are dirty and taking shortcuts. The purpose is to read the size of my classes from memory, without resorting to saving and temp files etc.
I look forward to hearing your advice and suggestions thanks.
Not sure how you got 12 for your InstanceSize on a class like that. It should be 20 (pre-D2009) or 24 (D2009 and later). The reason it's so much smaller than the actual size of your saved file is because the strings aren't held in the object itself; they're reference types, implemented as pointers to the actual string data.
Your "dirty approach" is on the right track. Pretty much the only way to find out how much disk space you need for an object like that is to actually serialize it. But you don't need to save it to disk. If you're currently saving the object with a TFileStream, (and you should be if you're not,) use a TMemoryStream instead, which will "save" it to a memory buffer. Then get the Size of the stream and that's the serialized size of your object, all without having to create and then delete a temp file.