question

Manchi-0330 avatar image
0 Votes"
Manchi-0330 asked DanielZhang-MSFT answered

Download image from listview C#

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)
         {
               
         }
     }
 }
dotnet-csharpsql-server-transact-sqlwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.