使用c#以BLOB格式显示存储在MySql数据库中的图像 [英] Display an image stored in MySql databse in BLOB format using c#

查看:108
本文介绍了使用c#以BLOB格式显示存储在MySql数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

am使用以下功能将图像存储在数据库中

am using following function to store image in db

 void SaveImage(byte[] image)
        {
            MySqlConnection con = new MySqlConnection(db);//new connection is made
            con.Open();

                string cmdText = "INSERT INTO Picture(RoomNo ,pic )VALUES ('" + RoomNo.Text + "',?Image)";
                MySqlCommand cmd = new MySqlCommand(cmdText, con);
                cmd.Parameters.Add("?Image", image);
                cmd.ExecuteNonQuery();

        }

主要是这样调用函数

 using (var ms = new MemoryStream())
            {
                picbox.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                SaveImage(ms.ToArray());
            }

我不知道如何在图片框中显示它..任何人都可以帮助我吗?

I do not know how to display it in a picture box .. can any one help me???

推荐答案

您是否正在使用Windows窗体?并且必须将字节数组转换为图像才能在图片框中显示它.

Are you using Windows Forms? And you must Convert Byte array to Image for displaying it in Picture Box.

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

以及如何将Image转换为字节数组.我希望这个问题不存在.您可以使用:

And how did you convert Image to byte array. I hope that problem not there. You can use:

  private byte[] ImageToByteArray(string ImageFile)
    {
        FileStream stream = new FileStream(
              ImageFile, FileMode.Open, FileAccess.Read);
        BinaryReader reader = new BinaryReader(stream);

        // Convert image to byte array.
        byte[] photo = reader.ReadBytes((int)stream.Length);

        return photo;
    }

这篇关于使用c#以BLOB格式显示存储在MySql数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆