使用 Oracle Blob 在 C# 中加载图片框

发布于 2024-12-20 13:04:52 字数 1553 浏览 5 评论 0原文

我有一个非常基本的表单,只有一个图片框、一个文本字段和一个按钮。我在 Oracle 中创建了一个表来存储 blob,我想在单击按钮后将该图像加载到 c# 中的图片框中。这是我到目前为止所写的内容。

我的图片框名称=“picBoxXray”。我的文本框名称=“txtXrayId”。我的按钮名称 =“btnGetXrayID”。我的表名称是“XRay”。

    private void btnGetXrayID_Click(object sender, EventArgs e)
    {


        if (txtXrayId.Text == "")
        {
            MessageBox.Show("XrayID be entered");
        }

        if (picBoxXray.Image != null)        
        { 
            picBoxXray.Image.Dispose();         
        }

        string connectionString = GetConnectionString();
        using (OracleConnection connection = new OracleConnection())
        {
            connection.ConnectionString = connectionString;
            connection.Open();
            Console.WriteLine("State: {0}", connection.State);
            Console.WriteLine("ConnectionString: {0}",
                              connection.ConnectionString);

            OracleCommand command = connection.CreateCommand();

            string sql = "SELECT * FROM XRay WHERE XrayID =" + txtXrayId.Text;

            OracleCommand cmd = new OracleCommand(sql, connection);
            OracleDataReader dr = cmd.ExecuteReader();
            cmd.CommandType = CommandType.Text;
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                // Obtain the image
                //Need code

            }

            command.CommandText = sql;
            command.ExecuteNonQuery();
            connection.Close();

    }

任何帮助将不胜感激, 谢谢

I've a very basic form with just one picture box,one textfield and one button. I've created a table in Oracle to store a blob, I want to load that image into a picturebox in c# once the button is clicked. Here is what I've wrote so far.

My picturebox name = "picBoxXray". My textbox name = "txtXrayId". My button name = "btnGetXrayID". And My table name is "XRay".

    private void btnGetXrayID_Click(object sender, EventArgs e)
    {


        if (txtXrayId.Text == "")
        {
            MessageBox.Show("XrayID be entered");
        }

        if (picBoxXray.Image != null)        
        { 
            picBoxXray.Image.Dispose();         
        }

        string connectionString = GetConnectionString();
        using (OracleConnection connection = new OracleConnection())
        {
            connection.ConnectionString = connectionString;
            connection.Open();
            Console.WriteLine("State: {0}", connection.State);
            Console.WriteLine("ConnectionString: {0}",
                              connection.ConnectionString);

            OracleCommand command = connection.CreateCommand();

            string sql = "SELECT * FROM XRay WHERE XrayID =" + txtXrayId.Text;

            OracleCommand cmd = new OracleCommand(sql, connection);
            OracleDataReader dr = cmd.ExecuteReader();
            cmd.CommandType = CommandType.Text;
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                // Obtain the image
                //Need code

            }

            command.CommandText = sql;
            command.ExecuteNonQuery();
            connection.Close();

    }

Any help would be greatly aprreciated,
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-12-27 13:04:52

您必须从流创建图像,例如:

public Image ByteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

然后将图像分配给图片框。其中的 byteArray 是您从数据库读取的 blob。当然,图像必须以已知的格式存储(例如 png/jpeg )

You have to create the image from a stream, for example:

public Image ByteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

Then aassign the image to the picture box. the byteArray in is the blob you read from the db. of course the image have to be stored in a format known ( png/jpeg for instance )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文