skip to Main Content

I assumed that cropping an image would be an extremely easy thing to do from .net. But no matter what I try I just cannot seem to get the thing to work.

The documentation is somewhat vague –

‘The first parameter is an array of four coordinates that mark the portion remaining after cropping’

That could mean an array of four numbers, or it could mean an array of four arrays of two numbers (a coordinate after all consists of two numbers). the ‘portion remaining after cropping’ I take to mean ‘the portion of the image designated to remain after cropping’.

Since the select function takes an array of coordinate arrays — {{x1,y1}, y2, y2}, {x3,y3}, {x4, y4}} — I had hoped crop would work the same way. No dice.

Next, I tried the really simple approach, assume that ‘left, top, right, bottom’ really mean just that. So, I plugged in perfectly reasonable values and … no dice.

In every case, PS throws a dialog box saying ‘ Could not complete the command because the affected area is empty or does not overlap the canvas’.

Here is a code snippet:

    Dim PSDapp
    PSDapp = CreateObject("Photoshop.Application")
    Dim psarray As Object = {20, 20, 120, 120}
    Dim PSDcurrentDoc
    PSDapp.preferences.rulerUnits = 1
    PSDcurrentDoc = PSDapp.open("c:cat.bmp")
    PSDapp.activeDocument = PSDapp.documents.item(1)
    PSDcurrentDoc.crop(psarray)

What is even more strange is that if I take the above code and port it to a script, it runs just fine. Can someone (anyone!) please please post a minimal working example of using the crop feature using COM (not scripting)?

2

Answers


  1. Chosen as BEST ANSWER

    Solved, with a work-around.

    I used a selection as a work-around. The code is longer than it really should have to be since I must first make a selection, but it does work.

    Below is the full working subroutine connected to a button. I hope it is of use to somebody that might face this issue as well.

     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim x As Integer = 100 ' The x-coordinate of the upper-left corner (pixel units).
            Dim y As Integer = 100 ' The y-coordinate of the upper-left corner (pixel units).
            Dim w As Integer = 200 ' The width of the selection/crop (pixel units).
            Dim h As Integer = 200 ' The height of the selection/crop (pixel units).
    
            Dim psArray1() As Object = {x, y}
            Dim psArray2() As Object = {x, y + h}
            Dim psArray3() As Object = {x + w, y + h}
            Dim psArray4() As Object = {x + w, y}
            Dim psArray() As Object = {psArray1, psArray2, psArray3, psArray4} ' A concatenated object consisting of an array of coordinates.
    
            Dim PSDapp As Object = CreateObject("Photoshop.Application")
            Dim PSDcurrentDoc As Object = PSDapp.open("c:cat.bmp") ' Could be any document of course.
    
            PSDapp.preferences.rulerUnits = 1
            PSDcurrentDoc.selection.select(psArray)
            Dim selectArray As Object = PSDcurrentDoc.selection.bounds
            PSDcurrentDoc = PSDcurrentDoc.crop(selectArray) ' This is key. PSDcurrentDoc.crop is read-only, so it must be assigned.
    
        End Sub
    

  2. I’ve never used Photoshop, but an array of coordinates could be written like this:

    Dim psarray() As Point = {
        New Point(20, 20),
        New Point(120, 20),
        New Point(120, 120),
        New Point(20, 120)
    }
    PSDcurrentDoc.crop(psarray)
    

    So you tried something like this already?

    Dim psarray() As Integer = {20, 20, 120, 120}
    PSDcurrentDoc.crop(psarray)
    

    If that doesn’t work, try “pinning” it:

    Dim psarray() As Integer = {20, 20, 120, 120}
    Dim gch As System.Runtime.InteropServices.GCHandle
    gch = System.Runtime.InteropServices.GCHandle.Alloc(psarray, Runtime.InteropServices.GCHandleType.Pinned)
    PSDcurrentDoc.crop(gch.AddrOfPinnedObject)
    gch.Free()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search