Git Product home page Git Product logo

xps2img's Introduction

Convert xps documents to a .NET bitmap. Supports both single and multi-paged xps documents.

This code is mostly derived from xps2img@sourceforge by Ivan Ivon.

Installing via Nuget

Install-Package xps2img

Supported frameworks

  1. .NET Framework 4.0
  2. .NET Framework 4.5
  3. .NET Framework 4.5.1
  4. .NET Framework 4.6

Usage

Additional examples

Convert a xps document to png from disk

using (var xpsConverter = new Xps2Image("multipage.xps"))
{
	var images = xpsConverter.ToBitmap(new Parameters
	{
		ImageType = ImageType.Png,
		Dpi = 300
	});
}

Convert a xps document to png from a byte array

using (var xpsConverter = new Xps2Image(File.ReadAllBytes("multipage.xps")))
{
	var images = xpsConverter.ToBitmap(new Parameters
	{
		ImageType = ImageType.Png,
		Dpi = 300
	});
}

Convert a xps document to png from stream

using (var xpsConverter = new Xps2Image(new MemoryStream(File.ReadAllBytes("multipage.xps"))))
{
	var images = xpsConverter.ToBitmap(new Parameters
	{
		ImageType = ImageType.Png,
		Dpi = 300
	});
}

License

GNU LESSER GENERAL PUBLIC LICENSE Version 3

xps2img's People

Contributors

peters avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xps2img's Issues

Remove OutOfMemoryException retries for RenderPageToBitmap.

https://github.com/peters/xps2img/blob/master/src/xps2img/Xps2Image.cs

private RenderTargetBitmap RenderPageToBitmap(DocumentPage page, RenderTargetBitmap bitmap)

For a library it makes no sense to keep trying on OutOfMemoryException (makes sense for separate console app, see also UI code at http://xps2img.sf.net.)

I'd suggest to reduce method as follows or even inline it.

bitmap.Render(page.Visual);

                // Memory leak fix.
                // http://social.msdn.microsoft.com/Forums/en/wpf/thread/c6511918-17f6-42be-ac4c-459eeac676fd
                ((FixedPage)page.Visual).UpdateLayout();

                _conversionStarted = true;

                return bitmap;

Cannot save images to Jpeg format

Every time I try to save a image in a jpeg format, I receive a "A generic error occurred in GDI+" Exception. The path of the output file is correct and the application has full permission on the folder where the file should be saved. Below the code that I used. The same code works great if I convert it to png:

public static bool XpsToImage(string inputFile, int page, int dpi)
{
  var pages = new List<Bitmap>();
  using (var xpsConverter = new Xps2Image(new MemoryStream(File.ReadAllBytes(inputFile))))
  {
    var image = xpsConverter.ToBitmap(new Parameters
    {
      Dpi = dpi,
      ImageType = ImageType.Jpeg,
   });

   if (image == null)
     return false;

   pages.AddRange(image);
 }

var outputfile = Path.GetDirectoryName(inputFile) + Path.DirectorySeparatorChar +
                        Path.GetFileNameWithoutExtension(inputFile) + "_preview.Jpeg";
try
{
   if (page < pages.Count)
     pages[page].Save(outputfile, ImageFormat.Jpeg);
}
catch (Exception ex)
{
  LOGGER.Error("Cannot save image", ex);
  return false;
}

return true;
}

Stopped working on Azure?

As title say I think this has stopped working in Azure Web Apps since somewhere around 2017-11-10?

Can anyone confirm? It did work before now I just get a transparent/blank image with png.

Update howto use

might want to include these lines so it's easier for someone to save each image

            var pages = new List<System.Drawing.Bitmap>();
            pages.AddRange(images);

XPS to PDF

Hello Peter,

Very nice library. Wonderful work though

Do you have library to convert XPS to PDF ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.