peters / xps2img

Convert xps document to a .NET bitmap
GNU Lesser General Public License v3.0
21 stars 12 forks source link

Cannot save images to Jpeg format #6

Open amigobv opened 8 years ago

amigobv commented 8 years ago

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;
}
allen3371 commented 2 years ago

me too