PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Apr 16, 2024 6:59 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Image fit in table
PostPosted: Tue Jan 08, 2013 3:02 pm 
Offline

Joined: Tue Apr 24, 2012 2:28 pm
Posts: 7
Hi there,
A simple question: I have a table cell, 15cm wide and 19 cm high. I want to fit any image into it, keeping aspect ratio.

Here is what I try
Code:
   Paragraph par = row.Cells[1].AddParagraph();
   par.Format.Alignment = ParagraphAlignment.Center;
   MigraDoc.DocumentObjectModel.Shapes.Image image = par.AddImage("myImage.png");

   /*xxx*/
   image.LockAspectRatio = true;


Now,
If I make
Code:
/*xxx*/
be
Code:
image.Height = "19cm";
, the width gets too big for a landscape image.
If I make
Code:
/*xxx*/
be
Code:
image.Width = "15cm";
, the height gets too big for a portrait image.
If I make set both width and height, I loose my aspect ratio. I bet this is by design, but how do I scale my image to max, keeping aspect ration and without exceeding the cell limits ?

Thanks a lot in advance
Lars Klitgaard, Denmark


Top
 Profile  
Reply with quote  
 Post subject: Re: Image fit in table
PostPosted: Tue Jan 08, 2013 3:51 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Hi!
Klitgaard wrote:
but how do I scale my image to max, keeping aspect ration and without exceeding the cell limits?
Calculate the width you'd need for a height of 19 cm, then use the smaller width value for the image (either 15 cm or the calculated value).
Sorry, but AFAIK there is no built-in solution that solves this task.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Image fit in table
PostPosted: Wed Jan 09, 2013 8:27 am 
Offline

Joined: Tue Apr 24, 2012 2:28 pm
Posts: 7
Hi Thomas,
Thanks a lot for your fast answer. Here is what I did - which seems to do the trick (assuming pixels are square).
Comments are welcome.
Lars Klitgaard

Code:
Paragraph par = row.Cells[1].AddParagraph();
par.Format.Alignment = ParagraphAlignment.Center;
MigraDoc.DocumentObjectModel.Shapes.Image image = par.AddImage("myImage.png");
           
System.Drawing.Bitmap bmp = new Bitmap(tempImageFilename);
if ((Convert.ToDouble(bmp.Height) / bmp.Width) > 19f / 15f)
    image.Height = "19cm";
else
    image.Width = "15cm";

image.LockAspectRatio = true;


Top
 Profile  
Reply with quote  
 Post subject: Re: Image fit in table
PostPosted: Wed Jan 09, 2013 9:40 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
If pixels are not square, use the code shown here:
viewtopic.php?p=5770#p5770

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 69 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group