PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Jul 03, 2024 5:17 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Sun Mar 28, 2010 1:00 pm 
Offline

Joined: Sun Mar 28, 2010 12:49 pm
Posts: 4
Hi
I am trying to create an XImage from a jpg that I have as a bytearray. I would like to avoid saving it to file and also there is a bug in the WPF XImage.FromFile method not releasing the file.

Problem is that the image seems not to be treated as a jpg which makes the pdf file huge compared to using the .FromFile method. I am not sure if I am getting the conversion from bytearray to BitmapSource wrong or if there is a bug in pdfsharp not recognizing the jpg properly.

Thanks,
Espen


This is my code:
Code:
XImage image = XImage.FromBitmapSource(BitmapSourceFromBytes(ImageBytes));


   public static BitmapSource BitmapSourceFromBytes(byte[] ImageBytes)
        {
            MemoryStream ms = new MemoryStream(ImageBytes);
            Bitmap sourceImage = (Bitmap)Bitmap.FromStream(ms);
            ms.Dispose();               
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(sourceImage.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        }



Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 29, 2010 7:59 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
For JPEG images, PDFsharp embeds the original JPEG file in the PDF.
Obviously this will only work if there is an original JPEG file available.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 29, 2010 12:10 pm 
Offline

Joined: Sun Mar 28, 2010 12:49 pm
Posts: 4
Hi Thomas
Thanks for you reply.
So what you are saying is that it is not currently possible to embed a JPEG file without first writing it to disk? Or is there something I am missing?
I would love to avoid saving it to disk, especially because of the bug when using XImage.FromFile method that locks the file even after disposing the XImage object.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 29, 2010 12:53 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
elaget wrote:
I would love to avoid saving it to disk, especially because of the bug when using XImage.FromFile method that locks the file even after disposing the XImage object.

You can try the GDI+ build.

Your code might work with the GDI+ build.
If not: the GDI+ build doesn't have the file lock problem.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 29, 2010 4:17 pm 
Offline

Joined: Sun Mar 28, 2010 12:49 pm
Posts: 4
I could of course try the GDI+ version, thanks.
Is it supposed to work doing it the way I am trying to? Or is the conversion to BitmapSource decompressing the picture somehow? If this is supposed to work at all, there must to be a bug somewhere...


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 30, 2010 8:14 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
elaget wrote:
Is it supposed to work doing it the way I am trying to?

When reading a JPEG image with GDI+, GDI+ keeps a reference to the original JPEG data.
Your code might work, but I don't know. I think it's worth a try.

WPF doesn't keep the reference to original JPEG data. Therefore the WPF build needs a filename so we can open the file to read the original JPEG data.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 30, 2010 8:17 am 
Offline

Joined: Sun Mar 28, 2010 12:49 pm
Posts: 4
I assume you mean compressed and not uncompressed - anyway that explains what I am observing.
Thanks!

Espen


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 30, 2010 8:33 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
elaget wrote:
I assume you mean compressed and not uncompressed

Correct - I edited my post. :oops:

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 16, 2015 11:45 pm 
Offline

Joined: Mon Nov 16, 2015 11:41 pm
Posts: 1
Here's what I ended up doing to create the XImage from a byte array. (imgresult.ImageData is the byte[])
Code:
XImage newimg = XImage.FromGdiPlusImage(System.Drawing.Image.FromStream(new MemoryStream(imgresult.ImageData)));


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 17, 2015 9:10 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 951
Location: CCAA
grefton wrote:
Here's what I ended up doing to create the XImage from a byte array. (imgresult.ImageData is the byte[])
Code:
XImage newimg = XImage.FromGdiPlusImage(System.Drawing.Image.FromStream(new MemoryStream(imgresult.ImageData)));
You are using a GDI function - and this (old) thread is about WPF.

XImage.FromStream seems to be a better choice - pass the MemoryStream directly and skip Image.FromStream. That should work on any platform (assuming PDFsharp 1.50 is used).

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


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

All times are UTC


Who is online

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