0

I am using the StitchEngine.dll from the Microsofot Image Composite Engine (ICE) to attempt to stitch images together in c#. I am trying to export the resulting image using the following method:

StartExporting(string, System.ValueType, Microsoft.Research.ICE.Stitching.OutputOptions, bool)

The description of this method gives:

public bool StartExporting(string *filename*, System.ValueType *cropRectangle*, float *scale*, Microsoft.Research.ICE.Stitching.OutputOptions *outputOptions*, bool *showCompletion*.

When I attempt to call the method, I am getting an InvalidCastException, stating that the Specified cast is not valid.

I am doing the following:

Microsoft.Research.ICE.Stitching.OutputOptions outOpt = new Microsoft.Research.ICE.Stitching.OutputOptions(Microsoft.Research.ICE.Stitching.ExportFormat.JPEG, 75, false, false, false);
var valTyp = new Rectangle(0, 0, 500, 1280);
StartExporting(myFilename, valTyp, outOpt, false);

Does anyone have any idea how I could correct this?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Richard Martin
  • 131
  • 1
  • 9
  • 2
    Are you referring to the `Rectangle`-struct from `System.Drawing` or the `Rectangle`-class from `system.Windows.Shapes`? The latter of course isn´t convertible to `ValueType`. Anyway you should never use `ValueType` in your code. What does `method` do with the parameter? I can´t imagine how anyone would use `ValueType` if not by a generic constraint `where T: struct`. – MakePeaceGreatAgain Jul 30 '18 at 15:15
  • 1
    Do you have documentation for the expected actual type of the parameter? – Jon Skeet Jul 30 '18 at 15:16
  • 10
    The fact that you're seeing `ValueType` there strongly suggests that something went wrong with the method signature. `ValueType` is the nominal base type of all value types in .NET, but using it explicitly is almost never correct. The method is apparently not accepting a `Rectangle`, but some other type. If the DLL is managed, you could use a disassembler like ILSpy to shed more light on what it's doing. If it's unmanaged (declared with `DllImport` or part of a COM interface), finding the unmanaged signature would help. This is the sort of thing I'd expect to see from a wonky type library. – Jeroen Mostert Jul 30 '18 at 15:18
  • I am attempting to use the StitchEngine of the Microsoft Image Composite Engine. It isn't something that is currently available as an SDK or API so documentation is non-existent. I am using the Object Browser in Visual Studio to see the property, method, and event definitions. I am going to modify the question to reflect what I know. I thought perhaps I should not immediately say what I am doing, but oh well. I will try to use ILSpy! – Richard Martin Jul 30 '18 at 16:34
  • 2
    It is a C++/CLI assembly, ValueType is a traditional mistake in that language when the programmer uses the ^ hat incorrectly. A decent disassembler is the weapon of choice, it tells you that you need to use System.Windows.Int32Rect from WindowsBase to keep the CLR happy. – Hans Passant Jul 30 '18 at 16:56
  • I should mention that I tried using a rectangle directly instead of ValueType, and got the same error. – Richard Martin Jul 30 '18 at 16:57
  • Int32Rect worked! Thank you. How do I mark a comment as the answer ? – Richard Martin Jul 30 '18 at 17:06
  • Just share what you discovered by writing your own post and marking it as the answer. Should be pretty useful to other programmers that try this library as well, I'll add the tag. – Hans Passant Jul 30 '18 at 17:29

1 Answers1

1

As Hans Passant has shown me, the correct type is System.Windows.Int32Rect. This requires adding WindowsBase as a reference.

Richard Martin
  • 131
  • 1
  • 9
  • If you wonder why your exception did not contain a helpful message similar to __"Cannot unbox a reference of type 'System.Drawing.Rectangle' into a value of type 'System.Windows.Int32Rect'."__, I think it is related to [Why does 'unbox.any' not provide a helpful exception text the way 'castclass' does?](https://stackoverflow.com/questions/39914845/). – Jeppe Stig Nielsen Feb 20 '19 at 13:28
  • Richard, do you mind providing a minimal example for a successful stitching using the `StitchEngine.dll`? I opened a new question for this [here](https://stackoverflow.com/questions/54800001/minimal-working-example-for-microsoft-image-composite-editor-stitchengine-dll). Thanks for considering my request. – HansHirse Feb 22 '19 at 13:59