Skip to main content
Participating Frequently
July 27, 2015
Question

Crop image from vb.net

  • July 27, 2015
  • 1 reply
  • 1923 views

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)?

This topic has been closed for replies.

1 reply

pixxxelschubser
Community Expert
Community Expert
July 27, 2015

With Javascript this works:

app.activeDocument.crop([80, 80, 300, 300]);

Perhaps you can translate it.

Participating Frequently
July 27, 2015

Thanks Pixxxel

Problem is, I can get the crop to work in vbs just fine (in a syntax similar to what you have). But, if I take that same syntax, properly coded for the COM system in .net, using the same values, I get an error thrown by PS. For example, the following works just fine with VBS:

Dim appRef 

Set appRef = CreateObject( "Photoshop.Application" )    

Dim docRef  

appRef.Preferences.RulerUnits = 1

Set docRef = appRef.ActiveDocument 

cropBounds = Array( 20, 20, 120, 120) 

docRef.Crop cropBounds 

pixxxelschubser
Community Expert
Community Expert
July 27, 2015

Excuse me if I misunderstand you, my english isn't the best.

Do you mean:

appRef.ActiveDocument works

and

PSDapp.documents.item(1)

does not work?

What is in VB the first item in the collection of documents? item(0) or item(1)