简体   繁体   English

如何使用C#从MySQL获取图像文件路径

[英]How to get the image file path from mySQL using C#

I am retrieving the image and other information to to users with the following code. 我正在使用以下代码向用户检索图像和其他信息。 I would like to also get the image file path display in a textbox below the image. 我还要在图像下方的文本框中显示图像文件路径。 I have been trying to do that without success. 我一直试图做到这一点而没有成功。

The following is the code which I have written, I have other matters work out except getting the image location from mysql display. 以下是我编写的代码,除了从mysql显示中获取图像位置外,我还有其他事项要解决。

Please anyone help me with his! 请任何人帮助他!

private void showData_Click(object sender, EventArgs e)
            {
                string myConnection = "datasource = localhost; port=3306; username=root; password=root";
                string Query = "select * from MawkMo.Enlist_info;";
                MySqlConnection sqlConnection = new MySqlConnection(myConnection);
                MySqlCommand sqlCommand = new MySqlCommand(Query, sqlConnection);
                MySqlDataReader myReader;
                try
                {
                    sqlConnection.Open();
                    myReader = sqlCommand.ExecuteReader();
                    while (myReader.Read())
                    {

    byte[] imgbyte = (byte[])(myReader["Photo"]);
                            if (imgbyte == null)
                            {
                                PhotoBox.Image = null;
                            }
                            else
                            {
                                //string imgPath = (string)sqlCommand.ExecuteScalar();
                                //Photo_path.Text = imgPath;
                                MemoryStream mryStream = new MemoryStream(imgbyte);
                                PhotoBox.Image = System.Drawing.Image.FromStream(mryStream);
                            }

                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

In the code's current form, you cannot retrieve an image file path because you aren't actually storing the image as a file, you are storing it as a series of bytes in the database. 在代码的当前形式下,您无法检索图像文件路径,因为您实际上并未将图像存储为文件,而是将其作为一系列字节存储在数据库中。 You won't find image files on the server's hard drive anywhere, and the images won't be retrievable outside your application. 您将无法在服务器硬盘上的任何位置找到图像文件,并且无法在应用程序外部检索图像。

If having access to the images independent of your application is a concern, or if you don't want the image stored in the database (performance concerns) then you would need to re-design the database. 如果需要考虑独立于应用程序访问映像,或者不希望将映像存储在数据库中(出于性能方面的考虑),则需要重新设计数据库。 In your database saves you would issue the Image.Save() method to save the file to a specific location, and then store that string (ImageLocation) into the database rather than storing the image itself as a byte array. 在数据库保存中,您将发出Image.Save()方法将文件保存到特定位置,然后将该字符串(ImageLocation)存储到数据库中,而不是将图像本身存储为字节数组。 The retrieval process is then simply to retrieve the ImageLocation String and use it in the Image.FromFile() method. 然后,检索过程就是简单地检索ImageLocation字符串并在Image.FromFile()方法中使用它。

What is a PhotoBox.Image is? 什么是PhotoBox.Image

MemoryStream imagebuf=new MemoryStream((byte[])myReader["Photo"]);

//create image object
System.Drawing.Image outImage=System.Drawing.Image.FromStream(imagebuf,true);
outImage.Save(Server.MapPath(“PhotoTemp/”)+”2.gif”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM