Results 1 to 3 of 3

Thread: [RESOLVED] Load image from SQL server

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Resolved [RESOLVED] Load image from SQL server

    Dear all,
    I'm saving an image into an MyExpress SQL database in the following way:

    Code:
     Dim cmdSchiera As SqlCommand
                Dim insertSchiera As String = String.Empty
    
                Dim dbsourceSchiera As String = ConfigurationManager.ConnectionStrings("CTO_Management_ConnectionString").ConnectionString
                Dim connSchiera = New SqlConnection(dbsourceSchiera)
    
                cmdSchiera = New SqlCommand
                cmdSchiera.Connection = connSchiera
                connSchiera.Open()
    
                insertSchiera = "INSERT INTO Tbl_CTOSchieramento (Codice_partita, Schieramento) VALUES(@CodParSchiera,@Pitch)"
    
                cmdSchiera.CommandType = CommandType.Text
                cmdSchiera.CommandText = insertSchiera
    
                cmdSchiera.Parameters.Clear()
                Dim codiceSchiera As String = ""
                codiceSchiera = TxtPSquadraC.Text.Trim + TxtPSquadraT.Text.Trim + CmbPLeva.Text.Trim + TxtPNOsservatore.Text.Trim + TxtPCOsservatore.Text.Trim + DtPckrPDatapartita.Value.ToShortDateString
    
                cmdSchiera.Parameters.Add("@CodParSchiera", SqlDbType.NChar).Value = codiceP
                Dim ms As New MemoryStream()
                PctSchieramento.Image.Save(ms, Imaging.ImageFormat.Jpeg)
                Dim data As Byte() = ms.GetBuffer()
                Dim p As New SqlParameter("@Pitch", SqlDbType.Image)
                p.Value = data
                cmdSchiera.Parameters.Add(p)
                cmdSchiera.ExecuteNonQuery()
    
    
                connSchiera.Close()
    Then I'm trying to recall it into another form with the following code:

    Code:
     cmdSchiera.CommandText = "SELECT Schieramento FROM Tbl_CTOSchieramento WHERE Codice_partita = @CodParSP"
                cmdSchiera.Parameters.Add("@CodParSP", SqlDbType.NChar).Value = codicepartitaDettagli
                cmdSchiera.Connection = New SqlConnection(connSPstring)
                cmdSchiera.Connection.Open()
    
    
     Dim dataSchieraGP As New System.Data.DataTable
                Dim adapterSchiera As New SqlDataAdapter
                adapterSchiera = New SqlDataAdapter(cmdSchiera)
                Dim commandSchiera As New SqlCommandBuilder(adapterSchiera)
                adapterSchiera.Fill(dataSchieraGP)
    
                If dataSchieraGP.Rows.Count > 0 Then
    
                    Dim picbyteSchiera() As Byte = dataSchieraGP.Rows(0).Item("Schieramento")
                    Dim picSchiera As New System.IO.MemoryStream(picbyteSchiera)
                    PartiteDataDisplay.PctDPSchiera.BackgroundImage = Image.FromStream(picSchiera)
                    PartiteDataDisplay.PctDPSchiera.BackgroundImageLayout = ImageLayout.Stretch
    
                End If
    
                adapterSchiera.Dispose()
                cmdSchiera.Dispose()
                cmdSchiera.Connection.Close()
    Now, the image is properly retrieved according to the selection parameters, it is shown, but not in a stretch mode - it is enlarged and then shelled.

    How can I fix it? Please consider also that the two pictureboxes - the one from which the picture is saved and the second one in which it is supposed to be shown - have the same size.

    Thanks,
    A.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Load image from SQL server

    Firstly, you ought to be calling ToArray rather than GetBuffer. As for the issue, does this really have anything to do with the database? If you save the image that you retrieved to a file and then view that in an external application, does it look as you expect? If so then the whole database stuff is irrelevant and the issue is simply how you're displaying the Image at the end.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Re: Load image from SQL server

    Dear jmcilhinney,
    first of all I changed the statement you indicated writing "ToArray" - thanks a lot.

    I then added a piece of code through which the image is opened in Paint by clicking on it.
    Opening the image on Paint, I realized that only a portion of it was saved to the db. This way, I realized that in a piece of code, before saving the image, I was selecting it using the width of another picturebox rather than the one in which the image is being set.
    Using the right width, everything works fine.

    Thanks again for the reply and the suggestion,
    A.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width