簡體   English   中英

從數據庫加載 PictureBox 圖像

[英]Loading PictureBox Image From Database

我正在嘗試將圖像從數據庫加載到PictureBox 我使用以下這些代碼將它們加載到我的圖片中。 我已經寫了一些代碼,但不知道我應該做什么才能繼續。

任何幫助將不勝感激。

private void button1_Click(object sender, EventArgs e)
    {
        sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
        cmd = new SqlCommand();
        cmd.Connection = sql;
        cmd.CommandText = ("select Image from Entry where EntryID =@EntryID");
        cmd.Parameters.AddWithValue("@EntryID", Convert.ToInt32(textBox1.Text));
    }

在button1_Click中繼續這樣的事情:

// Your code first, here.

var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds, "Images");
int count = ds.Tables["Images"].Rows.Count;

if (count > 0)
{ 
    var data = (Byte[])ds.Tables["Images"].Rows[count - 1]["Image"];
    var stream = new MemoryStream(data);
    pictureBox1.Image = Image.FromStream(stream);
} 

假設我們有一個名為BLOBTest的表的簡單數據庫:

CREATE TABLE BLOBTest
(
BLOBID INT IDENTITY NOT NULL,
BLOBData IMAGE NOT NULL
)

我們可以通過以下方式檢索圖像以進行編碼:

try
{
    SqlConnection cn = new SqlConnection(strCn);
    cn.Open();

    //Retrieve BLOB from database into DataSet.
    SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn);   
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "BLOBTest");
    int c = ds.Tables["BLOBTest"].Rows.Count;

    if(c>0)
    {   //BLOB is read into Byte array, then used to construct MemoryStream,
        //then passed to PictureBox.
        Byte[] byteBLOBData =  new Byte[0];
        byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
        MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
        pictureBox1.Image= Image.FromStream(stmBLOBData);
    } 
    cn.Close();
}
catch(Exception ex)
{MessageBox.Show(ex.Message);}

此代碼將數據庫中BLOBTest表中的行檢索到DataSet ,將最近添加的圖像復制到Byte數組中,然后復制到MemoryStream對象中,然后將MemoryStream加載到PictureBox控件的Image屬性中。

完整參考指南:

http://support.microsoft.com/kb/317701

private void btnShowImage_Click(object sender, EventArgs e)
{
    string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
    Con = new OleDbConnection(@constr);
    Con.Open();
    Com = new OleDbCommand();
    Com.Connection = Con;     
    Com.CommandText = "SELECT Photo FROM PatientImages WHERE Patient_Id =  " + val + " ";
    OleDbDataReader reader = Com.ExecuteReader();
    if (reader.Read())
    {
        byte[] picbyte = reader["Photo"] as byte[] ?? null;
        if (picbyte != null)
        {
            MemoryStream mstream = new MemoryStream(picbyte);
            pictureBoxForImage.Image = System.Drawing.Image.FromStream(mstream);
        {
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
    }
}

這是另一種方式:

您可以簡單地將Image轉換為Text ,然后再將其保存到DataBase中,然后在讀取后將其轉換回Image


public string ImageToStringFucntion(Image img)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] imgBytes = ms.ToArray();
                    string FinalText = Convert.ToBase64String(imgBytes, 0 , imgBytes.Length);

                    return FinalText;
                }
            }
            catch
            {
                return null;
            }
        }

現在您可以InsertUpdate您的數據庫...

現在讓我們考慮你想要它回來:

public Image StringToImage_(string input_)
        {
            try
            {
                byte[] imgBytes = Convert.FromBase64String(input_);
                using (MemoryStream ms = new MemoryStream(imgBytes))
                {
                    Image img = Image.FromStream(ms, true);

                    return img;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

現在您可以執行以下操作:

// Considering you have already pulled your data 
// from database and set it in a DataSet called 'ds',
// and you picture is on the field number [1] of your DataRow

pictureBox1.Image = StringToImage_(ds.Table[0].Rows[0][1].ToString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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