Page 1 of 1

jpeg:extent in Magick.NET

Posted: 2017-12-07T05:57:57-07:00
by cshark
Hi everyone, I want to set a maximum file size for an image if a user enters a value. My current attempt looks as following:

Code: Select all

private MagickImage Image(MagickImage image, int resolution)
{
    //some code here
    if (Filesize > 0)
    {
        image.Settings.SetDefine(MagickFormat.Jpeg, "extent", Filesize.ToString() + "KB");
        image.Rotate(22.5); //test
    }
    //more code here
    return image;
}
The rotate test part works and the image gets correctly rotated by 22.5°, but the extent does not work and the image still gets downloaded with the original file size. Any help would be appreciated!

Re: jpeg:extent in Magick.NET

Posted: 2017-12-07T12:58:07-07:00
by dlemstra
Whats your input image and how do you write it? And what is the value of Filesize?

Re: jpeg:extent in Magick.NET

Posted: 2017-12-08T00:39:23-07:00
by cshark
The input image is any image a user selects from a website. I tested this with a jpg image (I could pm you this because copyright), but theoretically it should work with any image.

Filesize reads the user's input from a textbox in a menu as an int, so this can be any numeric value. I tested it with 1000, 100, 10, 1.

The image(s) get(s) written in another method as following:

Code: Select all

var list = new List<MagickImage>();
list = download.GetImages(target);

foreach(MagickImage image in list) 
{
    foreach(string name in names)
    {
        var path = System.IO.Path.Combine(string + string + string);
        image.Write(path);
    }
}

Re: jpeg:extent in Magick.NET

Posted: 2017-12-08T05:24:57-07:00
by dlemstra
I have a very simple unit test that makes sure that `jpeg:extent` works. The following code creates a proper image:

Code: Select all

JpegWriteDefines defines = new JpegWriteDefines()
{
    Extent = 10, // 10KB
};

using (IMagickImage image = new MagickImage(Files.Builtin.Logo))
{
    using (MemoryStream memStream = new MemoryStream())
    {     
        // This does the same as what you do but in a different notation.
        image.Settings.SetDefines(defines);
        image.Write(memStream);
        Assert.IsTrue(memStream.Length < 10000);
    }
}
I am guessing something is happening inside the `//more code here` part.

Re: jpeg:extent in Magick.NET

Posted: 2017-12-08T12:17:08-07:00
by cshark
The "//code here" and "//more code here" parts are just changing the image's width and height, and resolution. I simply excluded those parts to reduce clutter since they're irrelevant.

However, loading the settings first and then using them like you did did the trick. At a later point I also forced conversion to jpeg if Filesize is greater than zero so all selected images get converted to jpeg and resized correctly.

Code: Select all

if (Filesize > 0)
{
    JpegWriteDefines setting = new JpegWriteDefines()
    {
        Extent = Filesize;
    }                                 
    image.Settings.SetDefines(setting);
}
Thanks a bunch!