• Getting/Maipulating Image Properties (VBA 6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Getting/Maipulating Image Properties (VBA 6)

    Author
    Topic
    #393244

    I am trying to find out how to access the properties of an image placed in a report. My goal is to manuplate the image by cropping it or changing its size. How can I access an images properties once it is placed in a report or in a MS word document?

    Thanks

    Viewing 3 reply threads
    Author
    Replies
    • #711588

      In Word, an image can be either an InlineShape or a (drawing layer) Shape. If you haven’t tagged the shape with some kind of identifier at the time you insert it, you might have difficulty determining which one is the target image. I don’t know whether you actually can crop a shape using VBA. Might be interesting to experiment.

    • #711589

      In Word, an image can be either an InlineShape or a (drawing layer) Shape. If you haven’t tagged the shape with some kind of identifier at the time you insert it, you might have difficulty determining which one is the target image. I don’t know whether you actually can crop a shape using VBA. Might be interesting to experiment.

    • #712115

      I think I know the sort of thing you’re wanting to do here, as I was faced with a similar problem myself. I needed to know the dimensions of .JPG files (in advance) that I was inserting programatically. I ended up writing a VBA routine to read the bytes of the .JPG file, and pull out the metrics when I found them. This was not trivial, since .JPG is not a simple “fixed field” format. It became a bigger problem when other graphic formats were added to the mix.

      I think it was Charlotte who alerted me to the fact that the picture box has a self-sizing property. From there, it was simply a matter of setting the picture name and pulling the height and width properties of the control, then using these for the image metrics. Is this relevant to your situation? If so, I could dig out some of the old code or try write a sample.

      Alan

      • #712175

        Yeah this is relevant to my situation to a certain degree. Any information that you could provide me would be a great help

        Here is more of a breakdown of what I am trying to do. In a nutshell, users submit forms with an image attached to them. In this case, I only have to worry about JPEGS. What I need to do is change the image size on the file when it is getting inserted into a report for printing. What makes this really complex, and I hope that this is possable is that if the image does not adjust to the proper dimension, then I need to crop the image so it does fit in the dimensions. I am would adjust the size of the image first to make it as close as possable, then crop afterwards.

        The other sticker, which I do not think is possable in VBA, is the ability to change the DPI, not just the pixel dimension of the image. So if an image is 1024×768 and is 72 DPI, I need to change that image to 200DPI so it will print better. I am wondering if it is possable to somehow interact with Photoshop to run a macro to make the DPI change of the image.

        Any help that I can get on this scenerio would be awesome

        Thanks

        • #712285

          I’m curious as to what extent VBA is involved here. From your description I get the impression that you need to preprocess the image before it even appears in the report. If this is correct, and if you know the final pixel dimensions you need, wouldn’t it be easier to do the image manipulation first, in a dedicated imaging program?

          If I’m reading this right, let’s say you want the image to sit in a 90 x 90 pixel “frame” in your report. The image sent to you is 320 x 240. You can use a freeware program like IrfanView to resample the image to ? x 90, where ? will be determined by maintaining the same aspect ratio (no distortion). ? will be bigger than 90 in this case, so you can then crop the image appropriately to the required 90 x 90.

          As far a changing the DPI, I just noticed that IrfanView can do that too! I think there are issues to consider re: DPI and PPI when it comes to printing, but that’s way beyond what I’ve dabbled into.

          Alan

          • #712324

            The goal is to get these images to happen on the fly, without using an image editing program to manipulate the size. They will have hundreds of images a day. Since they can be used in various sizes at different times, I do not want to change to origanal image

          • #712325

            The goal is to get these images to happen on the fly, without using an image editing program to manipulate the size. They will have hundreds of images a day. Since they can be used in various sizes at different times, I do not want to change to origanal image

        • #712286

          I’m curious as to what extent VBA is involved here. From your description I get the impression that you need to preprocess the image before it even appears in the report. If this is correct, and if you know the final pixel dimensions you need, wouldn’t it be easier to do the image manipulation first, in a dedicated imaging program?

          If I’m reading this right, let’s say you want the image to sit in a 90 x 90 pixel “frame” in your report. The image sent to you is 320 x 240. You can use a freeware program like IrfanView to resample the image to ? x 90, where ? will be determined by maintaining the same aspect ratio (no distortion). ? will be bigger than 90 in this case, so you can then crop the image appropriately to the required 90 x 90.

          As far a changing the DPI, I just noticed that IrfanView can do that too! I think there are issues to consider re: DPI and PPI when it comes to printing, but that’s way beyond what I’ve dabbled into.

          Alan

        • #712614

          > So if an image is 1024×768 and is 72 DPI, I need to change that image to 200DPI so it will print better.

          Think about this differently. The image has 1024 pixels across and 768 pixels down. The output size is determined by the number of pixels per inch that you tell your software to output. Conversely, the number of pixels per inch is determined by the output size you give your software. But the number of pixels/dots is a constant, unless you actually resample your image, which can introduce distortions.

          It makes more sense, I think to inspect and size the inserted image and let Word worry about how many DPI it wants the printer to print to represent that image.

          The following old, old code applies a scaling factor to all images in the document:

          Sub ShrinkPic()
          ' Created 8/22/00 to resize all graphics in a document at once
          If ActiveDocument.InlineShapes.Count = 0 Then
              MsgBox ("No ''inline'' pictures to resize in this document.")
              Exit Sub
          End If
           
          If MsgBox("Do you want to resize ALL the inline picture graphics in this document?", _
                  vbQuestion + vbYesNo)  vbYes Then Exit Sub
           
          Dim iShape As InlineShape, fPct As Single
          fPct = Val(InputBox("Enter new scaling factor, relative to current size (e.g., ", _
                  "Resize pictures", " 0.80"))
           
          For Each iShape In ActiveDocument.InlineShapes
              If iShape.Type = wdInlineShapePicture Then
                  iShape.Height = fPct * iShape.Height
                  iShape.Width = fPct * iShape.Width
              End If
          Next
          End Sub

          After running this code, if the size previously said 100% x 100%, it now will be adjusted to the selected scaling factor (by default, 80% x 80%). You also can use pixel measurements. Basically, decide your constraint (e.g., no taller than or no wider than or both), then figure out how to do the math. Then, your code can interrogate the object and, if needed, apply the constraint. Hope this helps.

          • #712703

            Hi. Thanks for the input. That is a good starting point to work from. Here are some more details/questions that I have.

            For resampling the images, I do need to do that as the overall image quality does improve. We conducted serveral tests and came to the conclusion resampling was needed. When you resample, the pixels size will increase (of course), but the print size will stay the same.

            I guess a better way of wording my first question would be; if I have an image 6″ x 9″, at 72 DPI, I would like to resample and resize the image to 5″ x 7″ at 200DPI. Is there a way to resample an image in VBA?

            In word, when placing the same image with two different sample sizes, both images are not the same size. I belive word changes the image with 200 DPI to 72DPI and increases the print size. Is there a way for word to keep the image at it same DPI and threfore, keep the same printsize? Is there a better program that I could use to dynamically insert images for high quality printing?

            As for resizing, if the original image does not fit to one of my required print sizes, which are in inches: 5×7, 5×3.5, 3.5×2.5, is there a way to crop an image after it is resized?

            Thanks for your help

            • #712727

              Most of this is completely unfamiliar territory for me. It looks as though Word treats inserted inline images as though they are meant to be displayed at 96ppi, not 72ppi. This is consistent with the browser standard. I think you are going to need external software to resample the image, hopefully a COM server so you can simply integrate it as a DLL call.

              To constrain the dimensions of an image you are inserting, you can use a TextBox pre-set to the proper height and width. However, if the aspect ratio of the picture differs from that of the textbox, the textbox will get resized. There probably is a way to avoid this, and that would be good, because the picture looks fuzzier to me when that happens.

              I don’t know if the object model supports cropping. Try recording a macro and seeing if any code is generated.

            • #712728

              Most of this is completely unfamiliar territory for me. It looks as though Word treats inserted inline images as though they are meant to be displayed at 96ppi, not 72ppi. This is consistent with the browser standard. I think you are going to need external software to resample the image, hopefully a COM server so you can simply integrate it as a DLL call.

              To constrain the dimensions of an image you are inserting, you can use a TextBox pre-set to the proper height and width. However, if the aspect ratio of the picture differs from that of the textbox, the textbox will get resized. There probably is a way to avoid this, and that would be good, because the picture looks fuzzier to me when that happens.

              I don’t know if the object model supports cropping. Try recording a macro and seeing if any code is generated.

          • #712704

            Hi. Thanks for the input. That is a good starting point to work from. Here are some more details/questions that I have.

            For resampling the images, I do need to do that as the overall image quality does improve. We conducted serveral tests and came to the conclusion resampling was needed. When you resample, the pixels size will increase (of course), but the print size will stay the same.

            I guess a better way of wording my first question would be; if I have an image 6″ x 9″, at 72 DPI, I would like to resample and resize the image to 5″ x 7″ at 200DPI. Is there a way to resample an image in VBA?

            In word, when placing the same image with two different sample sizes, both images are not the same size. I belive word changes the image with 200 DPI to 72DPI and increases the print size. Is there a way for word to keep the image at it same DPI and threfore, keep the same printsize? Is there a better program that I could use to dynamically insert images for high quality printing?

            As for resizing, if the original image does not fit to one of my required print sizes, which are in inches: 5×7, 5×3.5, 3.5×2.5, is there a way to crop an image after it is resized?

            Thanks for your help

        • #712615

          > So if an image is 1024×768 and is 72 DPI, I need to change that image to 200DPI so it will print better.

          Think about this differently. The image has 1024 pixels across and 768 pixels down. The output size is determined by the number of pixels per inch that you tell your software to output. Conversely, the number of pixels per inch is determined by the output size you give your software. But the number of pixels/dots is a constant, unless you actually resample your image, which can introduce distortions.

          It makes more sense, I think to inspect and size the inserted image and let Word worry about how many DPI it wants the printer to print to represent that image.

          The following old, old code applies a scaling factor to all images in the document:

          Sub ShrinkPic()
          ' Created 8/22/00 to resize all graphics in a document at once
          If ActiveDocument.InlineShapes.Count = 0 Then
              MsgBox ("No ''inline'' pictures to resize in this document.")
              Exit Sub
          End If
           
          If MsgBox("Do you want to resize ALL the inline picture graphics in this document?", _
                  vbQuestion + vbYesNo)  vbYes Then Exit Sub
           
          Dim iShape As InlineShape, fPct As Single
          fPct = Val(InputBox("Enter new scaling factor, relative to current size (e.g., ", _
                  "Resize pictures", " 0.80"))
           
          For Each iShape In ActiveDocument.InlineShapes
              If iShape.Type = wdInlineShapePicture Then
                  iShape.Height = fPct * iShape.Height
                  iShape.Width = fPct * iShape.Width
              End If
          Next
          End Sub

          After running this code, if the size previously said 100% x 100%, it now will be adjusted to the selected scaling factor (by default, 80% x 80%). You also can use pixel measurements. Basically, decide your constraint (e.g., no taller than or no wider than or both), then figure out how to do the math. Then, your code can interrogate the object and, if needed, apply the constraint. Hope this helps.

      • #712176

        Yeah this is relevant to my situation to a certain degree. Any information that you could provide me would be a great help

        Here is more of a breakdown of what I am trying to do. In a nutshell, users submit forms with an image attached to them. In this case, I only have to worry about JPEGS. What I need to do is change the image size on the file when it is getting inserted into a report for printing. What makes this really complex, and I hope that this is possable is that if the image does not adjust to the proper dimension, then I need to crop the image so it does fit in the dimensions. I am would adjust the size of the image first to make it as close as possable, then crop afterwards.

        The other sticker, which I do not think is possable in VBA, is the ability to change the DPI, not just the pixel dimension of the image. So if an image is 1024×768 and is 72 DPI, I need to change that image to 200DPI so it will print better. I am wondering if it is possable to somehow interact with Photoshop to run a macro to make the DPI change of the image.

        Any help that I can get on this scenerio would be awesome

        Thanks

    • #712116

      I think I know the sort of thing you’re wanting to do here, as I was faced with a similar problem myself. I needed to know the dimensions of .JPG files (in advance) that I was inserting programatically. I ended up writing a VBA routine to read the bytes of the .JPG file, and pull out the metrics when I found them. This was not trivial, since .JPG is not a simple “fixed field” format. It became a bigger problem when other graphic formats were added to the mix.

      I think it was Charlotte who alerted me to the fact that the picture box has a self-sizing property. From there, it was simply a matter of setting the picture name and pulling the height and width properties of the control, then using these for the image metrics. Is this relevant to your situation? If so, I could dig out some of the old code or try write a sample.

      Alan

    Viewing 3 reply threads
    Reply To: Getting/Maipulating Image Properties (VBA 6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: