[Mono-list] Mono image scaling quality

Daniel Lo Nigro lists at dan.cx
Tue Aug 13 21:17:05 UTC 2013


Thanks David. I dug around in the code and it looks like Cairo (the
graphics library Mono uses) doesn't support anything higher-quality than
bilinear interpolation, which is unfortunate.

I did end up using GraphicsMagick (a fork of ImageMagick) by invoking the
process, which means I didn't have to deal with P/Invoke signatures. This
code worked on both Windows and Linux (Debian with Mono 3.0.10) for me:

/// <summary>
/// Generates a thumbnail for the specified image, using ImageMagick or
GraphicsMagick
/// </summary>
/// <param name="sourceImg">Image to generate thumbnail for</param>
/// <param name="width">Width of the thumbnail</param>
/// <param name="height">Height of the thumbnail</param>
/// <returns>Thumbnail image</returns>
public static Bitmap GenerateMagickThumbnail(this Image sourceImg, int
width, int height)
{
// Create new GraphicsMagick process for thumbnail generation
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "gm",
Arguments = string.Format("convert - -filter sinc -size {0}x{1} -resize
{0}x{1} -", width, height),
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};

process.Start();

// Write source image to input stream of GraphicsMagick
sourceImg.Save(process.StandardInput.BaseStream, ImageFormat.Png);
process.StandardInput.Flush();
process.StandardInput.Close();

try
{
var thumb = new Bitmap(process.StandardOutput.BaseStream);
return thumb;
}
catch (Exception ex)
{
// Reading from stderr seems to block indefinitely if GraphicsMagick never
writes to it
// Only try reading stderr if we don't get a valid image.
var errors = process.StandardError.ReadToEnd();
throw new Exception(string.Format("Error invoking GraphicsMagick:
{0}\nOriginal exception: {1}", errors, ex));
}
finally
{
process.Dispose();
}
}



On Tue, Aug 13, 2013 at 2:00 AM, David Lechner <david at lechnology.com> wrote:

> On 8/10/2013 6:50 AM, Daniel Lo Nigro wrote:
>
>>
>> Any ideas? Should I just defer thumbnail creation to something like
>> ImageMagick/GraphicsMagick or are there some other options I can set in
>> Mono to make it produce higher-quality output?
>>
>>
> From what I read, there is nothing in mono. I ended up using ImageMagick
> to do what I needed on one of my projects. You are welcome to use my code. <
> https://github.com/dlech/**Keebuntu/tree/master/**ImageMagick<https://github.com/dlech/Keebuntu/tree/master/ImageMagick>
> >
>
>
>
> ______________________________**_________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/**mailman/listinfo/mono-list<http://lists.ximian.com/mailman/listinfo/mono-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20130814/09fb5ae2/attachment-0001.html>


More information about the Mono-list mailing list