locked
Inserting an Image and Scaling RRS feed

  • Question

  • I'm tryig to insert an signature image in a word document.  Unfortunately the sizes of the images is inconsistant (leave it to engineers to ignore detailed instructions).

    At any rate, what I want to do is insert an image make it 75px in height, then scale the width to match.  For some reason it's not working quite right.

    Here is the code I have so far:

    Dim shp As Word.InlineShape = oTable.Cell(2, 1).Range.InlineShapes.AddPicture(sEngineerSigFilename)
    With shp
      Dim fOldWidth As Single = .Width
      Dim fOldHeight As Single = .Height
      Dim fRatio As Single = 75 / fOldHeight
      .Height = 75
      .Width = fOldWidth * fRatio
    End With 
    
    

    While testing to see what's going wrong, I discovered the following:

    1. The original height/width of the image (according to the file properties) is 111px x 414 px
    2. The height/width of the image after it's been added, but before its scaled (QuickWatch on .Height and .Width) is 36.8 x 137.35 (Huh?)
    3. If I click on the image in the doc file, which displays a frame around it, then capture that into Paint and measure it, the frame is 100px high.
    4. The frame around the image is 373px wide.  But the image itself is cropped at 220px.

    What's going on?

    I have another image where I'm scaling similarly (except I'm scaling for a constant width of 100px instead of a constant height) and it's working.  Almost the exact same code block (except for the change from width to height, etc).

    How do I add an image and scale them all so that their height is consistant?

    TIA


    Clif (Your company name here)
    Thursday, June 30, 2011 6:47 PM

Answers

  • I know what it should be, and I know what I see it is.  Basically I'm saying that I put a BreakPoint at the line that reads "Dim fRatio As Single = 75 / fOldHeight". Then I check the values of shp.Height and shp.Width using QuickWatch. The values are as I said. Odd, yes. But if it were what it was supposed to be, I wouldn't have created a post asking (among other things) why it's different than it should be.

    Hi Clif001,

    Sorry for the delay.

    I could understand this problem. I reproduced the code on my side. It is the same scenario. The reason is related to the resolution you're using to display the image.  

    The original height/width of the image (according to the file properties) is 111px x 414 px. Here the unit is pixel. When you right click the image inserted in Word->click size and position, you could see the measure unit is inch. The unit of Word.InlineShape.Width returned value is pixel. Assume the height you check is 5'', the returned value using Word.InlineShape.Width is 360 pixels. About how many pixels per inch, this depends on your screen resolution. On my computer, the display resolution is 72 pixels per inch. So 5''*72=360 pixels. You could check your screen resolution. 

    More information about Pixel density: http://en.wikipedia.org/wiki/Pixels_per_inch

    When you insert a picture in a cell of the table, if it's wider than the column width, the picture will automatically resize to fit in the cell. Word.InlineShape.Width and Word.InlineShape.Height return the width and height of the specified inline shape. It has been scaled, not the original size.

    You could get the ScaleHeight/ScaleWidth which scales the height/width of the specified inline shape relative to its original size using InlineShape.ScaleHeight and InlineShape.ScaleWidth.

    So the original size could be calculated by:

     Dim originalheight As Single = shp.Height / shp.ScaleHeight
    
     Dim originalwidth As Single = shp.Width / shp.ScaleWidth

    In addition, you need to check the picture resolution, right click on filename, select "Properties"->select "Details" item, you could see there is a property about Horizontal resolution and Vertical resolution. If it is not the same as your screen resolution. You need to calculated the original size by:

     Dim Originalheightnew As Single = (originalheight / YourscreenResolution) * picturesolution
    
     Dim originalwidthnew As Single = (originalwidth / YourscreenResolution) * picturesolution

    If you want to use the original size of the inserted image with pixel, you could try below code to get directly:

    Dim img As Image = Image.FromFile("filename") 
    
    Dim imgwidth As Single = img.Width 
    
    Dim imgheight As Single = img.Height 

    More information about Word.InlineShape:
    http://msdn.microsoft.com/de-de/library/ms263854(Office.11).aspx

    If you have any concerns, please feel free to follow up.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    • Marked as answer by Clif001 Wednesday, July 6, 2011 12:53 PM
    Tuesday, July 5, 2011 12:12 PM

All replies

  • Ok, I know why it's cropping.  I'm putting the image in a table (hence the oTable.Cell(2, 1)...) and that's what's cropping it.

    I'm still having the sizing issue, but at least I've solved the cropping problem.


    Clif (Your company name here)
    Thursday, June 30, 2011 7:04 PM
  • Hi Clif001,

    Thanks for your post.

    I am sorry I could not understand this problem very clearly. I need to make some clarifications with you.

    1. The original height/width of the image (according to the file properties) is 111px x 414 px

    Before you add the image to the word, the height/width of the image is 111px x 414px.

    2. The height/width of the image after it's been added, but before its scaled (QuickWatch on .Height and .Width) is 36.8 x 137.35 (Huh?) 

    What do you mean before its scaled(QuickWatch on .Height and .Width) is 36.8 x 137.35? How do you get this result? When you added the image, the height and width should be 75px and 100px.

    3. If I click on the image in the doc file, which displays a frame around it, then capture that into Paint and measure it, the frame is 100px high.
    4. The frame around the image is 373px wide.  But the image itself is cropped at 220px. 

    For cropping, you have found the reason which is related to the oTable.Columns.Width because you put the image in a table. If you set the oTable.columns.width=fOldWidth * fRatio, then the frame around the image is the image's width.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, July 1, 2011 9:06 AM
  • Before you add the image to the word, the height/width of the image is 111px x 414px.

    True.  I check the properties of the physical file (right click on filename, select "Properties")

    What do you mean before its scaled(QuickWatch on .Height and .Width) is 36.8 x 137.35? How do you get this result? When you added the image, the height and width should be 75px and 100px.

    I know what it should be, and I know what I see it is.  Basically I'm saying that I put a BreakPoint at the line that reads "Dim fRatio As Single = 75 / fOldHeight". Then I check the values of shp.Height and shp.Width using QuickWatch. The values are as I said. Odd, yes. But if it were what it was supposed to be, I wouldn't have created a post asking (among other things) why it's different than it should be.

    For cropping, you have found the reason which is related to the oTable.Columns.Width because you put the image in a table. If you set the oTable.columns.width=fOldWidth * fRatio, then the frame around the image is the image's width.

    Yes, I figured all that out already. Basically, rather than adjust the width of the table to the scaled width of the image, I check the potential scaled width of the image and, if it will be greater than the width of the cell, then I re-scale the width to fit (re-adjusting the height to be proportional).

    But all this still doesn't answer the question as to why the shp.Height and shp.Width numbers are different than the physical size of the image as read in the file properties.


    Clif (Your company name here)
    Friday, July 1, 2011 12:27 PM
  • I know what it should be, and I know what I see it is.  Basically I'm saying that I put a BreakPoint at the line that reads "Dim fRatio As Single = 75 / fOldHeight". Then I check the values of shp.Height and shp.Width using QuickWatch. The values are as I said. Odd, yes. But if it were what it was supposed to be, I wouldn't have created a post asking (among other things) why it's different than it should be.

    Hi Clif001,

    Sorry for the delay.

    I could understand this problem. I reproduced the code on my side. It is the same scenario. The reason is related to the resolution you're using to display the image.  

    The original height/width of the image (according to the file properties) is 111px x 414 px. Here the unit is pixel. When you right click the image inserted in Word->click size and position, you could see the measure unit is inch. The unit of Word.InlineShape.Width returned value is pixel. Assume the height you check is 5'', the returned value using Word.InlineShape.Width is 360 pixels. About how many pixels per inch, this depends on your screen resolution. On my computer, the display resolution is 72 pixels per inch. So 5''*72=360 pixels. You could check your screen resolution. 

    More information about Pixel density: http://en.wikipedia.org/wiki/Pixels_per_inch

    When you insert a picture in a cell of the table, if it's wider than the column width, the picture will automatically resize to fit in the cell. Word.InlineShape.Width and Word.InlineShape.Height return the width and height of the specified inline shape. It has been scaled, not the original size.

    You could get the ScaleHeight/ScaleWidth which scales the height/width of the specified inline shape relative to its original size using InlineShape.ScaleHeight and InlineShape.ScaleWidth.

    So the original size could be calculated by:

     Dim originalheight As Single = shp.Height / shp.ScaleHeight
    
     Dim originalwidth As Single = shp.Width / shp.ScaleWidth

    In addition, you need to check the picture resolution, right click on filename, select "Properties"->select "Details" item, you could see there is a property about Horizontal resolution and Vertical resolution. If it is not the same as your screen resolution. You need to calculated the original size by:

     Dim Originalheightnew As Single = (originalheight / YourscreenResolution) * picturesolution
    
     Dim originalwidthnew As Single = (originalwidth / YourscreenResolution) * picturesolution

    If you want to use the original size of the inserted image with pixel, you could try below code to get directly:

    Dim img As Image = Image.FromFile("filename") 
    
    Dim imgwidth As Single = img.Width 
    
    Dim imgheight As Single = img.Height 

    More information about Word.InlineShape:
    http://msdn.microsoft.com/de-de/library/ms263854(Office.11).aspx

    If you have any concerns, please feel free to follow up.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    • Marked as answer by Clif001 Wednesday, July 6, 2011 12:53 PM
    Tuesday, July 5, 2011 12:12 PM
  • Hi Clif001,

    I modified my reply. Please check and let us know the situation on your side. If you still need further assistance with this issue, please feel free to let us know.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 6, 2011 2:41 AM
  • The original height/width of the image (according to the file properties) is 111px x 414 px. Here the unit is pixel. When you right click the image inserted in Word->click size and position, you could see the measure unit is inch. The unit of Word.InlineShape.Width returned value is pixel. Assume the height you check is 5'', the returned value using Word.InlineShape.Width is 360 pixels. About how many pixels per inch, this depends on your screen resolution. On my computer, the display resolution is 72 pixels per inch. So 5''*72=360 pixels. You could check your screen resolution. 


    This one paragraph answers my question as to why the values were so different. Thanks. As far as my problem, I ended up with a trial and error approach. I kept on trying different divisors until I found one that gave me the visual ratio that I needed and looked good.

    Thanks for the explanation, though.


    Clif (Your company name here)
    Wednesday, July 6, 2011 12:53 PM