[Lazarus] TPicture width questions (duplicate ways)

noreply at z505.com noreply at z505.com
Fri Mar 10 18:52:01 CET 2017

On 2017-03-10 11:01, Kostas Michalopoulos via Lazarus wrote:
> 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.

You can check the current sources but that does not mean it will stay 
that way, or that the current sources do not have a bug in them... the 
documentation would tell you how it is supposed to act, and, the source 
is one particular implementation, which could not match up with what the 
doc says, in that case it is buggy ;-)

But thanks very much for explaining it: indeed I figured any call to 
.jpeg.someproperty would immediately automagically do the work for you

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

This is very useful - I thought maybe it  would be in a wiki or docs 
somewhere.. It may be...

> 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 :-).

IMO the docs should always describe the theory of an object/procedure, 
and therefore if any time the source code is buggy, the docs are the 
official sources to what a function does.. :-)

Thanks much for documenting it in this mailing list !

Some of your comments, IMO should be uploaded to wiki/docs somewhere
But I'm unfamiliar with how the docs updating process works

More information about the Lazarus mailing list