Thank you.
I was able to scale the document using:
e.Graphics.ScaleTransform(0.95F, 0.95F)
In order to scale using Transform.Scale method I had to:
Dim myMatrix As New System.Drawing.Drawing2D.Matrix
myMatrix.Scale(0.95F, 0.95F)
e.Graphics.Transform = myMatrix
Is this the correct way of using Transform.Scale or am I missing the
concept somewhere?
Also I would really like to be able to use Mr. Wagners class for getting
the hard margins but keep getting a System.InvalidOperationException - The
object is currently in use elsewhere and just can't figure it out on my
own.
In stepping through the debugger I actually see the
System.InvalidOperationException error first comming up in the Locals
window on the Graphics object at:
Dim hDC As IntPtr = Graphics.GetHdc()
hDC is populated (with the printer handle I assume)
But no ErrorException is thrown until m.Left = CInt(ox * 100 /
Graphics.DpiX); then I get the "The object is currently in use elsewhere"
error.
Please help me to understand??!!!
Here is the code:
Imports System.Drawing.Printing
''' <summary>
''' Provides information about a device.
''' </summary>
Public Class Device
Private Declare Function GetDeviceCaps Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nIndex As Int32 _
) As Int32
Private Const PHYSICALOFFSETX As Int32 = 112 ' In device units.
Private Const PHYSICALOFFSETY As Int32 = 113 ' In device units.
Private Const HORZRES As Int32 = 8 ' In pixels/dots.
Private Const VERTRES As Int32 = 10 ' In pixels/dots.
''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="Graphics">The device's <c>Graphics</c> object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMargins( _
ByVal Graphics As Graphics _
) As Margins
Dim hDC As IntPtr
Try
hDC = Graphics.GetHdc() <========== Locals window shows
Graphics as System.InvalidOperationException, but no error is thrown
Catch ex As InvalidOperationException
End Try
Dim m As Margins = GetPhysicalMargins(hDC, Graphics)
Graphics.ReleaseHdc(hDC)
Return m
End Function
''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Handle to the device context.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Public Shared Function GetPhysicalMargins( _
ByVal hDC As IntPtr _
) As Margins
Dim g As Graphics = Graphics.FromHdc(hDC)
Dim m As Margins = GetPhysicalMargins(hDC, g)
g.Dispose()
Return m
End Function
''' <summary>
''' Gets a device's physical margins.
''' </summary>
''' <param name="hDC">Handle to the device context.</param>
''' <param name="Graphics">The device's <c>Graphics</c> object.</param>
''' <returns>
''' The device's physical margins in 0.001 inch units.
''' </returns>
Private Shared Function GetPhysicalMargins( _
ByVal hDC As IntPtr, _
ByVal Graphics As Graphics _
) As Margins
Dim m As New Margins
Dim ox As Int32 = GetDeviceCaps(hDC, PHYSICALOFFSETX)
Dim oy As Int32 = GetDeviceCaps(hDC, PHYSICALOFFSETY)
With m
.Left = CInt(ox * 100 / Graphics.DpiX) <==========
System.InvalidOperationException - The object is currently in use
elsewhere - ERROR IS THROWN HERE
.Top = CInt(oy * 100 / Graphics.DpiY)
.Right = _
CInt( _
(ox + GetDeviceCaps(hDC, HORZRES)) * 100 / _
Graphics.DpiX _
)
.Bottom = _
CInt( _
(oy + GetDeviceCaps(hDC, VERTRES)) * 100 / _
Graphics.DpiY _
)
End With
Return m
End Function
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New PrinterSettings
For Each PrinterName As String In PrinterSettings.InstalledPrinters
p.PrinterName = PrinterName
Dim g As Graphics = p.CreateMeasurementGraphics()
MsgBox( _
PrinterName & ControlChars.NewLine & _
Device.GetPhysicalMargins(g).ToString() & "[0.01 inch]" _
)
g.Dispose()
Next PrinterName
End Sub
End Class
Ron Allen said:
Just call the Graphics.Transform.Scale method on the Graphics being
used and specify the x and y scaling factors and everything will be done
for you as far as shrinking the document.
Ron Allen