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)
{
}
}
}