简体   繁体   English

C#中的ImageMagick:从转换调用中获取字节数组?

[英]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. 我正在尝试使用随Windows Installer for ImageMagick提供的ImageMagickObject COM组件,作为COM引用导入到我的C#项目中。 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. 我试图让它在内存中转换文件并将其返回到字节数组中,如库中包含的ArrayTest.vbs文件所示。 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: 他们的样本来自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. 如果您不熟悉ImageMagick,“null:”是指定空图像的内置类型。 "-profile" and "8BIM:iptctext.txt" are command line parameters passed to the Convert call and my array is the output essentially. “-profile”和“8BIM:iptctext.txt”是传递给Convert调用的命令行参数,我的数组本质上是输出。 In their example, myarray ends up with a byte array of the newly converted image. 在他们的例子中,myarray最终得到了新转换图像的字节数组。 In my code, myarray is unchanged. 在我的代码中,myarray没有变化。 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. ImageMagickObject旨在为大多数ImageMagick二进制文件提供等效功能,而无需创建执行二进制文件的过程。 In the case of "Convert", there is no expected output (or ImageMagickObject return value) in the success case. 在“转换”的情况下,成功案例中没有预期的输出(或ImageMagickObject返回值)。

From what I can tell you have two options if you want this from C#: 据我所知,如果你想从C#中得到这个,我有两个选择:

  1. Convert the image to a temporary file and then read it in using GDI+ Graphics . 将图像转换为临时文件,然后使用GDI + Graphics读取它。
  2. Create a .NET wrapper for the C++ API for ImageMagick. 为ImageMagick的C ++ API创建.NET包装器。 You can get a good head start by checking out ImageMagickNET . 您可以通过查看ImageMagickNET获得良好的开端。

The signature for the method you are calling from the ImageMagickObject source is: 您从ImageMagickObject源调用的方法的签名是:

[ 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. 这似乎意味着它应该接受两个参数,一个将返回一个返回值,但它无法使用除单个ref object[]参数之外的任何其他参数进行编译。

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. 我对COM编程不是很熟悉,所以希望有更多的专家能够发现这一点,并且可以帮助您找到正确的C#代码。

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). 另外(我确定你注意到了),Visual Studio检测到的方法签名暗示Convertparams object[]的形式接受参数,但它只能用一个ref object[]参数进行编译(即使它将其标记为编译前的错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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