PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Resize image in MigraDoc
https://forum.pdfsharp.net/viewtopic.php?f=2&t=947
Page 1 of 1

Author:  VibhoreBenjamin [ Mon Nov 23, 2009 10:40 am ]
Post subject:  Resize image in MigraDoc

Hi,

I am adding an image in Document using MigraDoc.
The dimension of the image is 771 X 1470.

1) How can i scale the image to a predefined size?

I am writing ...

MigraDoc.DocumentObjectModel.Shapes.Image img = ReceiptsPara.AddImage("D:\\abc.jpg");
img.LockAspectRatio = false;
img.Width = new Unit(300, UnitType.Point); ;
img.Height = new Unit(300, UnitType.Point);
ReceiptsPara.AddLineBreak();

but it causes the application to hang.

2) If i add an image 2 times the application hangs

ReceiptsPara.AddImage("D:\\abc.jpg");
ReceiptsPara.AddImage("D:\\xyz.jpg");
ReceiptsPara.AddImage("D:\\abc.jpg");

Is there any thing wrong i am doing? Or am i missing anything?
Hope to hear soon.

Thanks in advance

Author:  mikesowerbutts [ Mon Nov 23, 2009 10:46 am ]
Post subject:  Re: Resize image in MigraDoc

Hi,

when I have resized images - i used the XImage.ScaleHeight/ScaleWidth property and I didnt encounter any difficulties? I was using vector graphics though.

Mike

Author:  VibhoreBenjamin [ Mon Nov 23, 2009 11:40 am ]
Post subject:  Re: Resize image in MigraDoc

Hi Mike,

Excuse me if i am wrong, i am new to MigraDoc.
XImage is part of PdfSharp and i am using only MigraDoc.
How can i use XImage in MigraDoc.

My problem is I have image files to be added in Pdf document. And while adding image, i want the images to get resized to best fit a specified dimension say for e.g. 500 X 500.

Author:  Thomas Hoevel [ Mon Nov 23, 2009 12:19 pm ]
Post subject:  Re: Resize image in MigraDoc

Hi!
VibhoreBenjamin wrote:
And while adding image, i want the images to get resized to best fit a specified dimension say for e.g. 500 X 500.

This can be done using the Image object of MigraDoc.

If you don't need the power of MigraDoc, then use PDFsharp only and work with XImage objects.

If certain images cause the application to hang, then please provide us with these images and also specify whether you are using the WPF or the GDI+ build.

Author:  VibhoreBenjamin [ Mon Nov 23, 2009 12:37 pm ]
Post subject:  Re: Resize image in MigraDoc

Hi,

I am using MigraDoc GDI+ build.

I want the image to best fit (maintaining the aspect ratio) in 500 X 300 rectangle. To achieve this i am doing

MigraDoc.DocumentObjectModel.Shapes.Image img = ReceiptsPara.AddImage("C:\\abc.jpg");
img.LockAspectRatio = true;
img.Width = Unit.FromPoint(500);
img.Height = Unit.FromPoint(300);

The image gets fit into the specified dimension, but the ratio gets distorted. My requirement is that the image's aspect ratio should remain as it is and still should best fit in the specified dimension.

Author:  Thomas Hoevel [ Mon Nov 23, 2009 5:09 pm ]
Post subject:  Re: Resize image in MigraDoc

It's not a bug, it's a feature:
Quote:
Image.LockAspectRatio Property:
Gets or sets whether the AspectRatio of the image is kept unchanged. If both Width and Height are set, this property is ignored.


Calculate the height for a width of 500, and calculate the width for a height of 300, and then use the smaller rectangle to draw the image.

Author:  mikesowerbutts [ Tue Nov 24, 2009 3:01 pm ]
Post subject:  Re: Resize image in MigraDoc

XImage was a type in my earlier post - the same ScaleHeight/Width functions can be used on MigraDoc Image object.

Also, surely locking the aspect ratio, then setting just one of the properties i.e. width would do the trick?

Mike

Author:  Thomas Hoevel [ Tue Nov 24, 2009 3:56 pm ]
Post subject:  Re: Resize image in MigraDoc

mikesowerbutts wrote:
Also, surely locking the aspect ratio, then setting just one of the properties i.e. width would do the trick?

Yes.
But you'll lose if you e. g. have landscape and portrait photos and a square to show them in ...

It would make sense to allow LockAspectRatio if both Width and Height are set ...
Maybe I'll put that on our wish list.

Author:  olibara [ Thu Nov 11, 2010 10:10 am ]
Post subject:  Re: Resize image in MigraDoc

Quote:
If you don't need the power of MigraDoc, then use PDFsharp only and work with XImage objects.


Hello
I do not see any ScaleHeight/Width Method on the XImage class
For now I'm doing it manualy whith a

Code:
DrawImage(Image,DstRect,SrcRect,GraphicsUnit);


The problem to avoid to get too big images in the document is that I have to do it twice
Once to Hard Resize the image to a new one
Then to effectively drraw the resized image on the Document

Is there any better way ?

Thanks for any help

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/