Consulting

Results 1 to 6 of 6

Thread: Macro for cropping picture

  1. #1

    Macro for cropping picture

    I have several plots output from another program as jpgs. These are graphs or contour plots. They have a thin line for a border, but significant white space outside this border. I want to import these pictures, but to see the contents of the figures as best that I can, I have to crop out the white space and then resize the pictures to 6.5-inch width, keeping aspect ratio constant. This process takes a lot of time, as I have many of these pictures.

    I would like to have a macro that does the following after selecting the imported picture: (1) crop out the white space (I would have to experiment with how much to crop around each edge--the amounts cropped at each edge will be the same for each picture), and (2) resize to a width of 6.5 inches.

    I have never written a visual basic macro for Word. This cannot be very difficult. Can someone help me out?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    I have never flown a jumbo jet either, but it cannot be that hard! After all, all you have to do is sit in the cockpit and operate the controls.

    Here is some code that should get you started:

    Sub CropDemo()
    Dim oShp As Word.Shape
    Dim oILS As InlineShape
    Dim lngWidth As Double
    Dim lngHeight As Double
      Set oShp = ActiveDocument.InlineShapes(1).ConvertToShape
        ' Retrieve the dimensions of the shape:
        lngWidth = oShp.Width
        lngHeight = oShp.Height
        With oShp.PictureFormat.Crop
        ' Modify the picture itself, not its container:
        ' Shift the picture 10% to the right and down,
        ' and then make the picture 90% of its original size.
        .PictureOffsetX = lngWidth / 10
        .PictureHeight = lngHeight * 0.9
        .PictureWidth = lngWidth * 0.9
        ' Now modify the cropping by changing the shape
        ' of the container. Changing the shape alters
        ' the portion of the picture that you see:
        .ShapeHeight = 200
        .ShapeWidth = 200
        .ShapeLeft = 200
        .ShapeTop = 150
         End With
      Set oILS = oShp.ConvertToInlineShape
    lbl_Exit:
      Exit Sub
    End Sub
    You might also find this useful: http://gregmaxey.mvps.org/word_tip_p...ry_add_in.html
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Thanks Greg,

    You have provided me some points to investigate and learn. I thank you very much.

    I still have some confusion with the meaning of some terminology.
    For clarification, should I understand that the picture frame is
    the actual size of the picture, but the carriage is the cropping frame?

    Are the two frames (picture and carriage) connected or independent? I believe they are connected. But can I resize the picture once cropped so that the crop remains the same with respect to the picture?

    I am not sure I understand why you move the picture to begin with, except to show me some possible commands.

    The default of my jpgs is 750 x 564 pixels, resulting in a 5" by 3.76" initial picture. I can specify something else of the same ratio--something that will get me more resolution. I suppose that ideally, I should specify something such that the cropped final will have the dimensions that I want (6.5" width).

    Manually, I crop first, and then resize the width of the picture, with aspect ratio kept constant.

    Mark

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    I was just introducing you to some of the controls. Here is the fish.

    Lets say you have a 1/2 white border around the image.

    Sub CropDemo()
    Dim oILS As InlineShape
      Set oILS = Selection.InlineShapes(1)
      With oILS
        .PictureFormat.CropLeft = 36
        .PictureFormat.CropTop = 36
        .PictureFormat.CropRight = 36
        .PictureFormat.CropBottom = 36
      End With
      With oILS
        .LockAspectRatio = True
        .Height = 360
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Greg,

    Thank you very very much. I did not have the opportunity to test your code before now, but it was easy to follow and understand. I successfully modified the amounts of pixels cropped and changed Height to Width to get the 6.5 width needed. Thank you for taking the time to show and teach me.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    My pleasure and glad you may catch your own next fish.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •