Link to home
Start Free TrialLog in
Avatar of Bryce Bassett
Bryce BassettFlag for United States of America

asked on

VBA export picture changes image file dimensions

I've build a PowerPoint 2010 toolbar that contains an "insert image" gallery for commonly used images.  As a convenience, I have added a button to allow the user to browse to any image on their computer, then it copies the image file into the library folder that is the source for the gallery, and refreshes the ribbon to show the image in the gallery.  Works fine.

My automation requires two copies of each image to be in the source folder.  One is .PNG (to allow for transparency) and the other is .JPG to create the gallery thumbnail because MS Office doesn't like pngs in galleries.  My add image routine takes the selected image, adds it temporarily as a shape on the current slide, then exports it twice into the target folder, once as a .PNG and again as a .JPG.  This works fine too, except that in the process of exporting as a PNG it seems to be altering the dimensions of the file.  The original might be a big .JPG that fills the screen, but what gets copied to the target folder is a smaller version.  I'm opening the file with dimensions -1 which is supposed to keep original dimensions, and I can verify it is coming in big.

Does anybody know enough about the export method to explain to me why this is happening.  See some code below.

Private Sub OKButton_Click()

picroot = Me.picrootbox.Value

Dim currentslide As Slide
Set currentslide = ActivePresentation.Slides(ActiveWindow.View.Slide.SlideIndex)

Set opic = currentslide.Shapes.AddPicture(FileName:=pickedpix, _
    linktofile:=msoTrue, savewithdocument:=msoTrue, Left:=0, Top:=0, Width:=-1, Height:=-1)

'opic.ScaleHeight 1, msoTrue
'opic.ScaleWidth 1, msoTrue

    exportpath = contentlibraryfolder & "\Images\Icons\" & picroot & ".png"
    opic.Export exportpath, ppShapeFormatPNG  ', opic.Width, opic.Height, ppScaleXY
    opic.LockAspectRatio = True
    opic.Width = 100
    exportpath = contentlibraryfolder & "\Images\Icons\" & picroot & ".jpg"
    opic.Export exportpath, ppShapeFormatJPG
    opic.Delete

Open in new window


You can see a couple places where I tried some code (now commented out) to resize the picture to original scale, but this makes no difference.  

For example, the above code take a chosen picture 3264 x 2448 pixels @ 72 dpi, and saves is as a .png 383 x 287 pixels @ 96 dpi.

Thanks
Avatar of Bryce Bassett
Bryce Bassett
Flag of United States of America image

ASKER

Just ran into this page which seems to be addressing this issue.  But still anxious to hear what other experts have to say.

http://www.pptfaq.com/FAQ00052_Improve_PowerPoint-s_GIF-_BMP-_PNG-_JPG_export_resolution.htm
Avatar of Jamie Garroch (MVP)
My i itial question is what issue did you come across in using PNG images in galleries? PNG is supported as this is how the highlight works when hovering over an item in the gallery. I have an addin called vicons (at youpresent.biz) in which all gallery images are PNG.
I stand corrected, Jamie.  PNGs are allowed in galleries.  I must have confused it with image controls on userforms, where PNG isn't supported.  It makes my automation simpler to have separate image files for the gallery thumbnail (jpg) and the actual image file to be inserted (png).

But that's beside the point.  I'm back to my original question.  Is there a way, using VBA, to open a selected image (in any format) and save it as a PNG format while preserving its original resolution?  The export command shrinks JPGs and enlarges PNGs.

Thanks
Okey dokey versatilebb. I use the Export function in another add-in to get an 'approximation' of a picture that is X by Y pixels but because the two Scale properties are relative to the slide dimensions and of type single, you will end up with rounding errors which make it impossible to guarantee an exact number of pixels.
But the variances I am seeing are way beyond what could be accounted for by rounding errors.  As the number in my original post show, when I pull in a JPG (and I've paused my macro to confirm the full size JPG is brought in), then export as a PNG at supposedly full size, it shrinks to about one-tenth of its original size.
ASKER CERTIFIED SOLUTION
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jamie, you are on the right track here with the 96/72 formula. But I'm still struggling to get this to work.  If I bring in a large JPG without constraining size, for example, it fills the screen.  Then if I save it at opic.Width / .SlideWidth, opic.Height / .SlideHeight, it saves as 1 x 1 pixel.   I think what I need is (opic.Width / 72) * 96, (opic.Height / 72) * 96.  

But that still doesn't always work.  

I'm going to close out this question, but if you have other thoughts, LMK.

Thanks.
If you bring in a large image and it fills the slide, chances are that PowerPoint scaled it down from 100% so you need to reset the image to 100% (and optionally calculating the scale as you can't get it from the object model) before doing anything with it. Check the dimensions in the Format pane to confirm this is what's happening.