晚上好,有谁知道如何将图像(数据类型图像)与该数据一起存储在我的数据库中,当我想编辑时,图像也将检索到图片框。我在功能中使用了存储过程。
答案 0 :(得分:0)
您可以将图片保存在文件夹中,并将图片在文件夹中的地址保存在数据库中,也可以将图片转换为字节数组保存在数据库中,检索时将字节转换为数组。< /p>
保存图片到文件夹
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);
}