How to get path of Properties.Resources.Image in .

2020-07-09 02:44发布

问题:

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?

回答1:

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



回答2:

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

var image = XImage.FromGdiPlusImage(Properties.Resources.logo);


回答3:

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



标签: c# .net pdfsharp