windows 如何在 MySQL 数据库 longblob 中检索图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8489338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:38:51  来源:igfitidea点击:

how to retrieve image in MySQL database longblob?

mysqlwindowsforms

提问by Sagotharan

I am using Windows Form and MySQL for my project. In that i want to save a image and retrieve that.

我在我的项目中使用 Windows 窗体和 MySQL。因为我想保存图像并检索它。

I have created a table named 'image' in that,

我在其中创建了一个名为“image”的表,

CREATE TABLE `image` (
    `id` INT(15) NOT NULL AUTO_INCREMENT,
    `extension` VARCHAR(50) NOT NULL,
    `image` LONGBLOB NOT NULL,
    PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=2

And

OpenFileDialog open = new OpenFileDialog();
            // image filters
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
            if (open.ShowDialog() == DialogResult.OK)
            {                
                txt_imagePath.Text = open.FileName;
            }

            hp.getConnStr();
            try
            {
                MySqlConnection connection = new MySqlConnection(hp.myConnStr);
                MySqlCommand command = connection.CreateCommand();                
                command.CommandText = "insert into image (image) values ('"+txt_imagePath.Text +"')";
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }

in Browse button click event,..

在浏览按钮点击事件中,..

The file is successfully saved. Now i want to retrieve that picture and show in picture box. So that i try this bellow code,..

文件已成功保存。现在我想检索该图片并显示在图片框中。所以我尝试这个波纹管代码,..

 MySqlConnection connection = new MySqlConnection(hp.myConnStr);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = "select image from image";
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                   pictureBox1.Image = new Bitmap(Reader[0].ToString()); 
                }
                connection.Close(); 

but no use.

但没有用。

Please help me.

请帮我。

采纳答案by Emir Akayd?n

You should read the blob field as a MemoryStreamand set to Imageproperty of the control using Image.FromStream().

您应该将 blob 字段读取为 aMemoryStream并使用 设置为Image控件的属性Image.FromStream()

回答by Sudhir Bastakoti

I hope something like this would help:

我希望这样的事情会有所帮助:

public Bitmap loadImage(int imgID)
        {

            MySqlDataReader myData;
            MySqlCommand cmd = new MySqlCommand();

            string SQL;
            byte[] rawData;
            MemoryStream ms;
            UInt32 FileSize;
            Bitmap outImage;

            SQL = "SELECT ImageName, ImageSize, Image FROM Images WHERE ImageID =";
            SQL += imgID.ToString();

            try
            {
                cmd.Connection = connection;
                cmd.CommandText = SQL;

                myData = cmd.ExecuteReader();

                if (!myData.HasRows)
                    throw new Exception("There are no blobs to save");

                myData.Read();

                FileSize = myData.GetUInt32(myData.GetOrdinal("ImageSize"));
                rawData = new byte[FileSize];

                myData.GetBytes(myData.GetOrdinal("Image"), 0, rawData, 0, (Int32)FileSize);


                ms = new MemoryStream(rawData);
                outImage = new Bitmap(ms);
                ms.Close();
                ms.Dispose();

                myData.Close();
                myData.Dispose();

                cmd.Dispose();

                return outImage;


            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

        }

回答by Vladislav Vaintroub

while (Reader.Read()) 
{ 
  pictureBox1.Image = new Bitmap(new MemoryStream((byte[])Reader.GetValue(0)));
} 

this should do it. reader.GetValue() returns byte array on MySQL blobs, this does the trick.

这应该这样做。reader.GetValue() 在 MySQL blob 上返回字节数组,这就是诀窍。