This project has moved and is read-only. For the latest updates, please go here.

Resize(int width, int height) and Grayscale effect

Jul 7, 2014 at 5:07 AM
Edited Jul 7, 2014 at 5:09 AM
Good day,

I've been trying to convert my software from using imagemagick with command lines to magick.net dll.

So far most of them are fine but I do have issue with 2 functions:
  • Resize(int width, int height)
using (MagickImage image = new MagickImage(pathToPhotoInString))
{
    image.Resize(2100, 1400);
    return image.ToBitmap();
}
This resize method would cause my generated image to be all black when I call bitmap.Save(filePath) method, dimension is correct though.
  • Grayscale effect
I do have a grayscale function in my software which I used to call in command line with below
convert source.jpg -grayscale Rec709Luminance destination.jpg
I've looked into functions provided but I couldn't fine any similar function which I can generate the same effect, could you please guide me on this?

Thank you very much.
Jul 7, 2014 at 5:45 AM
I cannot reproduce your problem with the Resize method. Are you using the latest version of Magick.NET? Does the following work for you:
using (MagickImage image = new MagickImage("logo:"))
{
  image.Resize(2100, 1400);
  image.ToBitmap().Save(@"C:\test.jpg");
}
The grayscale method is not yet available in Magick.NET. It will be added the next release.
Jul 7, 2014 at 11:56 AM
Edited Jul 8, 2014 at 2:34 AM
Thanks for the information.

I believe it might be caused by certain image type? would you please have a resize method test on this image?

[Removed Link]

Also if you're kind enough to answer, may I know how this wrapper works in your library? does it compile all the operations and make a single call to imagemagick using command line? or are all the operations done in .Net without needing to call the command line at all?

Reason I'm asking this is because jpg gets compressed every time we do a save, I could combine most functions in single command line call but I couldn't combine together distort, grayscale, resize, crop and rotate, thus my image gets compressed a few times. So understanding how this library underlying works is pretty important to me.
Jul 7, 2014 at 1:33 PM
Edited Jul 7, 2014 at 2:09 PM
Can you post your image on something like dropbox? Imgur might modify the image. I will explain how the library works later.
Jul 7, 2014 at 2:29 PM
Edited Jul 8, 2014 at 2:34 AM
Thanks a lot.. Here's the image, i've placed the original file on server.. [Removed Link]
Jul 7, 2014 at 7:01 PM
I cannot reproduce the problem with your image. Can you try saving the image before you 'return' it? Maybe you are doing something in between that is causing this behavior?
using (MagickImage image = new MagickImage(pathToPhotoInString))
{
  image.Resize(2100, 1400);
  Bitmap bitmap = image.ToBitmap();
  bitmap.Save(@"C:\test.jpg"):
  return bitmap;
}
The Magick.NET library directly calls the ImageMagick methods. It does not wrap the command line. The image will only be compressed when you call image.Write() or save the Bitmap you created with ToBitmap().
Jul 8, 2014 at 2:22 AM
Well it might be my version problem then, I'll see what's wrong or at the end I can still replace this resize method with C# natively provided function. So it's not a really big issue here.. Glad to hear grayscale is coming up soon..

Thanks for all the help and information.. This library you made is a life saver =D
Jul 8, 2014 at 1:24 PM
Edited Jul 8, 2014 at 1:24 PM
Is the file 'C:\test.jpg' all black? And can you try disabling OpenCL?
MagickNET.UseOpenCL = false;
Jul 9, 2014 at 2:57 AM
Edited Jul 9, 2014 at 2:58 AM
Uh.. it worked! =D

Er..I don't really get what documentation says, its some kind of accelerator? Is latency the only downside after disabling this?

Latency is not a problem as I'd display loading progress during the process..

Just in case you'd like to know, previously when I generate a whole black photo, I can somehow view the thumbnail in Windows Explorer correctly, but it turns out all black when I double click to open it, not sure if this is just a Windows' bug.
  • ps: my snippet is not what I've been using, returning that bitmap in using clause is in fact a runtime error.
  • I'm using x86 version of Magick.Net library, but installed 64-bit version of ImageMagick, with folder named "ImageMagick-6.8.9-Q16"
Jul 9, 2014 at 6:00 AM
OpenCL uses your video card to perform certain operations faster but looking at the rest of your post I am not sure if this solved it.

Can you post a link to an image you created with ToBitmap? I am wondering if I can see the same behavior.

And what do you mean by 'my snippet is not what I've been using, returning that bitmap in using clause is in fact a runtime error.'? Returning a Bitmap should be okay as long as you are not using a 'using' statement when you create it. And the using statement in your post is for the MagickImage, not the Bitmap.

You don't need to install ImageMagick on your machine. All the code from ImageMagick is embedded in the Magick.NET library.
Jul 10, 2014 at 4:07 PM
Edited Jul 10, 2014 at 4:08 PM
Apology for the delay, I had a peak rush on project timeline.. =/

Uh..ya..my mistake, it should work, hehe..

I do not currently have the access to related files, will get back to you by early tomorrow..
Jul 10, 2014 at 4:09 PM
No need to rush. Feel free to post your message when you have time to do this. I check the discussion page daily so I will see when you have posted your message.
Jul 12, 2014 at 8:41 AM
Here's the link to related images, there's 3 files in it, the original file, the re-sized to black file, and re-sized correctly with opencl set to false...

http://54.254.176.246:8080/manager/images/images.zip
Jul 14, 2014 at 8:48 AM
Just in case if you have time, I've some new issue while progressing my project.

Its regarding to UI/Background thread issue, how easy is it for you to generate some long running animation with WPF? Like some kind of loading spinner, which spins indefinitely..

Normally if I wrap everything heavy in
Task.Factory.StartNew(new Action(() =>
{
    // You can replace this with new MagickImage(pathToPhotoLargePhoto).Distort(DistortMethod.Barrel, new double[] { 0.0, 0.0, 0.0, 0.98, 0, 0, -0.12, 1.15 });
    ImageMagickProcess(); 
});
my spinning icon would play smoothly until I do any further UI update..

However distort seems to freeze my UI while I already have it processing inside background thread..

Could you please have a try and see if you can reproduce the same problem? Let me know if you'd like to have this discussion in new thread..

Thank you..
Jul 14, 2014 at 7:07 PM
Thank you for your images. I don't understand why disabling OpenCL would resolve the issue but I would suggest you to disable it.

I cannot reproduce the problem with the distort operation. Are you doing something special with your video card in your application? And did you disable OpenCL at the start of your program?

How are you doing the animation of your spinning icon? Are you using a StoryBoard with a DoubleAnimation? That is what I use in the code of MagickViewer and the loading icon keeps animating when I add your Distort operation.