簡體   English   中英

將圖像從數據庫存儲和檢索到圖片框中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM