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

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.

Bryce BassettFreelance VBA programmerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bryce BassettFreelance VBA programmerAuthor Commented:
Just ran into this page which seems to be addressing this issue.  But still anxious to hear what other experts have to say.

Jamie GarrochSenior Technical Consultant at BrightCarbonCommented:
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.
Bryce BassettFreelance VBA programmerAuthor Commented:
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.

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Jamie GarrochSenior Technical Consultant at BrightCarbonCommented:
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.
Bryce BassettFreelance VBA programmerAuthor Commented:
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.
Jamie GarrochSenior Technical Consultant at BrightCarbonCommented:
In your sample code you have the Scale parameters commented out so that leaves PowerPoint to do what it likes when exporting the different formats, especially if you have the image scaled to anything other than 100% on the slide (you need to first reverse-calculate the scale in that case).

I would suggest doing something like this to fix the output to the same size:

With ActivePresentation.PageSetup
    exportpath = contentlibraryfolder & "\Images\Icons\" & picroot
    exportPNG = exportpath & ".png"
    exportJPG = exportpath & ".jpg"
    opic.Export exportPNG, ppShapeFormatPNG, opic.Width / .SlideWidth, opic.Height / .SlideHeight, ppScaleXY
    opic.Export   exportJPG, ppShapeFormatJPG, opic.Width / .SlideWidth, opic.Height / .SlideHeight, ppScaleXY
End With

Open in new window

If I insert a default square shape of 1" x 1" on the slide and export that in the two formats, both files get exported as 97px by 97px. You can then use something like this (assumes default Windows resolution setup) to get a close approximation of a specified number of pixels for a given shape:

pixels = oShp.Width / 72 * 96

Which, in my example of the default rectangle size gives:

pixels = 72 / 72 * 96 => 97 x 97 pixel image!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bryce BassettFreelance VBA programmerAuthor Commented:
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.

Jamie GarrochSenior Technical Consultant at BrightCarbonCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.