Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Drawing
- Imports System.Reflection
- Imports System.Drawing.Printing
- Imports System.Windows.Forms
- Imports System.Runtime.InteropServices
- Imports System.IO
- Public Class clsPrinting
- Private printDoc As PrintDocument
- Private printSettings As PrinterSettings
- Private imageFilename As String = ""
- Public Property Preview As Boolean = False
- Public Property Documentname As String = "Untitled document"
- Public Property Landscape As Boolean
- Get
- Return printSettings.DefaultPageSettings.Landscape
- End Get
- Set(value As Boolean)
- printSettings.DefaultPageSettings.Landscape = value
- End Set
- End Property
- Public Sub SetMinimumMargins()
- printSettings.DefaultPageSettings.Margins.Left = CInt(printSettings.DefaultPageSettings.HardMarginX) + 1
- printSettings.DefaultPageSettings.Margins.Top = CInt(printSettings.DefaultPageSettings.HardMarginY) + 1
- printSettings.DefaultPageSettings.Margins.Right = CInt(printSettings.DefaultPageSettings.HardMarginX) + 1
- printSettings.DefaultPageSettings.Margins.Bottom = CInt(printSettings.DefaultPageSettings.HardMarginY) + 1
- End Sub
- Public Sub New()
- MyBase.New()
- printSettings = New PrinterSettings
- End Sub
- Public Function ShowPrintDialog() As Boolean
- Try
- Dim pd As New PrintDialog
- pd.PrinterSettings = printSettings
- If pd.ShowDialog = Windows.Forms.DialogResult.OK Then
- printSettings = pd.PrinterSettings
- Return True
- Else
- Return False
- End If
- Catch ex As Exception
- VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
- Return False
- End Try
- End Function
- ''' <summary>
- ''' Prints an image file to full screen on the selected printer
- ''' </summary>
- ''' <param name="imageFile"></param>
- ''' <remarks></remarks>
- Public Sub PrintImage(ByVal imageFile As String)
- Try
- printDoc = New PrintDocument
- AddHandler printDoc.PrintPage, AddressOf printDoc_PrintImage
- imageFilename = imageFile
- With printDoc
- .PrinterSettings = printSettings
- .DocumentName = _Documentname
- If _Preview Then
- Dim prv As New PrintPreviewDialog
- prv.Document = printDoc
- prv.Width = Screen.PrimaryScreen.WorkingArea.Width
- prv.Height = Screen.PrimaryScreen.WorkingArea.Height
- prv.ShowDialog()
- Else
- .Print()
- End If
- End With
- Catch ex As Exception
- VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
- End Try
- End Sub
- Private Sub printDoc_PrintImage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
- Try
- Dim DpiX As Single = 0, DpiY As Single = 0
- Dim bmp As Bitmap = CType(Bitmap.FromFile(imageFilename), Bitmap)
- Dim imgSizeRatio As Double = bmp.Width / bmp.Height
- Dim printBitmap As Bitmap
- With printSettings.DefaultPageSettings
- Dim pageWidth As Single = .Bounds.Width - .Margins.Left - .Margins.Right
- Dim pageHeight As Single = .Bounds.Height - .Margins.Top - .Margins.Bottom
- Dim pageSizeRatio As Double = 0
- GetImageDPI(imageFilename, DpiX, DpiY)
- pageSizeRatio = (pageWidth / pageHeight)
- If imgSizeRatio > pageSizeRatio Then
- 'scale to image width
- printBitmap = ResizeImage(bmp, pageWidth / bmp.Width, DpiX, DpiY)
- Else
- 'scale to image height
- printBitmap = ResizeImage(bmp, pageHeight / bmp.Height, DpiX, DpiY)
- End If
- printBitmap.Save("C:\scratch\outputfile.png", Imaging.ImageFormat.Png)
- e.Graphics.DrawImage(printBitmap, .Margins.Left, .Margins.Top)
- End With
- e.HasMorePages = False
- Catch ex As Exception
- VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
- End Try
- End Sub
- ''' <summary>
- ''' Finds the Dots Per Inch of an Image File
- ''' </summary>
- ''' <param name="filename"></param>
- ''' <param name="dpiX"></param>
- ''' <param name="dpiY"></param>
- ''' <remarks>Returns 96DPI if an error occurs as this is the most common value</remarks>
- Private Sub GetImageDPI(ByVal filename As String, ByRef dpiX As Single, ByRef dpiY As Single)
- Try
- Dim workingImage As Image = Image.FromFile(imageFilename)
- Dim TempImg As Image = New Bitmap(workingImage.Width, workingImage.Height)
- Dim ImageDrawer As Graphics = Graphics.FromImage(TempImg)
- dpiX = ImageDrawer.DpiX
- dpiY = ImageDrawer.DpiY
- Catch ex As Exception
- dpiX = 96
- dpiY = 96
- End Try
- End Sub
- ''' <summary>
- ''' Resizes an image using the specified scaling factor whilst maintaining the images aspect ratio
- ''' </summary>
- ''' <param name="img"></param>
- ''' <param name="scaleFactor"></param>
- ''' <param name="dpiX"></param>
- ''' <param name="dpiY"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Private Function ResizeImage(ByRef img As Bitmap, ByVal scaleFactor As Double, ByVal dpiX As Single, ByVal dpiY As Single) As Bitmap
- Try
- Dim imgDest As New Bitmap(CInt(img.Width * scaleFactor * dpiX / 100), CInt(img.Height * scaleFactor * dpiY / 100))
- ' Make a Graphics object for the result Bitmap.
- Using grDest As Graphics = Graphics.FromImage(imgDest)
- ' Copy the source image into the destination bitmap.
- grDest.DrawImage(img, 0, 0, imgDest.Width, imgDest.Height)
- End Using
- Return imgDest
- Catch ex As Exception
- VizualLib.clsErrorHandling.ThrowException(Me.ToString, MethodBase.GetCurrentMethod().Name, ex.Message)
- Return Nothing
- End Try
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement