Hi!
i have a given area on my document dedicated for an image.
if the ratio of the image is different - i have to adjust the image to the area (adjusted to the height or width of the area) WITHOUT distortion (of course the quality of the image might get affected) and keep it centered in the area
Do someone have a script for the above?
Centering an image in a given area
Re: Centering an image in a given area
Support · Feb 18, 2008Hello,
You can set the image's height and width using the Height and Width properties of the image object without any problem. We add the images as is to the PDF and we do not do any modifications to the data of the image.
You can also use the SetDpi method to set the scale of the image.
If you want to add the image at the center position on the page then you will have to set the X, Y of the image depending on the page dimensions and the image dimensions. Following is the code to calculate this.
image.X = (page.Dimensions.Body.Width - image.Width) / 2;
image.Y = (page.Dimensions.Body.Height - image.Height) / 2;
Thanks,
ceTe Software Support Team.
Re: Centering an image in a given area
Forum member · Feb 18, 2008thanks for the reply - but it's not what i meant...
the case is: having a given area (width=370 & height=160) positionned at (x=215 & y=80) we have to put ANY image and centering it in the area!
here is the code...and it works!
CurrentY = 80
CurrentX = 215
'
'*** IMAGE Zone ****
If Data.ImgSrc <> "" Then
Dim MyImage As Image = New Image(ImgSrc, CurrentX, CurrentY)
Dim AreaWidth As Single = 370 'Image Area Width
Dim AreaHeight As Single = 160 'Image Area Height
Dim ImgWidth As Single 'Image Width
Dim ImgHeight As Single 'Image Height
Dim NewWidth As Single
Dim NewHeight As Single
ImgWidth = MyImage.Width
ImgHeight = MyImage.Height
MyImage.X = CurrentX
MyImage.Y = CurrentY
NewHeight = (ImgHeight * AreaWidth) / ImgWidth
If NewHeight > AreaHeight Then
NewHeight = AreaHeight
'calculate New image Width
NewWidth = (ImgWidth * AreaHeight) / ImgHeight
MyImage.X = CurrentX + ((AreaWidth - NewWidth) / 2)
ElseIf NewHeight <= AreaHeight Then
NewWidth = AreaWidth
'calculate New image Height
NewHeight = (ImgHeight * AreaWidth) / ImgWidth
MyImage.Y = CurrentY + ((AreaHeight - NewHeight) / 2)
End If
MyImage.Width = NewWidth
MyImage.Height = NewHeight
' Add the image to the page
NewPage.Elements.Add(MyImage)
End If