Click here to Skip to main content
15,566,615 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is a problem which doesn't un-sticks from me ,,i have a column of person_image varbinary(max)at my table in MS Sqlserver-2012,, now the thing is i have to bind this column to my datagridview in windows forms, actually while before binding this binary information to Datagridview ,It's needs to be converted into images and then, i have to bind them to datagridview as shows as images in it's column,,i also tried with datagridviewimagecolumn...but it was binding with broken images,not actually with real images..here is another point to remember..i don't have the paths of images,i have only binary images at my table in Back-End..Infact i could get the paths of images...but is that is the only thing to do...did i can't convert them(binary) in to images before binding..?I am trying to fix this for long 2 days..but what the code found in different sites was getting complicated in the way of modifying my whole application code...
Here is the code which i follows and also it was throwing an exception that mentioning an object reference was not set to an instance at try block

C#
public void BindDataGrid()
       {
           string bindQuery = "select * from employee";
           _cmd = new SqlCommand();
           _cmd.CommandText = bindQuery;
           _cmd.Connection = _con;
           _con.Open();
           _da = new SqlDataAdapter(_cmd);

           _ds = new DataSet();

           DataGridViewImageColumn dgi = new DataGridViewImageColumn();
           dgi.Name = "image";
           dgi.HeaderText = "Person_image";
           dgi.ImageLayout = DataGridViewImageCellLayout.Zoom;
           try
           {
               dgi.Image = bytearraytoimage((byte[])_ds.Tables["employee"].Rows[1]["image"]);
           }
           catch (Exception x)
           {

               MessageBox.Show(x.Message);
           }
           dataGridView1.Columns.Insert(0,dgi);
           _con.Close();
           _da.Fill(_ds);
           dataGridView1.DataSource = _ds.Tables[0];
       }

       private Image bytearraytoimage(byte[] b)
       {
          MemoryStream ms=new MemoryStream(b);
           Image img=Image.FromStream(ms);
           return img;
       }
       private void Form1_Load(object sender, EventArgs e)
       {
           BindDataGrid();
       }



I will be so thankful,, if i have a smart code to achieve this task....any suggestions..please....go-on...
Posted
Updated 14-Jun-14 3:46am
v4
Comments
phil.o 14-Jun-14 6:11am    
Would you mind showing us the code you are using to load the images from the database, please?
phil.o 14-Jun-14 7:19am    
Please, move this code from the comment to your orginal question. Use the 'Improve' button for that.
Comments are not really useful nor pleasant to read code :)
Naveen Kumar Malli 14-Jun-14 8:47am    
I placed my code at my question content,, please refer and guide me .........phil.o

1 solution

If your column is VARBINARY(50) that I don;t hold out much hope of anything, image related.
even as a "raw data" image without any of the usual (and required) file format headers, that would only be an image 20 pixels by 20 pixels in monochrome at most: that's smaller than a standard icon!
And my .ICO files wouldn't fit: they are all around 2.5Kb

So...there is a good chance that the first thing you need to do is look at your database design, and how you are loading images into the DB - because it is very, very wrong!
Sort that out, and then we can look at displaying pictures...
 
Share this answer
 
Comments
phil.o 14-Jun-14 6:27am    
Not if the images are obfuscated identity photographs :)
Good catch, my 5.
Naveen Kumar Malli 14-Jun-14 6:43am    
Bsollutely sorry it's varbinary(max)......uhfff
phil.o 14-Jun-14 6:45am    
And what about the code? Could you show it, please?
Naveen Kumar Malli 14-Jun-14 7:13am    
public void BindDataGrid()
{
string bindQuery = "select * from employee";
_cmd = new SqlCommand();
_cmd.CommandText = bindQuery;
_cmd.Connection = _con;
_con.Open();
_da = new SqlDataAdapter(_cmd);

_ds = new DataSet();

DataGridViewImageColumn dgi = new DataGridViewImageColumn();
dgi.Name = "image";
dgi.HeaderText = "Person_image";
dgi.ImageLayout = DataGridViewImageCellLayout.Zoom;
try
{
dgi.Image = bytearraytoimage((byte[])_ds.Tables["employee"].Rows[1]["image"]);
}
catch (Exception x)
{

MessageBox.Show(x.Message);
}
dataGridView1.Columns.Insert(0,dgi);
_con.Close();
_da.Fill(_ds);
dataGridView1.DataSource = _ds.Tables[0];
}

private Image bytearraytoimage(byte[] b)
{
MemoryStream ms=new MemoryStream(b);
Image img=Image.FromStream(ms);
return img;
}
private void Form1_Load(object sender, EventArgs e)
{
BindDataGrid();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900