Search code examples
c#imageimage-processingpngaspose.words

Text on image looks blurry after scaling image down using Aspose.Words.Saving.ImageSaveOptions


We have a problem in our system. We extract images from doc file and perform scaling up and down if the size of the extracted image is not as we want. Image looks good after scaling but the text that lies inside the image looks a little bit blurry or not in the same quality (sharpness) as in the image before scaling. Any ideas that can help to improve the text sharpness will be most appreciated.Thank you wonderful people!

Our scaling function looks like this:

const float highScalingFactor = 0.58F; // default scaling of large images - based on experience

const float lowScalingFactor = 0.44F; // default scaling of small images - based on experience

const int maxWidth = 1100;
const int maxHeight = 2500;

float formulaScale = 0.9F;

//calling scaling function here
var lImgStream = ScaleImage(shape, lowScalingFactor * formulaScale, maxWidth, maxHeight, "png");

private static MemoryStream ScaleImage(Shape shape, float scale, int maxWidth, int maxHeight, string extension)
{
    var resolution = 300F;

    ShapeRenderer r = new ShapeRenderer(shape);
    var rect = r.GetBoundsInPixels(scale, resolution);

    if (rect.Width > maxWidth)
    {
        scale = 1 - (rect.Width * scale - maxWidth) / rect.Width;
    }

    if (rect.Height > maxHeight)
    {
        scale = 1 - (rect.Height * scale - maxHeight) / rect.Height;
    }

    ImageSaveOptions imageOptions = new ImageSaveOptions(FileFormatUtil.ExtensionToSaveFormat(extension))
    {
        SaveFormat = SaveFormat.Png,
        Scale = scale,
        Resolution = resolution,
        UseHighQualityRendering = true
    };

    // sharpen formulas
    if (shape.ImageData.ImageType == ImageType.Wmf)
        imageOptions.ImageContrast = 0.55f;

    var output = new MemoryStream();
    r.Save(output, imageOptions);
    output.Seek(0, SeekOrigin.Begin);
    return output;
} 

We tried the above scaling algorithm with successful output in terms of image sharpness except text on the image. The text looks blurry.


Solution

  • Try using SaveOptions.UseAntiAliasing to improve the quality of a rendered image.