Hi everyone,

In my database table I created a column whose data type is Image. I have successfully inserted a image from PictureBox control in binary format in that table field. Now I would like to retrieve that image stored in binary format and would like to show this in a PictureBox. Therefore i have written code but when i run it show problems at the following line.

pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);


I am giving all the code written for the work. Is there any one who can help me. I need the solution very urgently as this is my university project work.

Thanks
Nasir

C# Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Initialize Connection string
                String strCn = "server=PC1\\SQLEXPRESS; integrated security=true; database=DB_Payroll"; 
                SqlConnection cn = new SqlConnection(strCn);
                cn.Open();

                //Retrieve BLOB from database into DataSet.                
                SqlCommand cmd = new SqlCommand("select Employee_ID, Photo from Employee_Info where employee_id='E004'", cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "Employee_Info");
                int c = ds.Tables["Employee_Info"].Rows.Count;
                MessageBox.Show(c.ToString());

                if (c > 0)
                {   //BLOB is read into Byte array, then used to construct MemoryStream,
                    //then passed to PictureBox.
                    Byte[] byteBLOBData = new Byte[0];
                    byteBLOBData = (Byte[])(ds.Tables["Employee_Info"].Rows[c - 1]["Photo"]);
                    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
                    if (stmBLOBData != null)
                    {
                        pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);
                    }
                }
                cn.Close();
            }            
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message);
            }				
        }
    }
}
pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);

> I have successfully inserted a image from PictureBox control in binary format in that table field.

Can I see that code?

What problems are shown at the line "pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);", can you give a specific error message?

> I have successfully inserted a image from PictureBox control in binary format in that table field.

Can I see that code?

here are the code for insert into database

byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;

here are the code for insert into database

byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;

You should be able to cut that down to a single line to get the byte array.

Byte[] buffer = File.ReadAllBytes(fileName);

What problems are shown at the line "pictureBox1.Image = Image.FromStream(stmBLOBData,false,false);", can you give a specific error message?

Yes this is showing invalid parameter

Line 41 of your code : Byte[] byteBLOBData = new Byte[0];
An array of zero elements?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.