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

How to add dynamic images?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1398
Page 1 of 1

Author:  crypto_rsa [ Mon Nov 01, 2010 2:30 pm ]
Post subject:  How to add dynamic images?

Hello,

I am using your library to create PDF and RTF documents. Sometimes I need to insert an image which is not a file (it is dynamically created at runtime). What is the preferred method of adding it? Since your library only allows adding an image from a file, I always need to save the dynamic image to a temporary file, then add it and later delete this file. It works fine but sometimes I get an exception when trying to delete the temporary files. I do it right after the call to RtfDocumentRenderer.Render() finishes, but some of the files are still in use. When it is safe to delete them? Or is there a cleaner way to add such images?

Author:  jeffhare [ Tue Dec 14, 2010 11:22 pm ]
Post subject:  Re: How to add dynamic images?

I found it to be a very simple change to enhance the image class to have an AddImage overload that took a stream and a name instead of a filepath.

Essentially:
Code:
// Add a new Image constructor that takes a MemoryStream.
public Image(MemoryStream imageStream) : this()
{
    ImageSteam = imageStream;   // Added new property to hold the stream
   FileName = String.Empty;         // When no filename, we'll look for the stream.
}

// for convenience, added Image class property to indicate whether this image is file or stream
public bool StreamBased
{
    get { return ImageStream != null; }
}

// Altered Image.GetFilePath to return string.empty if the image is StreamBased.
// ---------------------------------------------------------------------

///Another file I needed to make changes to was in the ImageRenderer class  (ImageRenderer.cs)

1)  internal override void Format(Area area, FormatInfo previousFormatInfo)   needed to be altered slightly
     only perform the "does a bit of searching to try to locate the image" behavior when we're a file based image

2) in the Render() method, we needed to add a conditional for when image is StreamBased.
      if (image.StreamBased)
         xImage = XImage.FromStream(image.ImageStream)     <<-- XImage already knows how to load from stream so call it!
      else
          xImage = XImage.FromFile(formatInfo.ImagePath)

3) Exact same thing in the CalculateImageDimensions() method.



While a set of diffs would be the *best* way to convey this change info, this should get you on your way. It's really simple.

Cheers,
-Jeff

Author:  crypto_rsa [ Wed Dec 15, 2010 7:21 am ]
Post subject:  Re: How to add dynamic images?

Thanks for the tip! I'll look into it. It would be worth suggesting the PDFsharp team to merge this code into the official codebase :wink:

Author:  TimJohnston [ Wed Aug 22, 2012 5:35 pm ]
Post subject:  Re: How to add dynamic images?

@jeffhare - any chance you could provide the full code for your changes that allow streams/memory based images please?

Thanks, TJ.

Author:  dheijl [ Thu Aug 23, 2012 6:32 am ]
Post subject:  Re: How to add dynamic images?

This is how I do it (GDI version), no changes to pdfsharp required:

Code:
            // byte[] jpegdata contains the jpeg image
            using (MemoryStream ms = new MemoryStream(jpegdata)) {
                using (Image img = Image.FromStream(ms)) {
                    using (var xi = XImage.FromGdiPlusImage(img)) {
                        _pdfGfx.DrawImage(xi, ip.dx, ip.dy, ip.dw, ip.dh);
                    }
                }
            }

Danny

Author:  Thomas Hoevel [ Thu Aug 23, 2012 8:42 am ]
Post subject:  Re: How to add dynamic images?

Hi!
dheijl wrote:
This is how I do it (GDI version), no changes to pdfsharp required
Your solution works with PDFsharp.
This thread is about MigraDoc. Jeff's solution works with MigraDoc unless you have to convert the document to MDDDL (and that's why the official codebase still does not support dynamic images with MigraDoc).

Author:  TimJohnston [ Thu Aug 23, 2012 11:12 am ]
Post subject:  Re: How to add dynamic images?

Thanks Danny / Thomas, the solution may be straight forward and simple for some, but not for me. If anyone can provide the changes necessary to make this work, I'd be very grateful.

TJ

Author:  Thomas Hoevel [ Thu Aug 23, 2012 11:38 am ]
Post subject:  Re: How to add dynamic images?

Jeff's post shows a new Image c'tor you have to add to Image.cs.
It also shows the changes that have to be done in ImageRenderer.cs.
Finally you have to add a new AddImage in Paragraph.cs that takes a stream (also requires a change in ParagraphElements.cs). Also AddImage to other classes where you need it (e.g. Section).

Author:  wasers [ Fri Jan 25, 2013 7:23 am ]
Post subject:  Re: How to add dynamic images?

Hi,

one question....the suggested post with a new c'tor is from Dec 14, 2010 !
What is the reason that it's still not implemented in version 1.32?

Author:  Thomas Hoevel [ Mon Jan 28, 2013 9:29 am ]
Post subject:  Re: How to add dynamic images?

wasers wrote:
one question....the suggested post with a new c'tor is from Dec 14, 2010!
What is the reason that it's still not implemented in version 1.32?
As mentioned above, this does not work with persistence in MDDDL files.
A good solution requires more than just this c’tor.

Author:  stormcrow79 [ Wed Nov 06, 2013 8:24 am ]
Post subject:  Re: How to add dynamic images?

Hi Thomas,

We have just started looking at using MigraDoc and PDFSharp in our (commercial, closed-source) product. We rapidly came across the need to load images from Stream and encountered the problems with MDDDL, because we need to use DocumentPreview.

I've implemented support for a byte array containing the image data, following the syntax suggested in ParseImage(). This works well for the limited scenarios I have tested, and is certainly sufficient for our needs - documents of only a few pages, with images used for header and footer only.

Do you accept patch submissions?

Author:  Thomas Hoevel [ Wed Nov 06, 2013 9:31 am ]
Post subject:  Re: How to add dynamic images?

stormcrow79 wrote:
Do you accept patch submissions?
I am not the Designer of MigraDoc.
If you submit the changed files we'll review them, test them, and maybe include the changes.

Author:  DaveR2 [ Tue Jul 29, 2014 5:44 pm ]
Post subject:  Re: How to add dynamic images?

I too am struggling with this. XImage doesn't have a FromStream method. The full code would really help.

Author:  munchj [ Mon Aug 04, 2014 9:41 am ]
Post subject:  Re: How to add dynamic images?

I am also struggling with this exact same issue and I am having a hard time opening the solution file in VS2010 to make the changes. For some reason all the projects immediately get unloaded.


Regards,
Julien

Author:  cyberpass [ Thu Apr 23, 2015 8:58 pm ]
Post subject:  Re: How to add dynamic images?

So a couple things with the solution provided above...

1. XImage doesn't have a FromStream method...atleast not in 1.32. Ok...So No Problem can do an XImage.FromGdiPlusImage(Image.FromStream(ms))

2. If the the Migra Doc Image object holds a reference to the stream, what happens when, externally someone disposes of that stream... I would suggest do a copy of the stream(resource intensive), or to make sure the user does not close the stream till after the pdf is rendered..


Thoughts, Ideas?

Author:  TH-Soft [ Tue Sep 22, 2015 7:55 am ]
Post subject:  Re: How to add dynamic images?

Hi!
crypto_rsa wrote:
Sometimes I need to insert an image which is not a file (it is dynamically created at runtime). What is the preferred method of adding it? Since your library only allows adding an image from a file, I always need to save the dynamic image to a temporary file, then add it and later delete this file.
With version 1.50 beta 2, there is a solution for this problem: pass the image as a string when you call AddImage():
http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
This method applies to MigraDoc only.
With PDFsharp you can create an XImage from a Stream.

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