ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Increase the size of a layer by keeping its AR?
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

PeterPanino

973 Posts

Posted - Sep 25 2020 :  06:21:34  Show Profile  Reply
I need to programmatically increase the size of a layer by keeping its aspect ratio.

A naive approach would be to increase both the layer-width and the layer-height by one pixel, but that obviously doesn't keep the aspect-ratio when width <> height.

So I came up with this code, assuming that setting TImageEnView.LayersResizeAspectRatio = iearAlways would work even programmatically (it works when resizing the layer with the mouse):

procedure TForm1.btnIncLayerSizeClick(Sender: TObject);
var
  OldLayersResizeAspectRatio: TIELayersResizeAspectRatio;
begin
  OldLayersResizeAspectRatio := ImageEnView1.LayersResizeAspectRatio;
  try
    ImageEnView1.LayersResizeAspectRatio := iearAlways;
    ImageEnView1.Layers[idx].Width := ImageEnView1.Layers[idx].Width + 1;
    ImageEnView1.Update;
    CodeSite.Send('new layer height', ImageEnView1.Layers[idx].Height);
  finally
    ImageEnView1.LayersResizeAspectRatio := OldLayersResizeAspectRatio;
  end;
end;


But unfortunately this does not work programmatically (only when resizing withe mouse).

So is there a method to programmatically increase the size of a layer by keeping its aspect ratio?

PeterPanino

973 Posts

Posted - Sep 25 2020 :  08:58:26  Show Profile  Reply
A (TImageEnView) property to help in programmatically resizing a layer with keeping aspect-ratio could be declared as:

TIELayersResizeAROptions = Set of (lrarShapeSize,
                                   lrarBorderWidth,
                                   lrarFontSize);


So when
LayersResizeAROptions = [lrarShapeSize, lrarBorderWidth]
then when changing the shape-size will automatically change the border-width by keeping aspect-ratio and vice-versa.

Or when
LayersResizeAROptions = [lrarShapeSize, lrarBorderWidth, lrarFontSize]
then when changing the text-layer's font-size will automatically change the layer's shape-size and its border-width by keeping aspect-ratio, and vice-versa.

That of course implicates that when lrarShapeSize is included in LayersResizeAROptions then when changing just the shape-WIDTH, it will automatically change shape-HEIGHT by keeping the aspect ratio!

An alternative would be declaring TIELayersResizeAROptions as:

TIELayersResizeAROptions = Set of (lrarShapeWidth,
                                   lrarShapeHeight,
                                   lrarBorderWidth,
                                   lrarFontSize);


The Default value of LayersResizeAROptions would be:
[lrarShapeWidth, lrarShapeHeight]
which would fit the current behavior of aspect-ratio layer-resizing.

What do you think?
Go to Top of Page

xequte

38988 Posts

Posted - Sep 27 2020 :  21:24:38  Show Profile  Reply
Hi Peter

I think at this stage, I will keep AR adjustment a user action feature.

Programmatically, to maintain AR you need only ensure that you update width/height in the ratio of Layer.Height/Layer.Width which is not too taxing, e.g.

// Set width to 600 while maintaining AR
ar := ImageEnView1.Layers[idx].Width / ImageEnView1.Layers[idx].Height
ImageEnView1.Layers[idx].Width  := 600;
ImageEnView1.Layers[idx].Height := Round( 600 / ar );
ImageEnView1.Update();

// Set height to 600 while maintaining AR
ar := ImageEnView1.Layers[idx].Width / ImageEnView1.Layers[idx].Height
ImageEnView1.Layers[idx].Width  := Round( 600 * ar );
ImageEnView1.Layers[idx].Height := 600;
ImageEnView1.Update();


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

xequte

38988 Posts

Posted - Sep 27 2020 :  21:28:06  Show Profile  Reply
Note: If you are going to update the values often you should use the double versions to avoid rounding errors:

// Set width to 600 while maintaining AR
ar := ImageEnView1.Layers[idx].Width / ImageEnView1.Layers[idx].Height;
ImageEnView1.Layers[idx].WidthD  := 600;
ImageEnView1.Layers[idx].HeightD := 600 / ar;
ImageEnView1.Update();

// Set height to 600 while maintaining AR
ar := ImageEnView1.Layers[idx].Width / ImageEnView1.Layers[idx].Height;
ImageEnView1.Layers[idx].WidthD  := 600 * ar;
ImageEnView1.Layers[idx].HeightD := 600;
ImageEnView1.Update();


Nigel
Xequte Software
www.imageen.com
Go to Top of Page

PeterPanino

973 Posts

Posted - Sep 28 2020 :  05:40:27  Show Profile  Reply
Thank you for the hint about using the double versions. Until now I have used the Integer versions obtained by div operation. It worked well but the double type is certainly better.
Go to Top of Page

xequte

38988 Posts

Posted - Sep 28 2020 :  16:44:01  Show Profile  Reply
Hi Peter

I will publish those properties for 9.2.1.

Nigel
Xequte Software
www.imageen.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: