Good evening, Does anyone know how to store image(datatype image) in my databse together with that data and when i want to edit, the image will also retrieve into picturebox. I used stored procedure in functionalities.
You can save the image in a folder and save the address of the image in the folder in the database or convert the image to an array of bytes and save it in the database and convert the bytes to the array when retrieving.
save image to folder
public void SaveImageToFile()
{
OpenFileDialog opFile = new OpenFileDialog();
opFile.Title = "Select a Image";
opFile.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\ProImages\";
if (Directory.Exists(appPath) == false)
{
Directory.CreateDirectory(appPath);
}
if (opFile.ShowDialog() == DialogResult.OK)
{
try
{
string iName = opFile.SafeFileName;
string filepath = opFile.FileName;
File.Copy(filepath, appPath + iName);
picProduct.Image = new Bitmap(opFile.OpenFile());
}
catch (Exception exp)
{
MessageBox.Show("Unable to open file " + exp.Message);
}
}
else
{
opFile.Dispose();File.ReadAllBytes()
}
}
and for convert image bytes to picturebox
public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}
and convert image to bytes
public byte[] ImageToByteArray(string fileName)
{
return File.ReadAllBytes(fileName);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.