Question How can i resize an image then save it into a database?

Rul3R

New member
Joined
Oct 17, 2008
Messages
1
Programming Experience
Beginner
Hi!

I need to resize an image and then save it to the database.

In the line:
VB.NET:
"Me.PictureBox1.Image.Save(ms,Me.PictureBox1.Image.RawFormat) "

Gets the error:
"The valor can´t be null. Name of parameter: Encoder"

Can Somebody help me please?

The code is:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        With OpenFileDialog1
            .InitialDirectory = "C:\"
            .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
            .FilterIndex = 2
        End With

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            With PictureBox1
                .Image = Image.FromFile(Me.OpenFileDialog1.FileName)
                .SizeMode = PictureBoxSizeMode.CenterImage
            End With

        End If
        
        Me.Label1.Text = Me.OpenFileDialog1.FileName.ToString

        Dim ms As New MemoryStream

       Dim width As Integer = 100

        Dim height As Integer = 100

        PictureBox1.Image = PictureBox1.Image.GetThumbnailImage(width, height, Nothing, Nothing)

        Me.PictureBox1.Image.Save(ms, Me.PictureBox1.Image.RawFormat)

        Dim arrayImage() As Byte = ms.GetBuffer

        ms.Close()

        Dim nStr As String = Me.Label1.Text.Substring(Me.Label1.Text.LastIndexOf("\") + 1)

        Dim strQuery As String = "INSERT INTO Imagens(NomeImagem, Imagem) VALUES(@NomeImagem, @Imagem)"

        Dim objcommand As New SqlCommand(strQuery, Me.SqlConnection1)
        With objcommand
            .Parameters.Add(New SqlParameter("@NomeImagem", SqlDbType.NVarChar, 50)).Value = nStr
            .Parameters.Add(New SqlParameter("@Imagem", SqlDbType.Image)).Value = arrayImage
        End With

        Me.SqlConnection1.Open()
        objcommand.ExecuteNonQuery()
        Me.SqlConnection1.Close()

        Me.SqlConnection1.Open()

        Me.DataSet11.Clear()
        Me.SqlDataAdapter1.Fill(Me.DataSet11.Imagens)

        With Me.ListBox1
            .DataSource = Me.DataSet11.Imagens
            .DisplayMember = "NomeImagem"

        End With

        Me.SqlConnection1.Close()

Thank you! ;)
 
Back
Top