1

I am trying to create a thumbnail image using IronPython utilising the Common Language Runtime.

Coming from a Visual Basic background I am struggling with the concept of passing a delegated function to the CLR from within IronPython.

Below is my coding:

import os
import clr

# contains Image definition
clr.AddReference('System.Drawing')
from System.Drawing import Image

# contains Action and Func for delegation
clr.AddReference('System.Core')
from System import Func

# open image filename
objImageA = Image.FromFile('a.jpg')

# delegated function
def ImageAbortDelegate():
    return False


objThumbImageAbort = Func[objImageA.GetThumbnailAbort](ImageAbortDelegate)

# for this example reduce image by 10 percent
intHeight = objImageA.Height / 10
intWidth = objImageA.Width / 10

# why is this failing?
objThumbImageA = objImageA.GetThumbnailImage(intHeight, intWidth, objThumbAbort, 0)
# gives error message TypeError: expected GetThumbnailImageAbort, got Func[GetThumbnailImageAbort]

2 Answers2

1

That works perfectly. Thank you.

For a better understanding, would you be able to explain what is happening with the ThumbnailImageAbort() function.

  • `GetThumbnailImageAbort` creates the delegate, that's why there is no need to wrap it in `Func`. – David Sep 01 '16 at 18:08
0

To create an instance of GetImageThumbailAbort, all you need is objImageA.GetThumbnailImageAbort(ImageAbortDelegate). When you try to create the thumbnail image you have to pass a IntPtr type, not just a regular integer, so you'll need to import the type. To do that import you will need from System import IntPtr. When you go to create the thumbnail in the end, you should have something like objThumbImageA = objImageA.GetThumbnailImage(intHeight, intWidth, objThumbAbort, IntPtr(0)).

David
  • 131
  • 4
  • 9