PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 9:30 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jan 07, 2010 7:10 pm 
Offline

Joined: Tue Dec 22, 2009 5:42 pm
Posts: 6
Is it possible to create an XImage from an existing image?
I can't save the image anywhere on the disk. So I need to read the image (which is an embedded resource) in a stream and create the XImage from it?

Thanks,
- Arch


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 11, 2010 9:05 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Archvile wrote:
Is it possible to create an XImage from an existing image?

You can use XImage.FromFile or XImage.FromGdiPlusImage.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 27, 2010 4:00 pm 
Offline

Joined: Wed Jan 27, 2010 3:50 pm
Posts: 1
It's possible but a little bit tricky

Code:
         
XGraphics gfx = XGraphics.FromPdfPage(p.Pages[0]);

Assembly a = Assembly.GetAssembly( typeof( Form1 ) );
Stream imgStream = a.GetManifestResourceStream( "Assembly.Resources.floating.png" );
XImage img = XImage.FromGdiPlusImage(Image.FromStream(imgStream));
const double dx = 250, dy = 140;
double width = img.PixelWidth * 72 / img.HorizontalResolution;
double height = img.PixelHeight * 72 / img.HorizontalResolution; 
gfx.DrawImage(img, (dx - width) / 2, (dy - height) / 2, width, height);


First get the page where you want to place an embedded image.

Code:
XGraphics gfx = XGraphics.FromPdfPage(p.Pages[0]);


Next get the filestream from your assembly. You have to replace the resource string in GetManifestResourceStream. And check if all your images are really embedded resources. Check this link

http://www.attilan.com/2006/08/accessin ... rces_u.php

Code:
Assembly a = Assembly.GetAssembly( typeof( Form1 ) );
Stream imgStream = a.GetManifestResourceStream( "Aufnahme.Resources.floating.png" );


And last but not least, create a XImage. In WPF you could use FromBitmapSource. As i'm not using WPF you should try by yourself.

Code:
XImage img = XImage.FromGdiPlusImage(Image.FromStream(imgStream));


Now just choose the right dimensions for your image an place it in your document.

Code:
const double dx = 250, dy = 140;
double width = img.PixelWidth * 72 / img.HorizontalResolution;
double height = img.PixelHeight * 72 / img.HorizontalResolution; 
gfx.DrawImage(img, (dx - width) / 2, (dy - height) / 2, width, height);


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 393 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