[Lazarus] TPicture width questions (duplicate ways)

Kostas Michalopoulos badsectoracula at gmail.com
Fri Mar 10 18:01:33 CET 2017

You can check yourself easily: Ctrl+Click on the p.Width to go to the
definition of the property, then Ctrl+Click on the GetWidth getter,
Ctrl+Shift+Down arrow to see the code for the getter and see that it simply
gets FGraphic's property. So what is FGraphic? Ctrl+Home to move at the top
of the source code (you'd be in picture.inc) and search for "FGraphic :="
to see where it is assigned (the source code style in that file seems to
put a space before := in assignments). You can see that it is used in a
bunch of places, so you can see where those uses are. ForceType is the
first one and if you search for that one, you'll find that it is used
wherever a type-specific property (p.Jpeg, p.Bitmap, etc) is used -
presumably to convert the underlying graphic to the requested type. So the
FGraphic object is actually an instance of the TBitmap,
TPortableNetworkGraphic, TJpegImage, etc (depending what you loaded and/or
accessed last).

So what p.Width gives you is the FGraphic's width or 0 if there is no
FGraphic loaded (because a picture may have nothing loaded until you either
load it directly or try to access any of the Bitmap, Jpeg, PNG, etc
properties). So if you already have a picture loaded from a PNG file,
p.Width, p.Graphic.Width and p.PNG.Width will give you the same number. If
your picture doesn't have a graphic (e.g. it is a newly constructed object)
then p.Width gives you 0, p.Graphic.Width will crash your program (FGraphic
is nil) and p.PNG.Width will give you zero (because accessing the PNG
property will call the ForceType function which will create a
TPortableNetworkGraphic object that by default is undefined and gives you
zero width).

Question for the readers: what happens to the image if you load a PNG file
with alpha values using TPicture's LoadFromFile, access the
TPicture.Jpeg.Width property and then save the picture as a PNG file using
TPicture's SaveToFile? Is the alpha component preserved? Do you get jpeg
artifacts? Why?

Bonus question: why is R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height) worse
than R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height)? Why is R := Rect(0, 0,
p.Width, p.Height) better than either of them?

You can find the answers to those (and many more) using Ctrl+Click,
Ctrl+Shift+Up/Down Arrow, Search and back and forward buttons (helps to
have a mouse with those). Considering how big and complex LCL is, it helps
to be able to navigate and familiarize yourself with its source code :-).

On Thu, Mar 9, 2017 at 7:44 PM, Lars via Lazarus <
lazarus at lists.lazarus-ide.org> wrote:

> On 2017-03-09 10:48, José Mejuto via Lazarus wrote:
>> TPicture.Width is the TGraphic container size.
>> TPicture.jpeg.Width is the width declared in the jpeg image.
>> TPicture.Bitmap.Width is the width of the bitmap in pixels.
>> Mostly the 3 will have the same value but extracted from different
>> sources.
> If I start off using a bitmap, and all of a sudden start using jpeg, will
> it automagically work or does one have to initiate the jpeg in some way?
> Example: you start off working with a classic BMP, but then 60 lines of
> code later you want to work with it as a jpeg. Do you just magically start
> working with a jpeg without any worry, or you have to take some precautions?
> Likely, this is explained in a article on the wiki somewhere, I don't know
> :-)
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus-ide.org
> http://lists.lazarus-ide.org/listinfo/lazarus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20170310/9083ee14/attachment.html>

More information about the Lazarus mailing list