Free Support Forum - groupdocs.com

Auto sizing image width and height


#1

Hi,
Just updated to the latest version (19.11.0) of Conversion. Previously if in the conversion options width had a value set and height was null, the aspect ratio of the image would be maintained. Now it looks like if width is set and and height is null, the image becomes misshapen. Is there any way to set only one value and have other change automatically to stay proportional?

Thank you!


#2

@ndollar,

Can you please share following details:

  • Source file that you are trying to convert to image format
  • Sample code you wrote for this particular conversation

#3

Sure, the source file is in the zip and not the zip itself:
2018 Service & Fees.zip (50.8 KB)

The relevant code:

public List<Stream> RenderThumbnails(Stream document, string documentName, int? height = null, int? width = null, string type = "png", uint fromIndex = 1, uint pageSize = 1)
{
    Guard.ArgumentNotNull(document, "document");
    var thumbnailOptions = GetThumbnailOptions(fromIndex, pageSize, height, width, type);

    var result = new List<Stream>();

    try
    {
        SavePageStream convertStream = page =>
        {
            result.Add(new MemoryStream());
            return result[page - 1];
        };

        var conversionThread =
            new Thread(() =>
            {
                using (var thumbnailConverter = new Converter(() => getStreamFromDoc(document)))
                {
                    try
                    {
                        thumbnailConverter.Convert(convertStream, thumbnailOptions);
                    }
                    catch (Exception e)
                    {
                        ExceptionHandler(documentName, e);
                        result = null;
                    }
                }
            });

        conversionThread.Start();
        conversionThread.Join(300000);
        if (conversionThread.IsAlive)
            conversionThread.Abort();

        return result;
    }

    catch (Exception ex)
    {
        //if an conversion exception is encountered, 
        //return null so that we can simply present the document to the user instead of an exception or error
        ExceptionHandler(documentName, ex);
        return null;
    }
}

private static ImageConvertOptions GetThumbnailOptions(uint fromIndex, uint pageSize, int? height, int? width, string type)
{
    var options = new ImageConvertOptions()
    {
        PagesCount = (int)pageSize,
        PageNumber = (int)fromIndex
    };

    if (height != null && height > 0)
        options.HorizontalResolution = (int)height;
    if (width != null && width > 0)
        options.VerticalResolution = (int)width;

    if (type.ToLower() == "jpg")
        options.Format = ImageFileType.Jpg;
    else if (type.ToLower() == "bmp")
        options.Format = ImageFileType.Bmp;
    else
        options.Format = ImageFileType.Png;

    return options;
}

Thanks!


#4

@ndollar,

Thanks for the details. We’re now investigating this scenario.


#5

@ndollar,

Can you please also tell the API version that you were using previously?


#6

Yep, it was 18.9.0.


#7

@ndollar,

We can see the difference between output of 18.9 and 19.11. In both cases we set width to 500.
18.9.Jpg (103.3 KB) - output
19.11.jpg (280.5 KB) - output
Output generated from 18.9 is what you’re expecting in 19.11?


#8

Hmmm not quite - we’re getting this:
old_version.png (106.0 KB)


#9

Aha, I’m very sorry I left out important information. In our old version we used viewer (18.9.0) for this function:

public async Task<List<Stream>> RenderThumbnails(Stream document, string documentName, int? height = null, int? width = null, string type = "png", uint fromIndex = 1, uint pageSize = 1)
        {
            Guard.ArgumentNotNull(document, "document");

            var config = GetViewerConfig();
            var imgHandler = new ViewerImageHandler(config);
            var thumbnailOptions = GetThumbnailOptions(fromIndex, pageSize, height, width, type);

            try
            {
                var memoryStream = new MemoryStream();
                await document.CopyToAsync(memoryStream); //Stream type given by Akkadia or Dataroom is not seekable so need to copy into a memory stream

                var extension = HelperMethods.GetFileExtension(documentName);

                List<PageImage> result = null;

                var conversionThread =
                    new Thread(() =>
                    {
                        try
                        {
                            result = imgHandler.GetPages(memoryStream, extension, thumbnailOptions);
                        }
                        catch (Exception e)
                        {
                            ExceptionHandler(documentName, e);
                            result = null;
                        }
                    });

                conversionThread.Start();
                conversionThread.Join(300000);
                if (conversionThread.IsAlive)
                    conversionThread.Abort();
                
                return result == null ? null : result.Select(thumb => thumb.Stream).ToList();
            }

            catch (Exception ex)
            {
                //if an conversion exception is encountered, 
                //return null so that we can simply present the document to the user instead of an exception or error
                ExceptionHandler(documentName, ex);
                return null;
            }
        }

private static ImageOptions GetThumbnailOptions(uint fromIndex, uint pageSize, int? height, int? width, string type)
        {
            var options = new ImageOptions()
            {
                CountPagesToRender = (int) pageSize,
                PageNumber = (int) fromIndex
            };

            if (height != null && height > 0)
                options.Height = (int) height;
            if (width != null && width > 0)
                options.Width = (int) width;

            if (type.ToLower() == "jpg")
                options.ConvertImageFileType = ConvertImageFileType.JPG;
            if (type.ToLower() == "bmp")
                options.ConvertImageFileType = ConvertImageFileType.BMP;

            return options;
        }

Viewer wasn’t an option for us when we upgraded though because of the formats we needed to move from and to, and we want to use only streams without ever saving the files. So we switched to using Conversion for most everything. Sorry again about the bad info to start with.


#10

But I guess we’d still be looking for the same handling when it comes to images. Always the same width/height ratio except when both are specified.


#11

@ndollar,

If you are looking for only document conversion module, please use GroupDocs.Conversion for .NET API and the latest version (19.11). You can set both width and height (because API allows you to set) and then let us know if there is any issue.


#12

Currently we are using GroupDocs.Conversion for .NET and the latest version - 19.11. Previously we were using GroupDocs.Viewer for .NET - 18.9.

Viewer 18.9 keeps the correct ratio for images and Conversion 19.11 does not.


#13

@ndollar,

In GroupDocs.Conversion for .NET, you have to set height and width in order to get desired output. However, GroupDocs.Viewer for .NET has a built-in feature to adjust height if width only is given and vice versa.
We can investigate it and see if this behavor can be implemented in our document conversion API. Your investigation ticket ID is CONVERSIONNET-3505.


#14

That would be great, thank you!


#15

@ndollar,

You are welcome.