简体   繁体   English

将图像从数据库存储和检索到图片框中

[英]Store and retrieve image into picturebox from database

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM