[英]Store and retrieve image into picturebox from database
您可以将图像保存在文件夹中,并将图像的地址保存在数据库中的文件夹中,或者将图像转换为字节数组并将其保存在数据库中,并在检索时将字节转换为数组。
将图像保存到文件夹
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()
}
}
并将图像字节转换为图片框
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;
}
并将图像转换为字节
public byte[] ImageToByteArray(string fileName)
{
return File.ReadAllBytes(fileName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.