Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 试图创建带有图像的动态按钮,但从数据库中获取图像时遇到问题_C#_Wpf_Button - Fatal编程技术网

C# 试图创建带有图像的动态按钮,但从数据库中获取图像时遇到问题

C# 试图创建带有图像的动态按钮,但从数据库中获取图像时遇到问题,c#,wpf,button,C#,Wpf,Button,好的,我的数据库中有一列(Image)[Blob],我应该在那里获取动态创建的按钮的图像。我想设置每个按钮对应的图像 我尝试了一个建议的代码,但它不起作用,请看一看 conn.Open(); using (MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT GarmentName FROM tblthesis", conn)) {

好的,我的数据库中有一列(Image)[Blob],我应该在那里获取动态创建的按钮的图像。我想设置每个按钮对应的图像

我尝试了一个建议的代码,但它不起作用,请看一看

            conn.Open();

            using (MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT GarmentName FROM tblthesis", conn))
            {
                ds = new DataSet();
                adapter.Fill(ds);
                MainWindow mainWin = new MainWindow();

                    //button.Click += new RoutedEventHandler(button_Click);
                    foreach (DataRow dataRow in ds.Tables[0].Rows)
                    {
                        var imageBuffer = (byte[])dataRow["Image"];
                        var bitmapImage = new BitmapImage();

                        using (var memoryStream = new MemoryStream(imageBuffer))
                        {
                            bitmapImage.BeginInit();
                            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                            bitmapImage.StreamSource = memoryStream;
                            bitmapImage.EndInit();
                        }

                        Button button = new Button();
                        button.Content = dataRow[0].ToString();
                        button.BorderThickness = new Thickness(7, 7, 7, 7);
                        button.Background = new SolidColorBrush(Colors.Transparent);
                        button.Width = 185;
                        button.Height = 200;
                        button.Background = new ImageBrush(bitmapImage);
                        mainWin.sp.Children.Add(button);
这就是我保存图像的方式

             try
        {
            if (imageName != "")
            {
                //Initialize a file stream to read the image file
                FileStream fs = new FileStream(imageName, FileMode.Open, FileAccess.Read);

                //Initialize a byte array with size of stream
                byte[] imgByteArr = new byte[fs.Length];

                //Read data from the file stream and put into the byte array
                fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));

                //Close a file stream
                fs.Close();

                using (MySqlConnection conn = new MySqlConnection(constr))
                {
                    conn.Open();
                    string sql = "insert into tblthesis(GarmentType,GarmentName,Image) values('" + txtType.Text + "','" + txtname.Text + "',@img)";
                    using (MySqlCommand cmd = new MySqlCommand(sql, conn))
                    {
                        //Pass byte array into database
                        cmd.Parameters.Add(new MySqlParameter("img", imgByteArr));
                        int result = cmd.ExecuteNonQuery();
                        if (result == 1)
                        {
                            MessageBox.Show("Image added successfully.");

                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
这就是我找回自己形象的方式

   ListBoxItem lb = (lbpics.SelectedItem as ListBoxItem);

        using (MySqlConnection conn = new MySqlConnection(constr))
        {
            conn.Open();
            ds = new DataSet();

            using (MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT Image FROM tblthesis where GarmentName = '" + lb.Content.ToString() + "'", conn))
            {
                 adapter.Fill(ds);

        byte[] data = (byte[])ds.Tables[0].Rows[0][0]; 

        MemoryStream strm = new MemoryStream(); 

        strm.Write(data, 0, data.Length); 

        strm.Position = 0;            
        System.Drawing.Image img = System.Drawing.Image.FromStream(strm); 
        BitmapImage bi = new BitmapImage(); 
        bi.BeginInit(); 
        MemoryStream ms = new MemoryStream(); 
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        ms.Seek(0, SeekOrigin.Begin);  
        bi.StreamSource = ms; 
        bi.EndInit(); 
        image2.Source = bi;

            }
            conn.Close();
        }
    }

感谢您的帮助。谢谢

“不工作”的确切含义尚不清楚。你到底遇到了什么问题?第一个代码块中的非工作代码与第三个代码块中的(工作?)代码如何关联?如果第三个块有效,为什么不使用
byte[]data=(byte[])ds.Tables[0].Rows[0][0]从那里?或者只是
var-imageBuffer=(字节[])数据行[0]我刚刚使用了第二块和第三块代码作为参考,我只在第一块有问题,你看,没有代码
var-imageBuffer=(byte[])dataRow[“Image”]
直到
bitmapImage.EndInit()我的代码工作正常,但没有按钮的图像背景。我只想根据我的列(图像)中的内容设置按钮的图像背景。参数缺少@。cmd.Parameters.Add(新的MySqlParameter(“@img”,imgByteArr));由于动态查询,您对SQL注入攻击的抵抗力很弱。我想要的是,当我保存数据时,我想自动创建一个动态按钮,该按钮具有与它一起保存的图像的背景属性。