在列表视图中绘制图像

本文关键字:绘制 图像 视图 列表 | 更新日期: 2023-09-27 18:07:00

我试图使这个目录应用程序显示其"标题"answers"类别"的图像,但我似乎无法显示图像,因为一个错误在

那一行
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();
            }
        }
    }

在列表视图中绘制图像

Xoriel,

你需要更具体地说明你在SO上的要求。你原来的帖子只表明你有一个错误,这是阻碍你的应用程序开发(欢迎编码)。你甚至没有告诉我们你得到了什么错误。

现在你问如何实现一个try catch?你应该自己做一些调查。至于try catch,你可以从这里开始。

你的代码表明缺乏对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());
    }

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

问候,Marc