Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to Convert byte[] to picterw.image for show pic or image to picurebox
Posted
Comments
saeed1364 10-Apr-11 1:03am    
byte[] pic=pic1.picbox(dataGridView1.CurrentRow.Cells[1].Value.ToString());picure.image=???;
class is......
public Byte[] picbox(string p) { // byte[] binaryData = null; byte[] binaryData =new byte[0]; connection.Open(); command.CommandText = "select * From adduser where (bakernum=@bakernum)"; command.Parameters.AddWithValue("@bakernum", p); SqlDataReader reader = command.ExecuteReader(); reader.Read(); if (reader.HasRows) binaryData = (byte[])reader.GetValue(14); return binaryData ; }

C#
byte[] buffer= byte Array here;
  MemoryStream ms = new MemoryStream(buffer);
  Bitmap bmp=new Bitmap(ms);


This bitmap can be assign to the image property of the picture box. Make sure the bytes includes the image headers as well

dispose the streams after use.
 
Share this answer
 
v2
Comments
saeed1364 10-Apr-11 1:01am    
No. is wrong solution
i want show pic or image to picurebox.
""""""""""""
byte[] pic=pic1.picbox(dataGridView1.CurrentRow.Cells[1].Value.ToString());

class is......
public Byte[] picbox(string p)
{
// byte[] binaryData = null;
byte[] binaryData =new byte[0];
connection.Open();
command.CommandText = "select * From adduser where (bakernum=@bakernum)";
command.Parameters.AddWithValue("@bakernum", p);
SqlDataReader reader = command.ExecuteReader();
reader.Read();

if (reader.HasRows)
binaryData = (byte[])reader.GetValue(14);

return binaryData ;
}
Espen Harlinn 10-Apr-11 17:22pm    
Good effort, my 5 :)
Dalek Dave 10-Apr-11 19:05pm    
Good Answer
Try this
C#
public static Bitmap BytesToBitmap(byte[] byteArray)
{
using (MemoryStream ms = new MemoryStream(byteArray))
{
Bitmap img = (Bitmap)Image.FromStream(ms);
return img;
}
}
 
Share this answer
 
Comments
Espen Harlinn 10-Apr-11 17:23pm    
Good effort - nice compact implementation using using - good stuff, my 5 :)
Dalek Dave 10-Apr-11 19:05pm    
Good Call
Sergey Alexandrovich Kryukov 11-Apr-11 0:08am    
That certainly works, my 5
--SA
When you read images from a db, you usually need a way to put them there in the first place, so just for completeness:
Use something like this to get byte[] from System.Drawing.Image
public static byte[] ConvertImageToBytes(System.Drawing.Image imageToConvert,
ImageFormat formatOfImage) 
 { 
 byte[] result; 
 try 
 { 
  using (MemoryStream ms = new MemoryStream()) 
  { 
   imageToConvert.Save(ms,formatOfImage); 
   result = ms.ToArray(); 
  } 
 } 
 catch (Exception) { /* do something useful like logging the exception */ throw;} 
 return result; 
} 


To convert a byte[] to a System.Drawing.Image you can use something like (as shown by d@nish, RaviRanjankr and Albin Abel ):
public static System.Drawing.Image ConvertBytesToImage(byte[] buffer)
{
 System.Drawing.Image result; 

 using (MemoryStream ms = new MemoryStream(buffer,0,buffer.Length)) 
 { 
  ms.Write(myByteArray,0,myByteArray.Length); 
  result = Image.FromStream(ms,true); 
 }
 return result;
}


Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Apr-11 0:08am    
Sure, a 5.
--SA
Espen Harlinn 11-Apr-11 15:11pm    
Thanks SAKryukov!
Once you have recieved the MemoryStream, you can create a image obect out of it using following:

MemoryStream memStream = new MemoryStream(imageBytes);
yourPictureBox.Image = Image.FromStream(memStream);
 
Share this answer
 
Comments
saeed1364 10-Apr-11 8:38am    
thank you my friend .ok solution very good solution
Albin Abel 10-Apr-11 10:48am    
Saeed My answer also the same. Is that mean you can't understand the code?
saeed1364 12-Apr-11 0:09am    
You just answered, but not perfect. Thank you from the time did you spend
error ms
Albin Abel 12-Apr-11 6:39am    
I didn't get you
Espen Harlinn 10-Apr-11 17:25pm    
Good effort, my 5 :)

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