想要开发一个照片自动排版程序要熟悉一下几个知识点:
System.Drawing.Printing.PrintDocument.Print涉及到的纸张设置是英寸相关,我们常用的长度单位是毫米,那么先需要将毫米转化为英寸,1mm=0.039370078740157英寸,要打印一张A4纸的纸张(210*297) 的英寸的方法为:
Dim mmToInch As Double = 0.039370078740157
PrintDocument1.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize(PrintDocument1.DocumentName, 210*mmToInch *100, 297*mmToInch *100)
DGI+将图片绘制到打印机的方法
Imports System.Windows.Forms
Imports System.Drawing
Namespace DCM_Print
Public Class C_PrintImage
Public PrintImg As Image
Dim WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
Public Sub New(PrintImg As Image)
Me.PrintImg = PrintImg
End Sub
''' <summary>
''' 打印图片到打印机
''' </summary>
''' <param name="PapperSize_Width">纸张宽度(mm)</param>
''' <param name="PapperSize_Height">纸张高度(mm)</param>
''' <param name="PrinterName">打印机名字,为空代表默认打印机</param>
''' <remarks></remarks>
Public Sub Print(PapperSize_Width As Single, PapperSize_Height As Single, Optional PrinterName As String = "")
PrintDocument1 = New System.Drawing.Printing.PrintDocument
PrintDocument1.DocumentName = "图片打印"
Dim PrintPriview As PrintPreviewDialog
PrintPriview = New PrintPreviewDialog
PrintPriview.Document = PrintDocument1
PrintDocument1.DefaultPageSettings.Margins.Left = 0
PrintDocument1.DefaultPageSettings.Margins.Top = 0
PrintDocument1.DefaultPageSettings.Margins.Right = 0
PrintDocument1.DefaultPageSettings.Margins.Bottom = 0
PrintDocument1.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize(PrintDocument1.DocumentName, PapperSize_Width * DCM.mmToInch * 100, PapperSize_Height * DCM.mmToInch * 100)
PrintDocument1.PrinterSettings.PrinterName = PrinterName
Dim prnres As New System.Drawing.Printing.PrinterResolution
prnres.Kind = Drawing.Printing.PrinterResolutionKind.High
PrintDocument1.DefaultPageSettings.PrinterResolution = prnres
Dim myCopies As Integer = 1
PrintDocument1.PrinterSettings.Copies = myCopies
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage1(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim G As Graphics = e.Graphics
G.DrawImage(PrintImg, 0, 0, e.MarginBounds.Width, e.MarginBounds.Height)
End Sub
End Class
End Namespace
照片标准尺寸大小
Public Function CreateDT_照片尺寸() As DataTable
Dim dt As New DataTable
DCM.DataTableColumnAdd(dt, "大小", GetType(String))
DCM.DataTableColumnAdd(dt, "宽", GetType(Single))
DCM.DataTableColumnAdd(dt, "高", GetType(Single))
DCM.DataTableColumnAdd(dt, "备注", GetType(String))
With dt
.Rows.Add("1寸", "2.5", "3.5", "证件照")
.Rows.Add("2寸", "3.5", "4.9", "标准2寸照片")
.Rows.Add("大1寸", "3.3", "4.8", "中国护照")
.Rows.Add("5寸/3R", "12.7", "8.9", "最常见的照片大小")
.Rows.Add("6寸/4R", "15.2", "10.2", "国际上比较通用的照片大小")
.Rows.Add("7寸/5R", "17.8", "12.7", "放大")
.Rows.Add("8寸", "15.2", "20.3", "大概是A4打印纸的一半")
.Rows.Add("小12寸", "20.3", "30.5", "大概是A4大小")
.Rows.Add("12寸", "25.4", "30.5", "大图")
End With
Return dt
End Function
打印纸张国际标准尺寸
Private Function CreateDT_打印尺寸() As DataTable
Dim dt As New DataTable
DCM.DataTableColumnsAdd(dt, {"大小", "尺寸"})
With dt
.Rows.Add("A0", "841*1189")
.Rows.Add("A1", "594*841")
.Rows.Add("A2", "420*594")
.Rows.Add("A3", "297*420")
.Rows.Add("A4", "210*297")
.Rows.Add("A5", "148*210")
End With
Return dt
End Function
要创建一张A4大小,分辨率为300(像素/英寸)的图片大小
宽=210×0.039370078740157×300=2480
高=297×0.039370078740157×300=3580
PS文件创建为证:
最后就是我们将图片载入到界面来,通过计算,将不同大小的图片画到一张打印的图片上来,然后通过图片打印功能打印到打印机里面就行了(当然,为了提高打印效率,我们完全可以讲图片通过打印文档直接排版,有兴趣的小伙伴可以自己修改),
注意,
- 载入的照片必须按照照片尺寸比例进行裁剪,否则打印出来的照片就是拉扯的,很难看.所以添加照片时需要给用户提供裁剪功能,如果用户还需要用PS裁剪的话,你这软件还有什么意义.
- 为了裁剪图片方便,排版计算时,高度不一的图片排在一排时,要取最高高度图片最为下一排图片的起始位置
- 排版时上下左右预留空白位置,打印图片时就不需要预留位置了
- 有些人的照片可能有白色背景,打在底片上,无法确定边缘位置,所以建议增加对照片描边的功能
- 打印的图片提供生产图片功能,方便用户转移打印机
- 经过测试,照片打印的分辨率最好是300以上,否则看起来矩形像素点很严重
源码附上,https://download.csdn.net/download/meiren777/10765331
注意:该源码涉及到的DCM,DCM_Control,DCM_HttpListen另行下载
https://download.csdn.net/download/meiren777/10765375
界面展示