从数据库加载图片框图像

本文关键字:框图 图像 数据库 加载 | 更新日期: 2023-09-27 18:33:23

我正在尝试将图像从数据库加载到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());