如何从图片框中保存图像vb

时间:2021-05-19 09:00:59

fillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertext Hello everyone, I am interested in vb and am confused how to save the contents of a Picturebox as a png file when button1 is clicked .I would be extremely pleased if someone would fix this. Here is the code;

fillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertextfillertext大家好,我对vb感兴趣,并且在点击button1时如何将Picturebox的内容保存为png文件。如果有人能解决这个问题,我会非常高兴。这是代码;

'I got frustrated trying to find a Simple VB.NET freehand drawing code
'So I made my own :)

'*** Acknowlegements  ***
'Ideas for this code came from the MicroSoft "Scribble" sample code, 
'Christian Graus's excellent arcticle on a C++ code called "Doodle" 
'and the MicroSoft website.

'Note that this is a VERY pedantic freehand drawing code, so be kind in you comments. :)
'It uses a graphics path to follow the users mouse movements
'The path is then painted in the window.

'By John Buettner
'26 July 2003
'******************************************************
'This code is for informational purposes
'It is property of the code writer, but may be modified and
'used for any purpose private or commercial, However,
'Users of this code must agree not to copyright or infringe
'upon the original coders rights to this code.
'******************************************************

Imports System
Imports System.Drawing
Imports System.Windows.Forms


Public Class Form
    'Namespace myPaint

    Inherits System.Windows.Forms.Form  ' Of course ;)

    Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement

    '*** below I declare some values for an Alpha  and other user selected variables
    'these will be used as I expand this program for a higher level use.

    Dim myAlpha As Integer = 100 ' declare a Alpha variable
    Dim myUserColor As New Color() 'this is a color the user selects
    Friend WithEvents Button1 As Button
    Friend WithEvents SaveFileDialog1 As SaveFileDialog
    Dim myPenWidth As Single = 5 'set pen width variable

    '**************************************************************



#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.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form))
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.Color.GhostWhite
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox1.Location = New System.Drawing.Point(0, 33)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(696, 423)
        Me.PictureBox1.TabIndex = 2
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Image = Global.Ubernote.My.Resources.Resources.fl
        Me.Button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button1.Location = New System.Drawing.Point(12, 4)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "Save"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'SaveFileDialog1
        '
        '
        'Form
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.White
        Me.ClientSize = New System.Drawing.Size(696, 456)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "Form"
        Me.Text = "Draw"
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region



    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown

        If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  

            mousePath.StartFigure()    ' The L mouse is down so we need to start a new line in mousePath

        End If

    End Sub


    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down  

            Try
                mousePath.AddLine(e.X, e.Y, e.X, e.Y)    'Add mouse coordiantes to mousePath

            Catch
                MsgBox("No way, Hose!")
            End Try

        End If

        PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event

    End Sub



    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        ' Here is where we do the actual painting

        Try ' error trapping

            myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
            'change the value of myUserColor

            myAlpha = 100  ' This will give the color a Alpha effect, you can set this to 255 if you want a full color


            Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen

            e.Graphics.DrawPath(CurrentPen, mousePath)  'draw the path! :)

        Catch
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            SaveFileDialog1.ShowDialog()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        Try
            '       Dim FileToSaveAs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, SaveFileDialog1.FileName)
            '      PictureBox1.Image.Save(FileToSaveAs, System.Drawing.Imaging.ImageFormat.Jpeg)
            'PictureBox1.Image.Save(FileToSaveAs)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

1 个解决方案

#1


1  

   Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
    Dim path As String
    Dim pic As Image
    pic = PictureBox1.Image
    SaveFileDialog1.ShowDialog()
    pic.Save(SaveFileDialog1.FileName)
End Sub

try this

尝试这个

#1


1  

   Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
    Dim path As String
    Dim pic As Image
    pic = PictureBox1.Image
    SaveFileDialog1.ShowDialog()
    pic.Save(SaveFileDialog1.FileName)
End Sub

try this

尝试这个