Advertisement
Guest User

Scale Printing

a guest
Sep 26th, 2011
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Drawing
  2. Imports System.Reflection
  3. Imports System.Drawing.Printing
  4. Imports System.Windows.Forms
  5. Imports System.Runtime.InteropServices
  6. Imports System.IO
  7.  
  8. Public Class clsPrinting
  9.  
  10.     Private printDoc As PrintDocument
  11.     Private printSettings As PrinterSettings
  12.     Private imageFilename As String = ""
  13.  
  14.     Public Property Preview As Boolean = False
  15.     Public Property Documentname As String = "Untitled document"
  16.     Public Property Landscape As Boolean
  17.         Get
  18.             Return printSettings.DefaultPageSettings.Landscape
  19.         End Get
  20.         Set(value As Boolean)
  21.             printSettings.DefaultPageSettings.Landscape = value
  22.         End Set
  23.     End Property
  24.     Public Sub SetMinimumMargins()
  25.         printSettings.DefaultPageSettings.Margins.Left = CInt(printSettings.DefaultPageSettings.HardMarginX) + 1
  26.         printSettings.DefaultPageSettings.Margins.Top = CInt(printSettings.DefaultPageSettings.HardMarginY) + 1
  27.         printSettings.DefaultPageSettings.Margins.Right = CInt(printSettings.DefaultPageSettings.HardMarginX) + 1
  28.         printSettings.DefaultPageSettings.Margins.Bottom = CInt(printSettings.DefaultPageSettings.HardMarginY) + 1
  29.     End Sub
  30.  
  31.     Public Sub New()
  32.         MyBase.New()
  33.         printSettings = New PrinterSettings
  34.     End Sub
  35.  
  36.     Public Function ShowPrintDialog() As Boolean
  37.         Try
  38.             Dim pd As New PrintDialog
  39.             pd.PrinterSettings = printSettings
  40.             If pd.ShowDialog = Windows.Forms.DialogResult.OK Then
  41.                 printSettings = pd.PrinterSettings
  42.                 Return True
  43.             Else
  44.                 Return False
  45.             End If
  46.         Catch ex As Exception
  47.             VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
  48.             Return False
  49.         End Try
  50.     End Function
  51.  
  52.     ''' <summary>
  53.    ''' Prints an image file to full screen on the selected printer
  54.    ''' </summary>
  55.    ''' <param name="imageFile"></param>
  56.    ''' <remarks></remarks>
  57.    Public Sub PrintImage(ByVal imageFile As String)
  58.         Try
  59.             printDoc = New PrintDocument
  60.             AddHandler printDoc.PrintPage, AddressOf printDoc_PrintImage
  61.             imageFilename = imageFile
  62.             With printDoc
  63.                 .PrinterSettings = printSettings
  64.                 .DocumentName = _Documentname
  65.                 If _Preview Then
  66.                     Dim prv As New PrintPreviewDialog
  67.                     prv.Document = printDoc
  68.                     prv.Width = Screen.PrimaryScreen.WorkingArea.Width
  69.                     prv.Height = Screen.PrimaryScreen.WorkingArea.Height
  70.                     prv.ShowDialog()
  71.                 Else
  72.                     .Print()
  73.                 End If
  74.             End With
  75.         Catch ex As Exception
  76.             VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
  77.         End Try
  78.     End Sub
  79.  
  80.     Private Sub printDoc_PrintImage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
  81.         Try
  82.             Dim DpiX As Single = 0, DpiY As Single = 0
  83.             Dim bmp As Bitmap = CType(Bitmap.FromFile(imageFilename), Bitmap)
  84.             Dim imgSizeRatio As Double = bmp.Width / bmp.Height
  85.             Dim printBitmap As Bitmap
  86.  
  87.             With printSettings.DefaultPageSettings
  88.  
  89.                 Dim pageWidth As Single = .Bounds.Width - .Margins.Left - .Margins.Right
  90.                 Dim pageHeight As Single = .Bounds.Height - .Margins.Top - .Margins.Bottom
  91.                 Dim pageSizeRatio As Double = 0
  92.  
  93.                 GetImageDPI(imageFilename, DpiX, DpiY)
  94.  
  95.                 pageSizeRatio = (pageWidth / pageHeight)
  96.  
  97.                 If imgSizeRatio > pageSizeRatio Then
  98.                     'scale to image width
  99.                    printBitmap = ResizeImage(bmp, pageWidth / bmp.Width, DpiX, DpiY)
  100.                 Else
  101.                     'scale to image height
  102.                    printBitmap = ResizeImage(bmp, pageHeight / bmp.Height, DpiX, DpiY)
  103.                 End If
  104.                 printBitmap.Save("C:\scratch\outputfile.png", Imaging.ImageFormat.Png)
  105.                 e.Graphics.DrawImage(printBitmap, .Margins.Left, .Margins.Top)
  106.             End With
  107.  
  108.             e.HasMorePages = False
  109.         Catch ex As Exception
  110.             VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
  111.         End Try
  112.     End Sub
  113.  
  114.     ''' <summary>
  115.    ''' Finds the Dots Per Inch of an Image File
  116.    ''' </summary>
  117.    ''' <param name="filename"></param>
  118.    ''' <param name="dpiX"></param>
  119.    ''' <param name="dpiY"></param>
  120.    ''' <remarks>Returns 96DPI if an error occurs as this is the most common value</remarks>
  121.    Private Sub GetImageDPI(ByVal filename As String, ByRef dpiX As Single, ByRef dpiY As Single)
  122.         Try
  123.             Dim workingImage As Image = Image.FromFile(imageFilename)
  124.             Dim TempImg As Image = New Bitmap(workingImage.Width, workingImage.Height)
  125.             Dim ImageDrawer As Graphics = Graphics.FromImage(TempImg)
  126.             dpiX = ImageDrawer.DpiX
  127.             dpiY = ImageDrawer.DpiY
  128.         Catch ex As Exception
  129.             dpiX = 96
  130.             dpiY = 96
  131.         End Try
  132.     End Sub
  133.  
  134.     ''' <summary>
  135.    ''' Resizes an image using the specified scaling factor whilst maintaining the images aspect ratio
  136.    ''' </summary>
  137.    ''' <param name="img"></param>
  138.    ''' <param name="scaleFactor"></param>
  139.    ''' <param name="dpiX"></param>
  140.    ''' <param name="dpiY"></param>
  141.    ''' <returns></returns>
  142.    ''' <remarks></remarks>
  143.    Private Function ResizeImage(ByRef img As Bitmap, ByVal scaleFactor As Double, ByVal dpiX As Single, ByVal dpiY As Single) As Bitmap
  144.         Try
  145.             Dim imgDest As New Bitmap(CInt(img.Width * scaleFactor * dpiX / 100), CInt(img.Height * scaleFactor * dpiY / 100))
  146.  
  147.             ' Make a Graphics object for the result Bitmap.
  148.            Using grDest As Graphics = Graphics.FromImage(imgDest)
  149.                 ' Copy the source image into the destination bitmap.
  150.                grDest.DrawImage(img, 0, 0, imgDest.Width, imgDest.Height)
  151.             End Using
  152.  
  153.             Return imgDest
  154.         Catch ex As Exception
  155.             VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
  156.             Return Nothing
  157.         End Try
  158.     End Function
  159.  
  160. End Class
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement