Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从DB加载后获取黑色图像_C#_Mysql_Image_Winforms - Fatal编程技术网

C# 从DB加载后获取黑色图像

C# 从DB加载后获取黑色图像,c#,mysql,image,winforms,C#,Mysql,Image,Winforms,经过一段时间的尝试,在这里四处寻找一些答案,我无法从MySQL加载图像 基本上,用户从OpenFileDialog中选择一张图片,然后将其加载到PictureBox中,这样效果很好。 然后,用户单击一个按钮,该按钮将通过将图片转换为字节[]将图片保存在数据库中。 最后,当我尝试将图片加载到另一个PictureBox时,它是全黑的 将图片添加到数据库时: public void PictureToDB(Image img) { MemoryStream tmpStream = new Me

经过一段时间的尝试,在这里四处寻找一些答案,我无法从MySQL加载图像

基本上,用户从OpenFileDialog中选择一张图片,然后将其加载到PictureBox中,这样效果很好。 然后,用户单击一个按钮,该按钮将通过将图片转换为字节[]将图片保存在数据库中。 最后,当我尝试将图片加载到另一个PictureBox时,它是全黑的

将图片添加到数据库时:

public void PictureToDB(Image img)
{
    MemoryStream tmpStream = new MemoryStream();
    img.Save(tmpStream, ImageFormat.Png);
    tmpStream.Seek(0, SeekOrigin.Begin);
    byte[] imgBytes = new byte[5000];
    tmpStream.Read(imgBytes, 0, 5000);

    string query = "Update TABLE Set IMG = @imgBytes";
    MySqlParameter param = new MySqlParameter("@img", imgBytes);

    //Skipping connection to db and all... it works
}
正在从数据库获取图片:

public void DBToPicture()
{
    string query = "Select IMG From TABLE Where ...";
    //Skipping Command lines ...
    MySqlDataReader reader = command.ExecuteReader();
    DataTable myDT.Load(reader);

    Byte[] data = new Byte[0];
    data = (Byte[])(myDT.Rows[0]["PHOTOLOISANT"]);
    MemoryStream mem = new MemoryStream(data);
    MyPictureBox.Image = Image.FromStream(mem);
}
更多信息:

MySQL中的类型为varbinary8000,在更新后包含89504E470D0A1A0A0000000D4948445200000280000001E008020000BAB34B300000017352474200AECE9000000467444D41002E2E 在第一个PictureBox中加载图像时,图像将转换为位图。 用于存储:

conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
            conn.Open();
            FileStream fs;
            Byte[] bindata;
            MySqlParameter picpara;
            cmd = new MySqlCommand("INSERT INTO mypic (pic) VALUES(?pic)", conn);
            picpara = cmd.Parameters.Add("?pic", MySqlDbType.MediumBlob);
            cmd.Prepare();

//txtPicPath is the path of the image, e.g. C:\MyPic.png

            fs = new FileStream(txtPicPath.Text, FileMode.Open, FileAccess.Read);
            bindata = new byte[Convert.ToInt32(fs.Length)];
            fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            picpara.Value = bindata;
            cmd.ExecuteNonQuery();
要检索它,请执行以下操作:

if (conn == null) // Just to make sure that the connection was not severed
        {


                conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
                conn.Open();

        }
        MemoryStream ms = new MemoryStream();
        FileStream fs;
        Byte[] bindata;

        cmd = new MySqlCommand("SELECT pic FROM mypic WHERE id=3", conn);
        bindata = (byte[])(cmd.ExecuteScalar());



        ms.Write(bindata, 0, bindata.Length);
        pb2.Image = new Bitmap(ms);

        fs = new FileStream(name, FileMode.Create, FileAccess.Write);
        ms.WriteTo(fs);
我将使用这些步骤从MySql存储和检索图像

真诚地

蒂亚古·拉詹德兰


**如果答案有帮助,请将其标记为答案;如果答案没有帮助,请取消标记。

图片是否必须存储到数据库中?请阅读以下内容:并且您的IMG应该是BLOB而不是varbinary8000,当您使用BLOB时,允许您将其存储为二进制数据,然后将其作为byte[]@ChesterCobus检索,它不必位于数据库中,但我希望它能。只是因为我想知道它是如何工作的,希望这能有所帮助:@ChesterCobus感谢您在BLOB btw中提到它!我必须使用binData=newbyte[Convert.ToInt32fs.Length-1];和fs.ReadbinData,0,Convert.ToInt32fs.Length-1;为了让它工作,否则我就错了。但是现在它工作得很好,非常感谢!干杯