Solved

Image control not shrinking

Posted on 2012-03-16
6
314 Views
Last Modified: 2012-05-12
My report is printing all images the same height even though my VBA code adjusts the height for each one. Here are the details.

My report contains a subform which contains an image control. It links to a graphic file. The filename changes for each record in report. In the subform's Detail_Format procedure, VBA code adjusts the height and width properties of the image based on values (in inches) stored in text boxes in the subform. The textboxes are bound to two fields in the underlying table that stores the desired height and width of the image when it prints. My report has images with different desired height and width values. However, they are all 5 inches (i.e., 7200 twips) tall in the report printout. There is a lot of white space below the small images. For example, a 3-inch high image has two inches of white space below it.

I have confirmed that the Shrink property is set to Yes for the image control, subform control, detail section for the subform (where the image control is located), and the section in the main form where the subform is located.

The code is below for the subform's detail_format event.
--------------------------------
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    Const clngTwipsPerInch As Long = 1440
    Const clngMaxImageWidth As Long = 9360 'in twips, 9360 twips = 6.5 inches
    Const clngMaxImageHeight As Long = 9360 'in twips, 9360 twips = 6.5 inches
   
    Dim lngImageWidth As Long 'in twips
    Dim lngImageHeight As Long 'in twips
    Dim dblImageRatio As Double

    lngImageWidth = clngTwipsPerInch * Me.txtStemImageWidth
    lngImageHeight = clngTwipsPerInch * Me.txtStemImageHeight

    'If the image is too wide for the page, then shrink it to fit.
    If lngImageWidth > clngMaxImageWidth Then
        dblImageRatio = clngMaxImageWidth / lngImageWidth
        lngImageWidth = dblImageRatio * lngImageWidth
        lngImageHeight = dblImageRatio * lngImageHeight
    End If

    'If the image (even after shrinking for width) is too long, then shrink to fit.
    If lngImageHeight > clngMaxImageHeight Then
        dblImageRatio = clngMaxImageHeight / lngImageHeight
        lngImageWidth = dblImageRatio * lngImageWidth
        lngImageHeight = dblImageRatio * lngImageHeight
    End If

    Me.imgStemGraphic.Width = lngImageWidth
    Me.imgStemGraphic.Height = lngImageHeight

    Me.imgStemGraphic.Picture = Me.txtItemFileName
   
    Debug.Print "Item " & Me.Parent.ItemID & _
        ", Seq = " & Me.Parent.txtItemSeq & _
        ",  Image = " & Me.imgStemGraphic.Width & _
        " W   x  " & Me.imgStemGraphic.Height & " H"

End Sub
----------------------------------

Here is the output of the debug.print statement above.
--------------------------
Item 700012, Seq = 34.,  Image = 5573 W   x  4176 H
Item 700012, Seq = 34.,  Image = 5573 W   x  4176 H
Item 700024, Seq = 96.,  Image = 9360 W   x  7369 H
Item 700024, Seq = 96.,  Image = 9360 W   x  7369 H
Item 700024, Seq = 96.,  Image = 9360 W   x  7369 H
---------------------------

This report contains two images. Each of the two item records has one image. I don't know whether this is relevant, but notice that the two item records are formatted a different number of times. The first one is formatted twice whereas the second one is formatted three times.

So, why does Access add white space below each graphic so that they are all 5 inches (7200 twips) high--even though their height property values differ?
0
Comment
Question by:gordonwwaugh
  • 3
  • 3
6 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37730517
This is why I simply set the size of the Image control to be a good size for all images. (and to fit nicely in the report)
Then I just set the sizemode property of the image control to "Zoom".
(to keep the aspect ratio for each image)
This way a 11X17 image is the same size on the report as a 5X7 image.

I also (if possible and practical), try to define a resolution and size for the images.
In other words, people don't just "Take Pictures" and give them to me,...
I tell them that the Pic should be at least 300dpi, ...etc.
Then, (if needed) the images are re-sized in a graphics application, so that they are all "roughly" the same Size, regardless of the resolution. (Again, if this is practical/possible)

(To me at least) Trying to adjust the image size dynamically at runtime (as you are trying to do here) is just not worth the trouble IMHO...

Let's see what other Experts may post...


JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37730553
There are just too many factors that might contribute to this issue:...

Does this work when you "keep it Simple" and just do this in a single Report?

For me it is extremely difficult to troubleshoot issues like this without a sample dB.
Sample database notes:
1. Back up your database(s).
2. Combine the front and back ends into one database file.
3. Remove any startup options, unless they are relevant to the issue.
4. Remove any records unless they are relevant to the issue.
5. Delete any objects that do not relate directly to the issue.
6. Remove any references to any "linked" files (files outside of the database, Images, OLE Files, ...etc)
7. Remove any references to any third party Active-x Controls (unless they are relevant to the issue)
8. Remove, obfuscate, encrypt, or otherwise disguise, any sensitive data.
9. Compile the code. (From the VBA code window, click: Debug-->Compile)
10. Run the compact/Repair utility.
11. Remove any Passwords and/or security.
12. If a form is involved in the issue, set the Modal and Popup properties to: No
    (Again, unless these properties are associated with the issue)
13. Post the explicit steps to replicate the issue.
14. Test the database before posting.

In other words, ...post a database that we can easily open and immediately see and/or troubleshoot the issue.
And if applicable, also include a clear graphical representation of the *Exact* results you are expecting, based on the sample data.

JeffCoachman
0
 

Author Comment

by:gordonwwaugh
ID: 37795793
Sorry for the delay. I will create the debugging database requested in the next couple of days.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Accepted Solution

by:
gordonwwaugh earned 0 total points
ID: 37938189
Okay. I have an important correction to my description. Image controls do not have shrink property or a grow property. So, Access is not doing anything unexpected. The VBA code is changing the size of the graphic correctly.

I figured out how to do this. I added some VBA code that moves down the objects below the graphic. In my situation, I have only one object below the graphic: me.txtBanner. Here is the modified code.

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    Const clngTwipsPerInch As Long = 1440
    Const clngMaxImageWidth As Long = 9360 'in twips, 9360 twips = 6.5 inches
    Const clngMaxImageHeight As Long = 9360 'in twips, 9360 twips = 6.5 inches
    Const clngPadding As Long = 288 'in twips, 288 twips = 0.2 inches
   
    Dim lngImageWidth As Long 'in twips
    Dim lngImageHeight As Long 'in twips
    Dim dblImageRatio As Double
    Dim lngDownwardAdjustment As Double 'in twips

    lngImageWidth = clngTwipsPerInch * Me.txtStemImageWidth
    lngImageHeight = clngTwipsPerInch * Me.txtStemImageHeight

    'If the image is too wide for the page, then shrink it to fit.
    If lngImageWidth > clngMaxImageWidth Then
        dblImageRatio = clngMaxImageWidth / lngImageWidth
        lngImageWidth = dblImageRatio * lngImageWidth
        lngImageHeight = dblImageRatio * lngImageHeight
    End If

    'If the image (even after shrinking for width) is too long, then shrink to fit.
    If lngImageHeight > clngMaxImageHeight Then
        dblImageRatio = clngMaxImageHeight / lngImageHeight
        lngImageWidth = dblImageRatio * lngImageWidth
        lngImageHeight = dblImageRatio * lngImageHeight
    End If

    Me.imgStemGraphic.Width = lngImageWidth
    Me.imgStemGraphic.Height = lngImageHeight

    Me.imgStemGraphic.Picture = Me.txtItemFilename
   
    Me.txtBanner.Top = Me.imgStemGraphic.Height + Me.imgStemGraphic.Top + clngPadding
   

End Sub
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37938642
OK, great.

AFAICT,  you can accept your own post as the solution...

;-)

Jeff
0
 

Author Closing Comment

by:gordonwwaugh
ID: 37960003
No solutions were offered. I figured out the solution myself.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now