Search code examples
sql-serverwinformsuploadpicturebox

Unable To Show Image In Picture Box Control From SQL Server Database In Windows Form Application


I want to add scanned images (Vehicle Documents) to SQL Server Database from my C# Windows Form Application. In the SQL Server database, the datatype for the column where scanned documents will be saved is Image where I want to store the image as an array for each scanned document. Here is my code that saves the data into the SQL Server database.

Browse Button Code:

        private void btnBrowseFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "image files|*.jpg;*.png;*.gif;*.icon;*.bmp;*.*";
            DialogResult dResult = fileDialog.ShowDialog();

            if (dResult == DialogResult.OK)
            {
                pBoxVehicleDocument.Image = Image.FromFile(fileDialog.FileName);
            }
        }

Add Button Code:

private void btnAdd_Click(object sender, EventArgs e)
{
    string documentName, vehicleId;
    documentName = txtDocumentName.Text;
    vehicleId = lblVehicleId.Text;
    
    MemoryStream mStream = new MemoryStream();
    pBoxDocument.Image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    mStream.ToArray();

    //The documentName, vehicleId and imageArray to be passed to AddVehicleDocuments Method within Vehicles Class.

    //No I am getting invalid arguments exception on AddVehicleDocuments() method.
    Vehicles.AddVehicleDocuments(documentName, vehicleId, imageArray);
}

And Finally the AddVehicleDocuments Method Code within Vehicles Class:

public static void AddVehicleDocuments(string documentName, int vehicleId, byte[] imageString)
        {
            string query = string.Format("insert into tblDocuments values ('{0}', {1}, {3})", documentName, vehicleId, imageString);
            DataAccess.Execute(query);
        }

Now, the following code is for image selection. I am not sure how can I convert the array values from the database back to a Picture Box on the Windows Form:

private void childDocumentDetails_Load(object sender, EventArgs e)
{
    int documentId = Convert.ToInt32(lblDocumentId.Text);
    DataRow dr = Vehicles.GetDocumentDetailsById(documentId);

    txtDocumentName.Text = dr["DocumentName"].ToString();
    txtVehicleNo.Text = dr["VehicleNo"].ToString();
    //here the image array from the database will locate in dr["VehicleDocument"];
    

    pBoxDocument.Image = //Need to write my code here
}

The DataRow dr contains all of the three records for the given document i.e. DocumentName, VehicleNo/VehicleID, and the byte string but the Picture Box Control still do not show the target image. Just need some expert suggestion in this regard.

Any help will be highly appreciated.


Solution

  • Because I was sure as per my research, that the Image data type on SQL Server-Side will work for me to store scanned documents for a vehicle. However, I modified my code later and used Verbinary(MAX) as the data type for storing images in the database.

    I modified my insertion code which now looks like this:

    private void btnAdd_Click(object sender, EventArgs e)
    {
        string documentName, vehicleId;
        documentName = txtDocumentName.Text;
        vehicleId = lblVehicleId.Text;
        
        MemoryStream mStream = new MemoryStream();
        pBoxDocument.Image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] imagesBytes = mStream.ToArray();
    
        Vehicles.AddVehicleDocuments(documentName, vehicleId, imagesBytes);
    }
    

    And the image selection code looks like:

    private void childDocumentDetails_Load(object sender, EventArgs e)
    {
        int documentId = Convert.ToInt32(lblDocumentId.Text);
        DataRow dr = Vehicles.GetDocumentDetailsById(documentId);
    
        txtDocumentName.Text = dr["DocumentName"].ToString();
        txtVehicleNo.Text = dr["VehicleNo"].ToString();
        byte[] imageString = (byte[]dr["VehicleDocument"]);
        MemoryStream mStream = new MemoryStream(imageString);
    
        pBoxDocument.Image = Image.FromStream(mStream);
    }
    

    Thanks to @Jimi for suggesting code modifications.