jpeg:extent in Magick.NET

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
cshark
Posts: 3
Joined: 2017-12-07T05:50:54-07:00
Authentication code: 1152

jpeg:extent in Magick.NET

Post 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!
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: jpeg:extent in Magick.NET

Post by dlemstra »

Whats your input image and how do you write it? And what is the value of Filesize?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
cshark
Posts: 3
Joined: 2017-12-07T05:50:54-07:00
Authentication code: 1152

Re: jpeg:extent in Magick.NET

Post 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);
    }
}
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: jpeg:extent in Magick.NET

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
cshark
Posts: 3
Joined: 2017-12-07T05:50:54-07:00
Authentication code: 1152

Re: jpeg:extent in Magick.NET

Post 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!
Post Reply