⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printbitmapform.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.

    Private WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Private WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
    Private WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
    Private WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Private WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Private WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem14 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem15 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem16 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem17 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem18 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem19 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem20 As System.Windows.Forms.MenuItem
    Private WithEvents MenuItem21 As System.Windows.Forms.MenuItem
    Private WithEvents ProcessRotateLeft As System.Windows.Forms.MenuItem
    Friend WithEvents ProcessRotateRight As System.Windows.Forms.MenuItem
    Friend WithEvents ZoomIn As System.Windows.Forms.MenuItem
    Friend WithEvents ZoomOut As System.Windows.Forms.MenuItem
    Private WithEvents ZoomAuto As System.Windows.Forms.MenuItem
    Private WithEvents ZoomMenu As System.Windows.Forms.MenuItem
    Private WithEvents ImageExit As System.Windows.Forms.MenuItem
    Private WithEvents ProcessMenu As System.Windows.Forms.MenuItem
    Private WithEvents ImageMenu As System.Windows.Forms.MenuItem
    Private WithEvents ImageLoad As System.Windows.Forms.MenuItem
    Private WithEvents ImagePrint As System.Windows.Forms.MenuItem
    Friend WithEvents ZoomNormal As System.Windows.Forms.MenuItem

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.ProcessRotateRight = New System.Windows.Forms.MenuItem()
        Me.ZoomIn = New System.Windows.Forms.MenuItem()
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
        Me.ZoomMenu = New System.Windows.Forms.MenuItem()
        Me.ZoomAuto = New System.Windows.Forms.MenuItem()
        Me.ZoomNormal = New System.Windows.Forms.MenuItem()
        Me.ZoomOut = New System.Windows.Forms.MenuItem()
        Me.ProcessMenu = New System.Windows.Forms.MenuItem()
        Me.ProcessRotateLeft = New System.Windows.Forms.MenuItem()
        Me.ImagePrint = New System.Windows.Forms.MenuItem()
        Me.ImageExit = New System.Windows.Forms.MenuItem()
        Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.ImageMenu = New System.Windows.Forms.MenuItem()
        Me.ImageLoad = New System.Windows.Forms.MenuItem()
        Me.MenuItem4 = New System.Windows.Forms.MenuItem()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(400, 400)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox1.TabIndex = 6
        Me.PictureBox1.TabStop = False
        '
        'ProcessRotateRight
        '
        Me.ProcessRotateRight.Index = 1
        Me.ProcessRotateRight.Text = "Rotate Right"
        '
        'ZoomIn
        '
        Me.ZoomIn.Index = 2
        Me.ZoomIn.Text = "Zoom In"
        '
        'PrintDocument1
        '
        '
        'ZoomMenu
        '
        Me.ZoomMenu.Index = 2
        Me.ZoomMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.ZoomAuto, Me.ZoomNormal, Me.ZoomIn, Me.ZoomOut})
        Me.ZoomMenu.Text = "Zoom"
        '
        'ZoomAuto
        '
        Me.ZoomAuto.Index = 0
        Me.ZoomAuto.Text = "Auto"
        '
        'ZoomNormal
        '
        Me.ZoomNormal.Index = 1
        Me.ZoomNormal.Text = "Normal"
        '
        'ZoomOut
        '
        Me.ZoomOut.Index = 3
        Me.ZoomOut.Text = "Zoom Out"
        '
        'ProcessMenu
        '
        Me.ProcessMenu.Index = 1
        Me.ProcessMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.ProcessRotateLeft, Me.ProcessRotateRight})
        Me.ProcessMenu.Text = "Process"
        '
        'ProcessRotateLeft
        '
        Me.ProcessRotateLeft.Index = 0
        Me.ProcessRotateLeft.Text = "Rotate Left"
        '
        'ImagePrint
        '
        Me.ImagePrint.Index = 1
        Me.ImagePrint.Text = "Print"
        '
        'ImageExit
        '
        Me.ImageExit.Index = 3
        Me.ImageExit.Text = "Exit"
        '
        'PrintPreviewDialog1
        '
        Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
        Me.PrintPreviewDialog1.Enabled = True
        Me.PrintPreviewDialog1.Icon = CType(resources.GetObject("PrintPreviewDialog1.Icon"), System.Drawing.Icon)
        Me.PrintPreviewDialog1.MaximumSize = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
        Me.PrintPreviewDialog1.Opacity = 1
        Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
        Me.PrintPreviewDialog1.Visible = False
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.ImageMenu, Me.ProcessMenu, Me.ZoomMenu})
        '
        'ImageMenu
        '
        Me.ImageMenu.Index = 0
        Me.ImageMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.ImageLoad, Me.ImagePrint, Me.MenuItem4, Me.ImageExit})
        Me.ImageMenu.Text = "Image"
        '
        'ImageLoad
        '
        Me.ImageLoad.Index = 0
        Me.ImageLoad.Text = "Load"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 2
        Me.MenuItem4.Text = "-"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoScroll = True
        Me.ClientSize = New System.Drawing.Size(416, 409)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1})
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Print Bitmap Demo"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim R As Rectangle
        Dim PWidth, PHeight As Integer
        Dim PictLeft, PictTop As Integer
        PWidth = PictureBox1.Width
        PHeight = PictureBox1.Height
        ' The following statements calculate the origin and the dimensions of the
        ' rectangle in which the image will be printed, so that the image will be centered on the page
        With PrintDocument1.DefaultPageSettings.PaperSize
            If PWidth < .Width Then
                PictLeft = (.Width - PWidth) / 2
            Else
                PictLeft = 0
            End If
            If PHeight < .Height Then
                PictTop = (.Height - PHeight) / 2
            Else
                PictTop = 0
            End If
        End With
        R = New Rectangle(PictLeft, PictTop, PWidth, PHeight)
        e.Graphics.DrawImage(PictureBox1.Image, R)
    End Sub

    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageLoad.Click
        Dim imgFile As String
        OpenFileDialog1.Filter = "Images|*.jpg;*.tif"
        OpenFileDialog1.ShowDialog()
        imgFile = OpenFileDialog1.FileName
        PictureBox1.Image = Image.FromFile(imgFile)
        PictureBox1.Width = PictureBox1.Height * (PictureBox1.Image.Width / PictureBox1.Image.Height)
        ' You can also call the following event handler to automatically
        ' resize the PictureBox control:
        '
        ' ZoomAuto_Click(sender, e)
        '
    End Sub

    Private Sub ProcessRotateRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessRotateRight.Click
        PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
        PictureBox1.Invalidate()
        Dim tmp As Integer
        tmp = PictureBox1.Width
        PictureBox1.Width = PictureBox1.Height
        PictureBox1.Height = tmp
    End Sub

    Private Sub ZoomIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomIn.Click
        PictureBox1.Width = PictureBox1.Width * 1.25
        PictureBox1.Height = PictureBox1.Height * 1.25
    End Sub

    Private Sub ZoomOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomOut.Click
        PictureBox1.Width = PictureBox1.Width / 1.25
        PictureBox1.Height = PictureBox1.Height / 1.25
    End Sub

    Private Overloads Sub ZoomAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomAuto.Click
        PictureBox1.Height = 400
        ' The code assumes that the height of the PictureBox control is 400 pixels and it will fit vertically
        ' in the form. To fit the PictureBox control on the form vertically, you can use
        ' the following expression to set the control's height:
        '
        PictureBox1.Height = Me.Height - SystemInformation.CaptionHeight - _
                                         SystemInformation.MenuHeight - _
                                         SystemInformation.HorizontalScrollBarHeight - 20

        ' The PictureBox control's height is set to the height of the form, 
        ' minus minus the height of the following items:
        ' the height of the form's caption
        ' the menu height,
        ' the horizontal scrollbar's height
        ' a small border of 20 pixels (some pixels above and below the PictureBox control) 
        ' You can add statements to resize either the width or the height of the PictureBox
        ' control initially, and then resize the other dimension accordingly.
        ' Even if you comment out the first statement in this subroutine
        ' and then uncomment the line that sets the PictureBox control's height,
        ' the application will work fine for images whose height exceeds their width.
        PictureBox1.Width = PictureBox1.Height * (PictureBox1.Image.Width / PictureBox1.Image.Height)
    End Sub

    Private Sub ProcessRotateLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessRotateLeft.Click
        PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
        PictureBox1.Invalidate()
        Dim tmp As Integer
        tmp = PictureBox1.Width
        PictureBox1.Width = PictureBox1.Height
        PictureBox1.Height = tmp
    End Sub

    Private Sub ImagePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImagePrint.Click
        Try
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        Catch exc As Exception
            MsgBox("Print operation failed" & vbCrLf & exc.Message)
        End Try
    End Sub

    Private Sub ZoomNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomNormal.Click
        PictureBox1.Width = PictureBox1.Image.Width
        PictureBox1.Height = PictureBox1.Image.Height
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -