Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET4
Hello,
 
There is not displaying images in ListView in C# .net Windows Application.
 
There is only displaying System.Byte[] in listview.
 
            DataTable dt = obj.GetFloorTileName(CmbFloorTiles.Text);
            listFloorTiles.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
 
                DataRow drow = dt.Rows[i];
                
//This is a Image field (draw["FloorTexture"].ToString());
//My Image field dataType is Image in sql server.

                ListViewItem lvi = new ListViewItem(drow["FloorTexture1"].ToString());
                lvi.SubItems.Add(drow["FloorTileName"].ToString());
                listFloorTiles.Items.Add(lvi);
            }
Please help me, How can we retrieve images from database to listview control in list format.
 
Thanks.
 
Ankit Agarwal
Software Engineer
Posted 9-Apr-13 0:10am
Edited 9-Apr-13 0:28am
Maciej Los151.2K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
ankit.agg1984 at 9-Apr-13 5:36am
   
i am working on windows application so I am not trying to asp.net code, please give me a correct solution for my application.
ankit.agg1984 at 9-Apr-13 5:40am
   
Your both solution has waste for my application.
I want to display only Image and related text from database, So please give me correct solution for that.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try below code
 
byte[] bytes = (byte[])draw["FloorTexture"];
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
            Image1.ImageUrl = "data:image/png;base64," + base64String;
 
You can also use Generic handler to show image
http://forums.asp.net/t/1372698.aspx[^]
  Permalink  
Comments
ankit.agg1984 at 9-Apr-13 5:48am
   
Please give me the correct code, my image already saved in database, i want to only display in listview.
patel_vijay at 9-Apr-13 6:01am
   
Above code is to display image from database only.
draw is object of DataRow which you have written "drow". Image1 is "<asp:Image />".
You need to set base 64 string as image URL as displayed above.
ankit.agg1984 at 9-Apr-13 6:20am
   
This code is not suitable in windows application like:-
 
DataTable dt = obj.GetFloorTileName(CmbFloorTiles.Text);
dgvFloorTiles.DataSource = dt;
listFloorTiles.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
 
DataRow drow = dt.Rows[i];
byte[] bytes = (byte[])drow["FloorTexture1"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
ListViewItem lvi = new ListViewItem(base64String);
lvi.SubItems.Add(drow["FloorTileName"].ToString());
listFloorTiles.Items.Add(lvi);
}
 
When we use this code so, my image is not displaying, it's displaying only image binary code.
ankit.agg1984 at 9-Apr-13 6:21am
   
So, Please correct my code!
patel_vijay at 9-Apr-13 7:13am
   
You can convert byte[] to Image as below.
For windows application i have used PictureBox and set its Image property to by converting byte[] to Image as below, so it will display image in PictureBox. Hope this will help.
 
byte[] arr;
pictureBox1.Image = Image.FromStream(new MemoryStream(arr));
patel_vijay at 9-Apr-13 7:31am
   
Your refnied code
 
DataTable dt = obj.GetFloorTileName(CmbFloorTiles.Text);
dgvFloorTiles.DataSource = dt;
listFloorTiles.Items.Clear();
ImageList list = new ImageList();
list.ImageSize = new Size(256, 256);
 
for (int i = 0; i < dt.Rows.Count; i++)
{
 
DataRow drow = dt.Rows[i];
byte[] bytes = (byte[])drow["FloorTexture1"];
list.Images.Add(Image.FromStream(new MemoryStream(arr)));
}
 
listFloorTiles.LargeImageList = list;
 
for (int j = 0; j < list.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
listFloorTiles.Items.Add(item);
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

        public byte[] ImageAArray(System.Drawing.Image imagen)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            imagen.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }
        public System.Drawing.Image ArrayAImage(byte[] ArrBite)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(ArrBite);
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
            return returnImage;
        }
 
        lvImagenes.Clear();//List View
        System.Collections.ArrayList pics = PicturesBO.GetPics();//Get Images DB
        ImageList imgList = new ImageList();//Image List
        imgList.ImageSize = new Size(120, 120);
        foreach (IMAGESDB i in pics)// Entity IMAGEDB (SQL Server Type IMAGE, C# type byte[])
        {
            System.Drawing.Image img = ArrayAImage(i.IMAGEFILE);
            imgList.Images.Add(img);                    
        }
        lvImagenes.View = View.LargeIcon;
        lvImagenes.LargeImageList = imgList;
        for (int j = 0; j < imgList.Images.Count; j++)
        {
            ListViewItem item = new ListViewItem();
            item.ImageIndex = j;
            lvImagenes.Items.Add(item);
        }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 9 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100