jvoros1
asked on
Image from C# into SQL and back
Trying to save a image from a PictureBox into SQL and back again.
I know that I have to convert the image into a byte[] and then save it on the SQL server as "image", but I dont know how to convert the image.
I know that I have to convert the image into a byte[] and then save it on the SQL server as "image", but I dont know how to convert the image.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I actually ended up using your way, cause what I did was had a webcam image in the box.
HOWEVER, i can't get it to save , it will say "Value is null" when trying to save. any suggestions? I'm assinging it like this... + e {"Value cannot be null.\r\nParameter name: encoder" } System.Exception
HOWEVER, i can't get it to save , it will say "Value is null" when trying to save. any suggestions? I'm assinging it like this... + e {"Value cannot be null.\r\nParameter name: encoder" } System.Exception
ASKER
rather..
MemoryStream ms2 = new MemoryStream();
//it dies on this line, saying its null...
pictureBox1.Image.Save(ms2 , pictureBox1.Image.RawForma t);
byte[] arr = ms2.GetBuffer();
aEmployee.setImage(arr);
MemoryStream ms2 = new MemoryStream();
//it dies on this line, saying its null...
pictureBox1.Image.Save(ms2
byte[] arr = ms2.GetBuffer();
aEmployee.setImage(arr);
ASKER
using System.IO;
// **** Read Image into Byte Array from Filesystem
public static byte[] setPhoto(string filePath)
{
try
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Lengt
br.Close();
fs.Close();
return photo;
}
catch (exception e)
{
MessageBox.Show(e.toString
}
}
public void getPhoto()
{
try
{
// dispose of the old image and set the picture box to null
if (pictureBox1.Image != null)
pictureBox1.Image.Dispose(
pictureBox1.Image = null;
byte[] MyData = null;
MyData = aEmployee.getImage();
int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
// Create a Filestream for writing the byte array to a gif file (the original format in this case)
FileStream fs = new FileStream("tmp.gif", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize+1); // don't ask me why, but I had to add 1 here for this to work
fs.Close();
// Assign the temporary gif file to the picture box
pictureBox1.Image = new Bitmap("tmp.gif");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message
}
}