1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
'2 exemple identique au 1er sauf que format papier imprimante est definit lors du passage dans
'dans l'event QueryPageSettings lors du 1er passage.....
'pour eviter une boucle infinie car l'event querySetting comme l'event PrintPage
'sont appeles a chaque impression d'une page....
'un boolean FirstPassage est utilise pour desactiver les appels suivants
'de CroopImage
'AJOUTER LES CONTROLS:
'-4 TEXTEBOX
'-1 PRINTDOCUMENT
'-1 PRINTPREVIEW
'-1 PICTUREBOX
'-2 BOUTONS
Imports System.Drawing.Drawing2D
Imports System.Drawing.Printing
Public Class frmPrintImageCustomSize
'counter:numero de page à imprimer
Private numPage As Integer = 0
'total pages à imprimer
Private totalPages As Integer = 0
'liste des sous-images croppees taille reelle
Private ListPagesImage As List(Of Bitmap)
'dimension "reelles" de l'image en inch
Private widthOriginalImage As Single
Private heightOriginalImage As Single
'RAJOUT DU BOOLEAN FirstPassage
Private FirstPassage As Boolean
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
' Pour connaitre la taille reelle d'image "sortie sur papier"
' à partir des dimensions (width et height) donnees en pixel d'une image et
' des resolutions Horizontale et Verticale de l'image donnees
' en "Pixel /Inch" (Inch =>pouce en francais)
'
' Size.Width(unite inch)=Size.Width(unite pixel)/ResH(unite:pixel/inch)
'
' Dimensions sont converties en mm car un "Inch" vaut 25.4 mm
' Formule sera reutilisee en sens "inverse" lors du "cropping" plus loin.
' Real Width in inch
widthOriginalImage = (Me.PictureBox1.Image.Width / Me.PictureBox1.Image.HorizontalResolution)
' Display Real Width in mm
Me.textBoxRealWidth.Text = (widthOriginalImage * 25.4).ToString
' Real Width in inch
heightOriginalImage = (Me.PictureBox1.Image.Height / Me.PictureBox1.Image.VerticalResolution)
'Display Real Height in mm pour la "satisfaction"
Me.textBoxRealHeight.Text = (heightOriginalImage * 25.4).ToString
'initialise variables pour l'impression pour gerer plusieurs pages !
numPage = 0
' Image Originale sera "croppee" en une serie d'image
' dont la taille correspondra à un format A4
' sous-images "croppees" à imprimer seront stockees dans ListPagesImage
FirstPassage = False
End Sub
Private Sub CroopImageBis(ByVal myPaperSize As PaperSize)
' taille papier est envoye par event querysettins cette fois
If myPaperSize Is Nothing Then Return
Dim myPaperSizeReal As PaperSize = myPaperSize
'affiche taille en mm pour la "satisfaction"
Me.textBoxWidthPapier.Text = (myPaperSizeReal.Width * 25.4).ToString
Me.textBoxHeightPapier.Text = myPaperSizeReal.Height * 25.4.ToString
' convertit taille papier en pixel
' rappel :resolution ecran par default 96 dpi.
Dim myPaperSizePixel As Size
myPaperSizePixel.Width = myPaperSizeReal.Width * 96
myPaperSizePixel.Height = myPaperSizeReal.Height * 96
'Original Image stockee sur le picturebox
Dim OriginalImage As Image = Me.PictureBox1.Image
'cotes de la grille de decoupage ou "cropping"
Dim nbSideW As Integer = OriginalImage.Width / myPaperSizePixel.Width
Dim nbSideH As Integer = OriginalImage.Height / myPaperSizePixel.Height
ListPagesImage = New List(Of Bitmap)
Dim x1 As Integer = 0
Dim y1 As Integer = 0
Dim grCropBitmap As Graphics = Nothing
For j As Integer = 1 To nbSideH
For i As Integer = 1 To nbSideW
Dim CropRect As Rectangle = New Rectangle(x1, y1, myPaperSizePixel.Width, myPaperSizePixel.Height)
Dim CropBitmap As Bitmap = New Bitmap(CropRect.Width, CropRect.Height)
'dessine un sous-bitmap croppe
grCropBitmap = Graphics.FromImage(CropBitmap)
grCropBitmap.DrawImage(OriginalImage, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
ListPagesImage.Add(CropBitmap)
x1 = x1 + myPaperSizePixel.Width
Next
x1 = 0
y1 = y1 + myPaperSizePixel.Height
Next
'libere resources
If grCropBitmap IsNot Nothing Then
grCropBitmap.Dispose()
End If
OriginalImage = Nothing
'compute total pages
totalPages = Me.ListPagesImage.Count - 1
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docToPrint.PrintPage
'no pages to print.
If totalPages = 0 Then Return
' Specify units CoordinateSpace.Page in Pixel
e.Graphics.PageUnit = GraphicsUnit.Pixel
' Specify PageScale
e.Graphics.PageScale = 1.0
Dim BmpToPrint As Bitmap = Me.ListPagesImage(numPage)
e.Graphics.DrawImage(BmpToPrint, 0, 0)
numPage = numPage + 1
' Test for last page
e.HasMorePages = (numPage < totalPages)
End Sub
Private Sub docToPrint_QueryPageSettings(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles docToPrint.QueryPageSettings
'Exemple papier custom de 150x150 mm
Dim myPaperSize As PaperSize = New PaperSize
'specifier RawKind=PaperKind.Custom pour taille personnalise
myPaperSize.RawKind = PaperKind.Custom
'taille en millimetres doit etre converties en inch(divise par 25.4)
myPaperSize.Width = (150) / 25.4
'ensuite change en centieme de inch
myPaperSize.Width = myPaperSize.Width * 100
'Idem hauteur
myPaperSize.Height = (150) / 25.4
myPaperSize.Height = myPaperSize.Height * 100
'"Sette" taille papier d'imprimante
e.PageSettings.PaperSize = myPaperSize
'Affiche taille en "centieme inch"
Me.textBoxWidthPapier.Text = myPaperSize.Width.ToString
Me.textBoxHeightPapier.Text = myPaperSize.Height.ToString
'LE FLAG FirstPassage
If Not FirstPassage Then
'reprend papersize en inch pour CroopImageBis pour eviter
'de nombreuse conversions
myPaperSize.Width = (150) / 25.4
myPaperSize.Height = (150) / 25.4
CroopImageBis(myPaperSize)
FirstPassage = True
End If
End Sub
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
PrintPreviewDialog1.Document = Me.docToPrint
PrintPreviewDialog1.Show()
End Sub
End Class |
Partager