1. 程式人生 > >VBNet WinForm如何開發一個照片自動排版程式

VBNet WinForm如何開發一個照片自動排版程式

想要開發一個照片自動排版程式要熟悉一下幾個知識點: 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以上,否則看起來矩形畫素點很嚴重

原始碼後面附上.