1

嗨,我使用下面的代码将图像保存到我的数据库中,

但现在我想知道如何将图片从数据库中获取到 PictureBox

你能帮我么。

private void button1_Click(object sender, EventArgs e)
{
    ofdFoto.ShowDialog();
    string i = ofdFoto.FileName.ToString();
    pbxFoto.ImageLocation = i;
}

private void button2_Click(object sender, EventArgs e)
{
    dbConn.Open();
    string querys = "INSERT INTO Fruits (Name, Picture) VALUES ('" + txtName.Text + "','" + ImageToByte(pbxFoto.Image) + "')";
    OleDbCommand cd = new OleDbCommand(querys, dbConn);
    cd.ExecuteNonQuery();
    dbConn.Close();
    MessageBox.Show("Picture saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
4

2 回答 2

0
private void button1_Click(object sender, EventArgs e)
    {
        PictureBox p =new PictureBox();
        p.ImageLocation = ofdFoto.FileName.ToString();
        p.Location = new Point(100, 100);
        this.Controls.Add(p);
    }

看看这对你有没有帮助!!

于 2012-08-01T09:48:19.243 回答
0

试试这个代码:

public static Bitmap BytesToBitmap(byte[] byteArray)
{
    using (var ms = new MemoryStream(byteArray))
    {
        var img = (Bitmap)Image.FromStream(ms);
        return img;
    }
}

并设置为PictureBox.Image属性:

pictureBox1.Image = BytesToBitmap(byteArray);
于 2012-08-01T09:48:45.193 回答