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

Inserting jpg images using XImage.FromBitmapSource (WPF)
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1118
Page 1 of 1

Author:  elaget [ Sun Mar 28, 2010 1:00 pm ]
Post subject:  Inserting jpg images using XImage.FromBitmapSource (WPF)

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());
        }


Author:  Thomas Hoevel [ Mon Mar 29, 2010 7:59 am ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

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.

Author:  elaget [ Mon Mar 29, 2010 12:10 pm ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

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.

Author:  Thomas Hoevel [ Mon Mar 29, 2010 12:53 pm ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

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.

Author:  elaget [ Mon Mar 29, 2010 4:17 pm ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

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...

Author:  Thomas Hoevel [ Tue Mar 30, 2010 8:14 am ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

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.

Author:  elaget [ Tue Mar 30, 2010 8:17 am ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

I assume you mean compressed and not uncompressed - anyway that explains what I am observing.
Thanks!

Espen

Author:  Thomas Hoevel [ Tue Mar 30, 2010 8:33 am ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

elaget wrote:
I assume you mean compressed and not uncompressed

Correct - I edited my post. :oops:

Author:  grefton [ Mon Nov 16, 2015 11:45 pm ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

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)));

Author:  TH-Soft [ Tue Nov 17, 2015 9:10 am ]
Post subject:  Re: Inserting jpg images using XImage.FromBitmapSource (WPF)

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).

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