Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello,

There is not displaying images in ListView in C# .net Windows Application.

There is only displaying System.Byte[] in listview.

C#
            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
Updated 8-Apr-13 23:28pm
v2

 
Share this answer
 
Comments
[no name] 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.
[no name] 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.
Try below code

C#
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[^]
 
Share this answer
 
Comments
[no name] 9-Apr-13 5:48am    
Please give me the correct code, my image already saved in database, i want to only display in listview.
vijay__p 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.
[no name] 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.
[no name] 9-Apr-13 6:21am    
So, Please correct my code!
vijay__p 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));
C#
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);
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900