PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Apr 19, 2024 3:29 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Get Images Size
PostPosted: Fri Oct 02, 2015 9:44 am 
Offline

Joined: Sun Dec 07, 2014 1:03 pm
Posts: 18
Hello !

I guess it can not be so diffcult. But I have noc idea how to do it.

I have this code to add an picture centered on my Document:
Code:
            Dim paragraph1 As Paragraph
            paragraph1 = document.LastSection.AddParagraph
            paragraph1.AddImage("C:\Images\bild\" & images)
            paragraph1.Format.Alignment = ParagraphAlignment.Center


The image path comes from a database and the images have different sizes and some are horizontaly some are verticaly. So I want to find out the width and the height of the image to add some lines and to set the right width and height.
I hope youunderstand what I mean.
mgbig


Top
 Profile  
Reply with quote  
 Post subject: Re: Get Images Size
PostPosted: Fri Oct 02, 2015 1:25 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 915
Location: CCAA
Hi!
mgbig wrote:
So I want to find out the width and the height of the image to add some lines and to set the right width and height.
I hope youunderstand what I mean.
Finding the width and height of the image is very simple: just use the image returned from the AddImage() call and query width and height. You can also set width and height - e.g. to maximize the size within the given margins.
See my next post.

You don't have to add lines - you can simply set the absolute position of the image on the page. Knowing the size of the image and the size of the page, it is simple to get it centred.

This post shows how to position a TextFrame, but Image class shares the same properties:
viewtopic.php?p=3133#p3133

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Last edited by TH-Soft on Sat Oct 17, 2015 8:29 am, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Get Images Size
PostPosted: Sat Oct 17, 2015 5:41 am 
Offline

Joined: Sat Oct 17, 2015 5:36 am
Posts: 5
I'm experiencing a problem with this technique; the Width and Height of the image (loaded from a database in my instance using the 'base64:...' technique are always 0 after using AddImage, so my (very simple currently) check for the image width is failing:

Code:
byte[] data;
using (Stream stream = new MemoryStream(evidence.FileData))
{
    if (stream == null)
        throw new ArgumentException("Unable to load evidence file from database");
    int count = (int)stream.Length;
    data = new byte[count];
    stream.Read(data, 0, count);
}
// Insert image
string b64image = "base64:" + Convert.ToBase64String(data);
Image image = para.AddImage(b64image);
image.LockAspectRatio = true;
if (image.Width > new Unit(17,UnitType.Centimeter))
{
    image.Width = "17cm";
}
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Center;
image.WrapFormat.Style = WrapStyle.None;


I experienced this with both the stable code and the beta version. Any thoughts?


Top
 Profile  
Reply with quote  
 Post subject: Re: Get Images Size
PostPosted: Sat Oct 17, 2015 7:49 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 915
Location: CCAA
philmwebb wrote:
I experienced this with both the stable code and the beta version. Any thoughts?
My fault.
MigraDoc reads the image only when rendering the PDF, therefore the size is not known while you create the document.

If you do not know the size of your images, you will have to read it from the images while creating the MigraDoc document.
You can use the XImage class of PDFsharp (or other image libraries) to query the image size:
Code:
int width;
int height;
using (XImage image2 = XImage.FromFile(imageFile))
{
    width = image2.PixelWidth;
    height = image2.PixelHeight;
}


For images with "base64:" you can use FromStream() instead of FromFile() if you are using the current beta version.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: Get Images Size
PostPosted: Wed Dec 02, 2015 10:33 am 
Offline

Joined: Sat Oct 17, 2015 5:36 am
Posts: 5
Sorry for the delay in responding - just getting back onto this project.

Your solution works fine and enables me to do what I need. Thanks.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 223 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