I'm sure it's just my math but I'm wondering if someone has an idea from helping me center an image I'm trying to place on a page. Below is the math I'm using. The image always seems to be a little too far on the X and the Y axes. Can anyone see the problem?
float giftImageWidth = imageWidth / imageResolution * 72 DPI
float giftImageHeight = imageHeight / imageResolution * 72 DPI
ceTe.DynamicPDF.PageElements.Image giftImage = new ceTe.DynamicPDF.PageElements.Image(imagePath,
(float)((PageWidth / 2) - (giftImageWidth / 2)),
(float)((PageHeight / 2) - (giftImageHeight / 2)));
Can't Center An Image
Re: Can't Center An Image
Support · Aug 5, 2008Hello,
Centering an Image on a page is actually very simple. You will just need to dynamically find the center of the page and set the image to vertical and horizontal alignment at that point. C# code for this will look something like this:
Document document= new Document();
Page page = new Page();
float centerX = (page.Dimensions.Width - page.Dimensions.LeftMargin - page.Dimensions.RightMargin) / 2;
float centerY = (page.Dimensions.Height - page.Dimensions.TopMargin - page.Dimensions.BottomMargin) / 2;
Image MyImage = new Image(@"C:\Temp\logo.tif", centerX, centerY);
MyImage.VAlign = VAlign.Center;
MyImage.Align = Align.Center;
page.Elements.Add(MyImage);
document.Pages.Add(page);
document.DrawToWeb();
Typically the Image object's X and Y coordinates are positioned from the top left corner of the image. Setting the Image's Align and VAlign properties to center then the X and Y location specified will actually be positioned from the center of the image. This simplifies the calculations needed to get the image in the center of the page.
The above code will always work no matter what size your image is and no matter what size you page is. The alignment is calculated completely dynamically.
If you know the page size and margins ahead of time then you can hard code the centerX and centerY values. But the code above will work for any page size.
Thanks,
ceTe Software Support Team.
Re: Can't Center An Image
Forum member · Aug 5, 2008That did the trick. Thanks for your help.