How to get path of Properties.Resources.Image in .NET
Asked Answered
C

3

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?

Carbuncle answered 10/1, 2013 at 19:10 Comment(4)
You don't have to save the image (Bitmap) to draw it, see my answer for an example.Axseed
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.Axseed
@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.Colourable
@ThomasH It would have been nice to know what the case really is here then =)Axseed
S
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

Supernational answered 10/1, 2013 at 19:13 Comment(7)
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.Ratchford
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[].Carbuncle
Ohh Its bitmap, there is better API, bitmap.Save()Supernational
I get some error about general exception GDI+ when the Save method is called. is that common?Carbuncle
that indicates some problem in bitmap file. Generally GDI+ throws OutOfMemoryException for plethora of reasons.Supernational
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.Ratchford
@Carbuncle you need to add a filename like "sampleImg.png" to the path. I had the same issue and solved it by doing thisKirov
A
1

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

var image = XImage.FromGdiPlusImage(Properties.Resources.logo);
Axseed answered 10/1, 2013 at 19:43 Comment(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 FromGdiPlusImageCursory
C
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

Colourable answered 26/11, 2015 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.