11

I included an image as a resource following this post: How to create and use resources in .NET

I am using PDFSharp library to create a PDF. The method to draw an image, requires the path of the image. How do I get the path of Properties.Resources.Image?

Or is there another way to do this?

Community
  • 1
  • 1
Crystal
  • 28,460
  • 62
  • 219
  • 393
  • You don't have to save the image (Bitmap) to draw it, see my answer for an example. – Mario S Jan 10 '13 at 19:51
  • 1
    I can't see how the accepted answer, with saving image to disk, could be the "better" option. It's more code involved, meaning more to maintain and affects readability. Also the images would be duplicated on disk (the resource is already taking disk space) and take more disk space, having to do more code for deleting temporary images and making it more error prone. All you really needed was to change one method call to avoid all this. – Mario S Jan 10 '13 at 22:28
  • @Mario: Some folks write PDFsharp when they actually use MigraDoc (which uses PDFsharp). For MigraDoc you need a filename, so saving to a file is OK. For PDFsharp, using the image is better so saving to a file is not necessary. Since the question has no MigraDoc tag, I presume your answer is better. – I liked the old Stack Overflow Jan 11 '13 at 08:23
  • @ThomasH It would have been nice to know what the case really is here then =) – Mario S Jan 11 '13 at 08:40

3 Answers3

10

The Properties.Resources.Image is in-memory resource.

You can save Image to temp file and the get the path.

var path = Path.GetTempPath();
Properties.Resources.logo.Save(path);

Above uses Bitmap.Save

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • If it's an image, you should really use File.WriteAllBytes instead of WriteAllText. You shouldn't treat binary data as a string; weird things can happen. – Rob Jan 10 '13 at 19:15
  • When I try `File.WriteAllBytes(path, Properties.Resources.logo;);` I get invalid expression term. If I do `File.WriteAllBytes(path, Properties.Resources.logo);`, I get the errors: 1) The best overloaded method match for System.IO.File.WriteAllBytes has invalid arguments. 2) Argument 2: cannot convert from System.Drawing.Bitmap to byte[]. – Crystal Jan 10 '13 at 19:20
  • Ohh Its bitmap, there is better API, `bitmap.Save()` – Tilak Jan 10 '13 at 19:21
  • I get some error about general exception GDI+ when the Save method is called. is that common? – Crystal Jan 10 '13 at 19:39
  • that indicates some problem in bitmap file. Generally GDI+ throws `OutOfMemoryException` for plethora of reasons. – Tilak Jan 10 '13 at 19:40
  • You can configure your resources file to store the data as a binary blob (a byte[]) instead of as a GDI+ Bitmap object, in which case, the OP's plan of saving using File.WriteAllBytes would work well for you. – Rob Jan 10 '13 at 19:47
  • @Crystal you need to add a filename like "sampleImg.png" to the path. I had the same issue and solved it by doing this – smoothumut Dec 08 '14 at 13:22
1

You can actually create an image, without saving it, using XImage.FromGdiPlusImage():

var image = XImage.FromGdiPlusImage(Properties.Resources.logo);
Mario S
  • 11,715
  • 24
  • 39
  • 47
  • 1
    hmmm ... I'm getting a message "'XImage' does not contain a definition for 'FromGdiPlusImage'" .... in XImage.cs I can see a commented out static method FromGdiPlusImage – whytheq Jan 22 '19 at 11:14
0

As of PDFsharp/MigraDoc 1.50 beta 2 and newer you no longer need a path when using MigraDoc. It was already mentioned that PDFsharp does not need a filename, as images can be read from e.g. streams.

MigraDoc still requires a string. You encode the image data as a string (BASE64 format) and pass that string as a filename.

See also:
http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx