【问题标题】:Is it possible to print or save the picturebox with other picture box within it是否可以打印或保存带有其他图片框的图片框
【发布时间】:2013-11-16 18:13:04
【问题描述】:

我是 vb.net 初学者。我有一个项目,我需要执行使用户能够添加新图片(这是一个新图片框)并将其移动到图片框中的功能。我已经让这两个发生了,但我不知道如何使图片框(允许新的图片框移动到里面)保存为位图/jpg到数据库中。有可能这样做吗?如果可以,怎么做?

Public Class Form1

    Private btn As Button ' this is a reference object
    Private pic As PictureBox
    Private ptX, ptY As Integer
    Private drag As Boolean


    Private Sub nodepic_MouseDown(ByVal senderPic As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        If e.Button = MouseButtons.Left Then
            drag = True
            pic = CType(senderPic, PictureBox)
            ptX = e.X : ptY = e.Y
        End If

        If pic.Focused Then
            clearButton.Enabled = True
        End If

    End Sub

    Private Sub nodepic_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        If drag Then
            If pic.Location.X >= 1 AndAlso pic.Location.Y >= 1 AndAlso
                (pic.Location.X + pic.Width) <= panelPictureBox.Width - 5 AndAlso
                (pic.Location.Y + pic.Height) <= panelPictureBox.Height - 5 Then
                pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
                Me.Refresh()
            Else
                drag = False
                pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
            End If
        End If

    End Sub

    Private Sub node_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        drag = False
    End Sub

    Private Sub deleteButton(senderPic As Object, e As EventArgs)
        Dim delete As DialogResult
        delete = MessageBox.Show("Are you sure to delete this icon?", "Delete Icon", MessageBoxButtons.YesNo)
        If delete = Windows.Forms.DialogResult.Yes Then
            senderPic.Dispose()
            locationLabel.Text = String.Empty
        End If
    End Sub

    Private Sub locationButton(senderPic As Object, e As System.Windows.Forms.MouseEventArgs)
        pic.Location = New Point(pic.Location.X + e.X - ptX, pic.Location.Y + e.Y - ptY)
        locationLabel.Text = pic.Location.ToString()
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick

        Dim picBox As New PictureBox

        If e.Node.Name.Equals("red") Then
            picBox.Image = ImageList1.Images(0)
        End If
        If e.Node.Name.Equals("orange") Then
            picBox.Image = ImageList1.Images(1)
        End If
        picBox.Location = New Point(10, 10)
        panelPictureBox.Controls.Add(picBox)
        action(picBox)
    End Sub

    Private Sub action(sender As PictureBox)
        AddHandler sender.MouseDown, AddressOf nodepic_MouseDown
        AddHandler sender.MouseMove, AddressOf nodepic_MouseMove
        AddHandler sender.MouseUp, AddressOf node_MouseUp
        AddHandler sender.MouseDoubleClick, AddressOf deleteButton
        AddHandler sender.MouseClick, AddressOf locationButton
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        panelPictureBox.Enabled = True
        panelPictureBox.BackColor = Color.White
    End Sub

    Private Sub clearButton_Click(senderPic As Object, e As EventArgs) Handles clearButton.Click
        pic.Dispose()
    End Sub**strong text**

End Class

【问题讨论】:

    标签: vb.net image printing save


    【解决方案1】:

    您可以使用如下代码将 PictureBox 及其“子”PictureBox 保存到位图中:

        Dim bmp As New Bitmap(panelPictureBox.Width, panelPictureBox.Height)
        panelPictureBox.DrawToBitmap(bmp, panelPictureBox.ClientRectangle)
    

    接下来,您通过将其写入 MemoryStream 来将其保存到内存中。注意可以在第二个参数中指定格式:

        Dim ms As New System.IO.MemoryStream()
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
    

    最后,您可以从生成的 MemoryStream 中获取一个字节数组:

        Dim bytes() As Byte = ms.ToArray
        ' ... save "bytes" to your database ...
    

    【讨论】:

      【解决方案2】:

      如果您需要在图片框内打印图像,则应在表单设计中插入打印对话框,打印文档,然后复制下面的代码

      Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
      
          PrintDialog1 = New PrintDialog
      
          PrintDialog1.Document = PrintDocument1 'pbxLogo.Image
      
          Dim r As DialogResult = PrintDialog1.ShowDialog
      
          If r = DialogResult.OK Then
      
              PrintDocument1.Print()
      
          End If
      
      End Sub
      
      Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
      
          e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多