Scaling a form to fit to page

mtaylor314

Member
Joined
Nov 30, 2006
Messages
19
Location
Cleves, OH
Programming Experience
Beginner
I have code that takes a snapshot of a form to send to a printer.
VB.NET:
        Dim g As Graphics = Me.CreateGraphics()
        Dim s As Size = Me.Size
        memoryImage = New Bitmap(s.Width, s.Height, g)
        Dim mg As Graphics = Graphics.FromImage(memoryImage)
        Dim dc1 As IntPtr = g.GetHdc
        Dim dc2 As IntPtr = mg.GetHdc
        ' added code to compute and capture the form
        ' title bar and borders
        Dim widthDiff As Integer = _
           (Me.Width - Me.ClientRectangle.Width)
        Dim heightDiff As Integer = _
           (Me.Height - Me.ClientRectangle.Height)
        Dim borderSize As Integer = widthDiff \ 2
        Dim heightTitleBar As Integer = heightDiff - borderSize
        BitBlt(dc2, 0, 0, _
           Me.ClientRectangle.Width + widthDiff, _
           Me.ClientRectangle.Height + heightDiff, dc1, _
           0 - borderSize, 0 - heightTitleBar, 13369376)

        g.ReleaseHdc(dc1)
        mg.ReleaseHdc(dc2)
What I need now is a way to blow up or shrink the image to make it a page size. Some forms are small and some are larger. Thank you for your help.
 
You have a form snapshot rectangle/size maybe 150*250, and you have a MarginBounds rectangle in PrintDocument.PrintPage event perhaps 400*800.

width ratio is 400/150 = 2.67
height ratio is 800/250 = 3.2

You are here limited by the width ratio, so you can scale up your image by 2.67 which gives a new image size of 400*667. (which fits the imagined 400*800 page)

The calculations used here is always true and will always make your image fit the container if you use this algoritm.

Sum it up: calculate both dimensions ratio (available width divided by image width, available height divided by image height), choose the minimum ratio value and multiply it with both images dimensions.

Graphics.DrawImage used in PrintPage can be specified the target rectangle (ex: 0,0,400,667) which will draw the image streched.

Getting the screen copy of form can also be done with only Framework methods, see the GetSnap function in post 2 of this thread here: http://www.vbdotnetforums.com/showthread.php?t=14626
 
I was able to implement that and I appreciate the help. I also need it to print as landscape but I do not want to client to be able to choose. Is there any setting or property that will make the image print landscape?
 
Back
Top