简体   繁体   中英

ImageMagick in C#: Get byte array from Convert call?

I am trying to use the ImageMagickObject COM component supplied with the Windows Installer for ImageMagick, imported to my C# project as a COM reference. So far, I've been able to use it to convert images from one file on disk to another file on disk. I'm trying to get it to convert the file in-memory and return it in a byte array as shown in the ArrayTest.vbs file included with the library. Has anyone successfully done this before? Documentation is severely lacking on this feature...

My code:

MagickImageClass _magic = new MagickImageClass();
object[] myarray = new object[1];
myarray[0] = "JPEG:";

object[] args = new object[] { tbFilename.Text, "-colorspace", "cmyk", "-resize", "50%", myarray };
var result = _magic.Convert(ref args);

Their sample from ArrayTest.vbs:

Dim img
Dim myarray(1)

Set img = CreateObject("ImageMagickObject.MagickImage.1")
myarray(0)="8BIM:"

msgs = img.Convert("null:","-profile","8BIMTEXT:iptctext.txt",myarray)

If you're not familiar with ImageMagick, "null:" is a built-in type specifying a null image. "-profile" and "8BIM:iptctext.txt" are command line parameters passed to the Convert call and my array is the output essentially. In their example, myarray ends up with a byte array of the newly converted image. In my code, myarray is unchanged. Help!!

The ImageMagickObject is intended to provide equivalent features for most of the ImageMagick binaries, without the overhead of creating a process to execute the binaries. In the case of "Convert", there is no expected output (or ImageMagickObject return value) in the success case.

From what I can tell you have two options if you want this from C#:

  1. Convert the image to a temporary file and then read it in using GDI+ Graphics .
  2. Create a .NET wrapper for the C++ API for ImageMagick. You can get a good head start by checking out ImageMagickNET .

The signature for the method you are calling from the ImageMagickObject source is:

[ vararg, id( 6 ) ] HRESULT Convert([ in, out, satype( VARIANT ) ] SAFEARRAY * *pArrayVar, [ out, retval ] VARIANT * pVar );

This seems to imply that it should accept two arguments, one which will out a return value, but it fails to compile with anything other than a single ref object[] argument.

I'm not very familiar with COM programming, so hopefully somebody more expert spots this and can help direct you to the correct C# code.

Also (as I'm sure you noticed), the method signature detected by Visual Studio implies that Convert takes arguments in the form params object[] , but it will only compile with one ref object[] argument (even though it marks it as an error before compilation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM