如何保存和上传图片

时间:2012-12-03 10:15:09

标签: asp.net vb.net save image-uploading

如何使用vb语言和SQL Server 2008在ASP.net上传和保存图像。

1 个答案:

答案 0 :(得分:1)

您可以使用此tutorial

从数据库中保存图像的链接中的一部分:

Private Sub Button2_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text = "" Then
        MsgBox("Fill the Name Field")
    Else
        Dim sql As String = "INSERT INTO Information VALUES(@name,@photo)"
        Dim cmd As New SqlCommand(sql, con)
        cmd.Parameters.AddWithValue("@name", TextBox1.Text)
        Dim ms As New MemoryStream()
        PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
        Dim data As Byte() = ms.GetBuffer()
        Dim p As New SqlParameter("@photo", SqlDbType.Image)
        p.Value = data
        cmd.Parameters.Add(p)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Name & Image has been saved", "Save", MessageBoxButtons.OK)
        Label1.Visible = False
        TextBox1.Visible = False
    End If
End Sub

以下是图片的检索方式:

Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As _
            System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
    cmd = New SqlCommand("select photo from Information where name='" & _
              DataGridView1.CurrentRow.Cells(0).Value() & "'", con)
    Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
    If Not imageData Is Nothing Then
        Using ms As New MemoryStream(imageData, 0, imageData.Length)
            ms.Write(imageData, 0, imageData.Length)
            PictureBox1.BackgroundImage = Image.FromStream(ms, True)
        End Using
    End If
    GroupBox2.SendToBack()
    GroupBox2.Visible = False
    Label1.Visible = True
    Label1.Text = DataGridView1.CurrentRow.Cells(0).Value()
End Sub

your findingsthis sample进行比较,并探索您缺少的内容。