image-width = 106
image-height = 106
image-shape = rectangle
image-sizing-strategy = FixedMargin
image-vmargin = 12
image-hmargin = 0
image-bcolor = 00ffffff
image-bd-width = 1
image-bd-color = 0C7CBA
I am using an image with above width and height. After the conversion I got fuzzy images. how can I fix it?
public static byte[] Resize(Image inputImage, ImageResizeOptions options)
{
using (var outputStream = new MemoryStream())
{
var inputImageWidth = inputImage.Width;
var inputImageHeight = inputImage.Height;
float contentHeight, contentWidth, top, left;
var outputHeight = options.OutputHeight != 0 ? options.OutputHeight : inputImageHeight;
var outputWidth = options.OutputWidth != 0 ? options.OutputWidth : inputImageWidth;
var stratergy =
(options.Strategy != ImageSizingStrategy.Auto) ? options.Strategy :
((options.ContentHeight != 0) && (options.ContentWidth != 0)) ? ImageSizingStrategy.FixedSize :
ImageSizingStrategy.FixedMargin;
if (stratergy == ImageSizingStrategy.FixedSize)
{
contentHeight = options.ContentHeight;
contentWidth = options.ContentWidth;
top = (outputHeight - contentHeight) / 2;
left = (outputWidth - contentWidth) / 2;
}
else if (stratergy == ImageSizingStrategy.FixedAspectRatioWithCrop)
{
if (inputImage.Width > inputImage.Height)
{
contentHeight = outputHeight - options.VerticalMargin * 2;
contentWidth = contentHeight * ((float)inputImageWidth / (float)inputImageHeight);
}
else
{
contentWidth = outputWidth - options.HorizontalMargin * 2;
contentHeight = contentWidth * ((float)inputImageHeight / (float)inputImageWidth);
}
top = (outputHeight - contentHeight) / 2;
left = (outputWidth - contentWidth) / 2;
}
else if (stratergy == ImageSizingStrategy.FixedAspectRatio)
{
if (options.ContentHeight != 0)
{
contentHeight = options.ContentHeight;
contentWidth = contentHeight * ((float)inputImageWidth / (float)inputImageHeight);
if (contentWidth > options.ContentWidth)
{
contentWidth = options.ContentWidth;
contentHeight = contentWidth * ((float)inputImageHeight / (float)inputImageWidth);
}
}
else
{
contentHeight = outputHeight - options.VerticalMargin * 2;
contentWidth = contentHeight * ((float)inputImageWidth / (float)inputImageHeight);
if (contentWidth > outputWidth - options.HorizontalMargin * 2)
{
contentWidth = outputWidth - options.HorizontalMargin * 2;
contentHeight = contentWidth * ((float)inputImageHeight / (float)inputImageWidth);
}
}
left = (int)(((float)outputWidth - contentWidth) / 2);
top = (int)(((float)outputHeight - contentHeight) / 2);
}
else
{
contentHeight = outputHeight - options.VerticalMargin * 2;
contentWidth = outputWidth - options.HorizontalMargin * 2;
left = options.HorizontalMargin;
top = options.VerticalMargin;
}
PngOptions imageOptions = new PngOptions();
imageOptions.Source = new Aspose.Imaging.Sources.StreamSource(outputStream);
imageOptions.ColorType = PngColorType.TruecolorWithAlpha;
using (var outputImage = Aspose.Imaging.Image.Create(imageOptions, outputWidth, outputHeight))
{
Graphics g = new Graphics(outputImage);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.FillRectangle(options.BackgroundBrush, 0, 0, outputWidth, outputHeight);
if (options.Shape == ImageShape.Ellipse)
{
using (var imageWithOffset = new PngImage((int)(contentWidth + top), (int)(contentHeight + top)))
{
Graphics imageWithOffsetGraphic = new Graphics(imageWithOffset);
imageWithOffsetGraphic.DrawImage(inputImage, left, top, contentWidth, contentHeight);
var imageBrush = new TextureBrush(imageWithOffset, WrapMode.Clamp);
g.FillEllipse(imageBrush, left, top, contentWidth, contentHeight);
}
}
else
{
g.DrawImage(inputImage, left, top, contentWidth, contentHeight);
}
if (options.BorderWidth != 0)
{
g.DrawRectangle(new Pen(options.BorderBrush, options.BorderWidth), left, top, contentWidth, contentHeight);
}
outputImage.Save();
var bytes = outputStream.ToArray();
return bytes;
}
}
}