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
 Setting the form dimensions to fit the image
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

w2m

USA
1990 Posts

Posted - Jun 25 2014 :  10:07:05  Show Profile  Reply
I have a project I am working on where I need to fit the form position and width to the dimensions of the bitmap. The form must be right aligned to the desktop and its height is the screen.height minus the taskbar height. The zoom is set to FitToHeight. Center is True.

In order to set the Form1.Left position I need to figure out how to calculate the distance between the left edge of the form and the left edge of the image within TImageEnView. This distance varies depending on the dimensions of the image that is loaded. I tried using the OffsetX value but OffsetX always returns 0. OffsetX should return the horizontal position where the image is drawn. If Center property is False, OffsetX will be 0, but in this case, Center is true, so OffsetX should provide the distance from the left edge of the form and the left edge of the image, but it does not... it returns 0.

The screenshot below shows the form as it appears after I try to set the Form1.Left and Form1.Width values. The whitespace between the left side of the form and the left side of the image is the value I need to calculate or get from an ImageEnView property.

The screenshot below shows the form as I would like it to appear.

Here is the code I am using... Note that the code is sloppy because I have been trying different ways to adjust the left edge of the form to eliminate the whitespace without success.


function TaskbarHandle: Winapi.Windows.THandle;
begin
  Result := Winapi.Windows.FindWindow('Shell_TrayWnd', nil);
end;

function TaskbarBounds: Winapi.Windows.TRect;
var
  Data: Winapi.ShellAPI.TAppBarData; // structure receive task bar info
begin
  // set up data structure
  Data.cbSize := SizeOf(Data);
  Data.HWND := TaskbarHandle;
  // get bounding rectangle
  if Winapi.ShellAPI.SHAppBarMessage(Winapi.ShellAPI.ABM_GETTASKBARPOS, Data) <> 0
  then
    Result := Data.rc
  else
    // can't get task bar info: return empty rectangle
    FillChar(Result, SizeOf(Result), 0);
end;

procedure TForm1.Open1Click(Sender: TObject);
var
  iWidth: Integer;
  iLeft: Integer;
  iTop: Integer;
  iMx: Integer;
  iMy: Integer;
  iTaskBarHeight: Integer;
  iHeight: Integer;
  iAspectRatio: double;
  iHorzContainerWidth: Integer;
  iOffsetX: Integer;
  x: integer;
  xo: integer;
begin
  {Make the form right aligned to the desktop and adjust the form dimensions to fit the image}
  if OpenPictureDialog1.Execute then
  begin
    ImageEnView1.IO.LoadFromFile(OpenPictureDialog1.FileName);
    ImageEnView1.Center := True;
    { Set up the form dimensions and position }
    if ImageEnView1.IEBitmap.Width <= Screen.Width then
    begin
      iAspectRatio := ImageEnView1.Bitmap.Width / ImageEnView1.Bitmap.Height;
      iHorzContainerWidth := Round(ImageEnView1.Bitmap.Height * iAspectRatio);
      iLeft := Screen.Width - iHorzContainerWidth;
      iWidth := ImageEnView1.Bitmap.Width;
      Form1.Left := iLeft;
      Form1.Width := iHorzContainerWidth;
      iTop := 0;
      iTaskBarHeight := TaskbarBounds.Bottom - TaskbarBounds.Top;
      iHeight := Screen.Height - iTaskBarHeight;
      Form1.Top := iTop;
      Form1.Height := iHeight;
      ImageEnView1.FitToHeight;
    end
    else if ImageEnView1.IEBitmap.Width >= Screen.Width then
    begin
      { This part does not work as needed.  There is white space between the
        left side of the form and the left edge of the image and between the
        right side of the form and the right edge of the image.  To eliminate
        this space, the forms left position needs to be adjusted, but I can
        not figure out how to use ImageEn properties to get the width of the
        white space to adjust Form1.Left and Form1.Width. }
      iTaskBarHeight := TaskbarBounds.Bottom - TaskbarBounds.Top;
      iHeight := Screen.Height - iTaskBarHeight;
      iAspectRatio := ImageEnView1.Bitmap.Width / ImageEnView1.Bitmap.Height;
      iHorzContainerWidth := Round(iHeight * iAspectRatio);
      iLeft := Screen.Width - iHorzContainerWidth;
      iWidth := ImageEnView1.Bitmap.Width;
      iTop := 0;
      Form1.Top := iTop;
      Form1.Height := iHeight;
      ImageEnView1.FitToHeight;
      iOffsetX := Screen.Width - iHorzContainerWidth;
      xo := ImageEnView1.OffsetX;
      Form1.Left := iOffsetX;
      x := iHorzContainerWidth - ImageEnView1.ExtentX;
      Form1.Width := Screen.Width - Form1.Left;
    end;
  end;
end;

Any suggestions on how to accomplish this would be appreciated. A small demo that I am using to test this can be downloaded here:
http://www.imageen.com/ieforum/attach/w2m/20146251176_AlignFormRight.zip

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development

Uwe

284 Posts

Posted - Jun 25 2014 :  11:53:23  Show Profile  Reply
Bill,

Wouldn't the width of Form1 exceed the screen width if IEBitmap.Width > Screen.Width ? You'll have to either create a fixed size Form1, limit the width of Form1, or scale the image.

-Uwe
Go to Top of Page

w2m

USA
1990 Posts

Posted - Jun 25 2014 :  12:14:05  Show Profile  Reply
Uwe,

The image must occupy as much of the screen as possible. We have the height working ok because FitToHeight is used, but when the resulting width is smaller than the Form width after calling FirToHeight, the Form1.Left and Form1.Width has to be adjusted to fit around the bitmap.

A fixed form is not what my client is looking for. He wants to resize the form if the bitmap width is less than the form width and he wants to scale the image if the bitmap is wider than the form, and eliminate as much "whitespace" as possible.

You can not just scale the image because we are using the FitToHeight which results in "whitespace" on the left and right side of the bitmap.

If there is a way to get the width of the whitespace like with OffsetX, the Form1.Left and Form1.Width may be adjusted so that the whitespace is reduced or eliminated so the form resizes itself to fit around the bitmap while remaining aligned to the right as shown in the screen captures.

Personally I do not like the approach, but it is not my spec so I am trying to accommodate what the client is looking for. All we need is a way to get the OffsetX, the distance between the left side of the component and the left size of the bitmap then the form can be adjusted accordingly.


Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

xequte

39061 Posts

Posted - Jun 25 2014 :  13:28:18  Show Profile  Reply
Hi Bill

TImageEnView.ViewX gives you the position of image within the component (see also ExtentX).

But to resize your form after displaying an image will cause unnecessary repaint (so you might want to use LockPaint/UnlockPaint).

You might also consider (pseudo-code):

LockUpdate of ImageEnView1
Load Image via ImageEnView1.IO
Get size of ImageEnView1.IEBitmap
Calculate optimal size of image (e.g. using hyieutils.GetImageSizeWithinArea or GetImageRectWithinArea)
Set form size based on optimal size
Set ImageEnView1.zoom with optimal size.X/IEBitmap.Width
UnlockUpdate




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