Problem using Printer.PaintPicture with wmf files

Posted on 2010-09-21
Medium Priority
Last Modified: 2013-12-25
We create our wmf files using PowerPoint vbscript (Slide.Export to wmf). In particular these slides may contain pictures (clipart) which extend beyond the slide's frame. In other words, a portion of the clipart is within the slide’s borders -- the rest is outside of the frame but viewable in PowerPoint’s editor.

When opening these wmf files using Paint, Windows Picture and Fax Viewer, or Microsoft Office Picture Manager, the wmf is displayed correctly -- all the content within the slide’s frame is visble – any content outside the frame is obscured.

Now, in our standalone VB6 Windows form application, we load the wmf files using the following code (Image1 is an Image component within our form)

Image1.Picture = LoadPicture("c:\sample.wmf")

When we attempt print the image, using the following code

Printer.PaintPicture Image1.Picture, 0, 0 (Printer could be pointing to any printer)

the printout shows all of the content, including the portion of the pictures which were supposed to be obscured. I had attempted to use PictureBox component  instead of an Image component but Printer.PaintPicture still prints all of the content.

I need to print just what’s inside of the slide’s frame. Any thoughts?

Question by:mic138
  • 3
  • 3
LVL 17

Expert Comment

ID: 33732811

The documentation for paintpicture is a touch scanty when it comes to the implantation of picture cropping.

object.PaintPicture picture, x1, y1, width1, height1, x2, y2, width2, height2, vbSrcCopy

1) You need to set x1 & y1 as the position on the page where you want the top left of the picture to be.
2) Width1 & Height1 are the size of the picture that you want on your page.
Setting these values must be done in proportion to the width/height of the source image in order to preserve the aspect ratio of the image.
3) X2 & Y2 you set to the left/top position on the source image that you want. So these are affectively your crop left and crop top settings.
4)  width2 & height2 specify the size of the area from point x2,y2 that you want to select from the source image.
So to use copping from right and bottom is a simple matter of calculation.

Tip 1 test the output to the screen before testing the print so you don;t waste a lot of ink & paper.

Code Sample to follow

Good luck and hope this helps :~)

' Sample assumes picture boxes picture1 & picture2 and chkLandscape check box

Dim sFile As String

' this example assumes that your picture boxes & printer are set to twips

' the the source image
sFile = "c:\$WebZone\WebSite\Newsletters\globe.wmf"
Picture2.AutoRedraw = True ' the source image will acquire correct sizes
Picture2.AutoSize = True 

Picture1.AutoRedraw = True ' so you can see changes

Set Picture2.Picture = LoadPicture(sFile)

Dim sngSourceWidth As Single
Dim sngSourceHeight As Single

' Get the width & height of source of the image
sngSourceWidth = Picture2.ScaleWidth
sngSourceHeight = Picture2.ScaleHeight

' Set a value depending on metric centimeters or inches for cropping conversion
Dim TWIPS As Single
Dim YourScaleMode As String

YourScaleMode = "M" ' today we will work in cm

If YourScaleMode = "M" Then
    TWIPS = 567 ' metric
    TWIPS = 1440 ' inches
End If

' Connect to the destination of the picture
Dim objOutput As Object
Dim bTest As Boolean

bTest = True
If bTest Then
    Set objOutput = Picture1
    Set objOutput = Printer
    Printer.ScaleMode = vbTwips
    If cnkLandscape.Value = 1 Then ' check box to select landscape
        objOutput.Orientation = 2
        objOutput.Orientation = 1
    End If
End If

' Set output margins

Dim LeftMargin As Single
Dim RightMargin As Single
Dim TopMargin As Single
Dim BottomMargin As Single

' set margin postitions at 1 cm
LeftMargin = TWIPS * 1 ' Set position 1 cm from left
RightMargin = TWIPS * 1 ' set position 1 CM from top
TopMargin = TWIPS * 1
BottomMargin = TWIPS * 1

' set destination image size
Dim X1 As Single
Dim Y1 As Single
Dim W1 As Single
Dim H1 As Single

X1 = LeftMargin
Y1 = TopMargin

' now set Source cropping regions
Dim CropLeft As Single
Dim CropTop As Single
Dim CropRight As Single
Dim CropBottom As Single

CropLeft = TWIPS * 0.1 'val(txtCtopLeft) set these form a textbox or something
CropRight = TWIPS * 0.1
CropTop = TWIPS * 0.1
CropBottom = TWIPS * 0.1

' set source image size
Dim X2 As Single
Dim Y2 As Single
Dim W2 As Single
Dim H2 As Single

X2 = CropLeft
Y2 = CropTop
W2 = sngSourceWidth - CropLeft - CropRight
H2 = sngSourceHeight - CropTop - CropBottom

' Calculate Aspect Ratio using formula

Dim AspectRatio As Single
AspectRatio = H2 / W2

' calculate output available width
W1 = objOutput.ScaleWidth - LeftMargin - RightMargin
H1 = AspectRatio * W1

' make sure that the image will fit on the page
Dim AvailableHeight As Single
AvailableHeight = objOutput.ScaleHeight - TopMargin - BottomMargin

If H1 > AvailableHeight Then
    H1 = AvailableHeight
    W1 = (H1 * W2) / H2
End If

If X2 < 0 Or Y2 < 0 Or W2 < 0 Or H2 < 0 Then
    MsgBox "You have over cropped your image dummy!"
End If

' finally we are now ready to print the image
objOutput.PaintPicture Picture2.Image, X1, Y1, W1, H1, X2, Y2, W2, H2, vbSrcCopy

Open in new window


Author Comment

ID: 33734182
I thought of cropping the image as well by supplying values for  x2, y2, width2, height2. However, there is no way of determining these values considering, in order to crop, I would need to know the width and height of the slide's frame relative to overall width and height of the entire image. That information is not available within our VB6 application once the wmf is loaded into an Image object using LoadPicture.
LVL 17

Expert Comment

ID: 33734518
If the picturebox.autosize = true, the image will autosize when loaded so you get to know image width & height.

I presume that there is some kind of border around the slides. If the slide border is white, or a common format, if would be possible to calculate automatically using some crafty magic with the image pixels.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 17

Expert Comment

ID: 33734589
Perhaps you could upload an image and I could give you some code that you calculate the real image x2,y2,w2 & h2

Author Comment

ID: 33736721
The file has been uploaded. Please note, EE does not allow attachments of wmf file types, Hence, although the attached file is a wmf file, you will have to be rename it by replacing the word "dot" with a period ".".

Thanks for all of your help so far.

Accepted Solution

mic138 earned 0 total points
ID: 33799382
I am going to forego obtaining an answer since we have established a workaround with the user.

It seems as if PowerPoint (PW) does not obscure portions of an image out side of the slide's frame when the image is manipulated (in our case rotated) within the PW editor. If you manipulate the image, using graphics software, before pasting (straddling the image over the slide's border), then the portion of the image outside of the slide is obscured.
LVL 101

Expert Comment

ID: 34904991
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background   Certain code in VBA requires initialization, such as application events. The app initialization is often triggered by the Auto_Open sub which is a special procedure that runs when an add-in loads. More significantly, this sub does n…
Setting the Scene Animations in PowerPoint are a great tool to convey messages when used carefuly with the content of your slides. There are plenty of animation effects and options, including a Repeat feature for individual animation effects. …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question