在列表视图中绘制图像

Drawing image in listview

我正在尝试制作此目录应用程序以其 "title" 和 "category" 显示图像,但由于错误我似乎无法显示图像 在

images.Images.Add(row["id"].ToString(), new Bitmap(image_stream));

这就是我的全部代码。我需要解决方案,以便我可以在列表视图中打印图像及其相应的详细信息。谢谢。

 namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();

                comboBox1.Items.Add("Books");
                comboBox1.Items.Add("Games");
                comboBox1.Items.Add("Music");
            }

            SqlConnection connection = new SqlConnection("Data Source=DESKTOP-4T5BLQ6\SQLEXPRESS;Initial Catalog=CatalogDB;Integrated Security=True");
            SqlCommand command = new SqlCommand();
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            SqlDataReader dataReader;
            DataTable dataTable = new DataTable();
            MemoryStream stream1;
            byte[] photo_array;

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                connection.Open();
                int i = 0;

                MemoryStream stream = new MemoryStream();
                command.CommandText = "insert into EntryTable(Title,Category,Image) values('" + textBox1.Text + "','" + comboBox1.Text + "','" + pictureBox1.Image + "')";
                pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] pic = stream.ToArray();

                if (i > 0)
                {
                    MessageBox.Show("Saved new item in index" + i);
                }

                connection.Close();
                MessageBox.Show("Made New Entry");
                showData();
                clear();
            }

            void clear()
            {
                textBox1.Clear();
                pictureBox1.Image = null;
                comboBox1.SelectedIndex = -1;
            }

            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Images only. |*.jpg; *jpeg; *.png; *.gif; *.bmp;";
                DialogResult result = ofd.ShowDialog();
                if (result == DialogResult.OK)
                {
                    pictureBox1.Image = Image.FromFile(ofd.FileName);
                }
            }
            private void showData()
            {
                connection.Open();
                listView1.Clear();
                ImageList images = new ImageList();
                images.ColorDepth = ColorDepth.Depth32Bit;
                listView1.LargeImageList = images;
                listView1.LargeImageList.ImageSize = new System.Drawing.Size(100 , 100);
                command.Connection = connection;
                command.CommandText = "SELECT * FROM EntryTable";
                dataAdapter.SelectCommand = command;
                dataTable.Clear();
                dataAdapter.Fill(dataTable);
                foreach (DataRow row in dataTable.Rows)
                {
                    var image_buffer = (byte[])(row["Image"]);
                    MemoryStream image_stream = new MemoryStream(image_buffer, true);
                    image_stream.Write(image_buffer, 0, image_buffer.Length);
                    images.Images.Add(row["id"].ToString(), new Bitmap(image_stream));
                    image_stream.Close();
                    ListViewItem listItem = new ListViewItem();
                    listItem.Text = row["Title"].ToString();
                    listItem.ImageKey = row["Image"].ToString();
                    listView1.Items.Add(listItem);
                    listView1.Items.Add(row["Category"].ToString());
                    listView1.Items.Add(row["Title"].ToString());

                }



                connection.Close();
            }
        }
    }

索里尔,

你需要更具体地说明你在 SO 上的要求。您原来的 post 仅表示您有错误并且它阻碍了您的应用程序开发(欢迎编码)。你甚至不告诉我们你遇到了什么错误。

现在你问如何实现try catch?你应该自己做一些研究。至于try catch,可以开始Here.

您的代码表明您对 windows winforms 的实例化方式及其事件顺序缺乏了解。对我来说,这表明在修复此问题后您将遇到更多问题,因此我将在您的代码中添加一个 try catch。它会将异常写入您的控制台。

foreach (DataRow row in dataTable.Rows)
  {
    var image_buffer = (byte[])(row["Image"]);
    MemoryStream image_stream = new MemoryStream(image_buffer, true);
    image_stream.Write(image_buffer, 0, image_buffer.Length);
    try 
    {
        images.Images.Add(row["id"].ToString(), new Bitmap(image_stream));
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.Message);
    }
    image_stream.Close();
    ListViewItem listItem = new ListViewItem();
    listItem.Text = row["Title"].ToString();
    listItem.ImageKey = row["Image"].ToString();
    listView1.Items.Add(listItem);
    listView1.Items.Add(row["Category"].ToString());
    listView1.Items.Add(row["Title"].ToString());
    }

您真正的问题是将图像存储到数据库的格式以及检索图像的方式。

此致,

马克