Download image from listview C#

Manchi 21 Reputation points
2021-03-30T22:22:10.947+00:00

I am displaying images from database into ListView. is working fine, but my idea got lost in trying to download an image or multiple from ListView to any folder on my desktop. I don't know if I can get a professional to help me out

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Data.SqlClient;

namespace data_pictures
{
    public partial class Form1 : Form
    {
        SqlConnection connect = new SqlConnection(@"Data Source=****-pc;Initial Catalog=*****;Integrated Security=***");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lvwBooks.View = View.LargeIcon;
            lvwBooks.LargeImageList = imlLargeIcons;
            {
                SqlCommand cmd = new SqlCommand("SELECT name, data FROM gallery", connect);
                connect.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    listviewImage.Items.Clear();
                    imlLargeIcons.Images.Clear();

                    while (reader.Read())
                    {
                        if (!reader.IsDBNull(1))
                        {
                            Bitmap bm = BytesToImage((byte[])reader.GetValue(1));
                            float source_aspect = bm.Width / (float)bm.Height;

                            AddImageToImageList(imlLargeIcons,
                            bm, reader[0].ToString(),
                            imlLargeIcons.ImageSize.Width,
                            imlLargeIcons.ImageSize.Height);
                        }
                       listviewImage.AddRow(reader[0].ToString(), reader[0].ToString());
                    }
                }
            }
        }

        private void AddImageToImageList(ImageList iml, Bitmap bm, string key, float wid, float hgt)
        {
            Bitmap iml_bm = new Bitmap(iml.ImageSize.Width, iml.ImageSize.Height);
            using (Graphics gr = Graphics.FromImage(iml_bm))
            {
                gr.Clear(Color.Transparent);
                gr.InterpolationMode = InterpolationMode.High;

                RectangleF source_rect = new RectangleF(0, 0, bm.Width, bm.Height);
                RectangleF dest_rect = new RectangleF(0, 0, iml_bm.Width, iml_bm.Height);
                dest_rect = ScaleRect(source_rect, dest_rect);

                gr.DrawImage(bm, dest_rect, source_rect, GraphicsUnit.Pixel);
            }

            iml.Images.Add(key, iml_bm);
        }
        private Bitmap BytesToImage(byte[] bytes)
        {
            using (MemoryStream image_stream = new MemoryStream(bytes))
            {
                Bitmap bm = new Bitmap(image_stream);
                return bm;
            }
        }
        private RectangleF ScaleRect(RectangleF source_rect, RectangleF dest_rect)
        {
            float source_aspect = source_rect.Width / source_rect.Height;
            float wid = dest_rect.Width;
            float hgt = dest_rect.Height;
            float dest_aspect = wid / hgt;

            if (source_aspect > dest_aspect)
            {
                hgt = wid / source_aspect;
            }
            else
            {
                wid = hgt * source_aspect;
            }

            float x = dest_rect.Left + (dest_rect.Width - wid) / 2;
            float y = dest_rect.Top + (dest_rect.Height - hgt) / 2;
            return new RectangleF(x, y, wid, hgt);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,837 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,302 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,557 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-03-31T06:58:17.017+00:00

    Hi Manchi-0330,
    For each image in your image list, you can try the following code:

    foreach (Image image in listView1.LargeImageList.Images)  
    {  
        string filename = ""; // with your own supplied directory and file name  
        image.Save(filename, ImageFormat.Jpeg);  
    }  
    

    You can also save each image to folder from listview by using ListView.SelectedItems property.
    Sevo has provided a simple code example in this thread you can refer to.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments