2017-12-28 8 views
0

データベースイメージをlistviewに表示したいのですが、動作しません。私は試みたが、誰も助けることができない。データベースからリストビューへのイメージの取得方法c#

 ListViewItem liv = new ListViewItem(read[0].ToString()); 
       liv.SubItems.Add(read[1].ToString()); 
       liv.SubItems.Add(read[2].ToString()); 
       liv.SubItems.Add(read[3].ToString()); 
       listView1.Items.Add(liv); 

データベースのインデックス[3]は、ここに保存されたイメージです。私が見ることができるものについては

画像におけるエラーショー

error System.Byte[]

+3

? –

+0

それはインデックス[3] –

+1

のsystem.Byte []のみを表示するので、より詳細な質問を改善する必要があります。 –

答えて

0

、あなたのイメージは、データベースに格納されたByteArray形式で取得されます。あなたはビットマップイメージにByteArrayのストリームを変換する必要があります。

Image _image = new Bitmap(new MemoryStream((Byte[])read[3])); 

この画像は、複数の方法で参照する必要があるため、クラスレベルでそれを定義します。

listView1.OwnerDraw = true; 

は、データソースから項目を取得します:

using System.Drawing; 
using System.Drawing.Text; 

Image _image; 

は、サブアイテムは画像を表示するようにするには、アイテムにyourselftペイントので、あなたのリストビューのOwnerDrawするプロパティを設定する必要があります
(... LOOP ...) 

//Define a new ListView Item... 
ListViewItem _LVItem = new ListViewItem(read[0].ToString()); 
ListViewItem.ListViewSubItemCollection _ItemsCollection = 
      new ListViewItem.ListViewSubItemCollection(_LVItem); 

//... and SubItems 
_ItemsCollection.Add(new ListViewItem.ListViewSubItem(_LVItem, read[1].ToString())); 
_ItemsCollection.Add(new ListViewItem.ListViewSubItem(_LVItem, read[2].ToString())); 
//No text here, this is where the image will be drawn 
_ItemsCollection.Add(new ListViewItem.ListViewSubItem(_LVItem, "")); 

//Create a new Bitmap using a MemoryStream 
_image = new Bitmap(new MemoryStream((Byte[])read[3])); 

listView1.Items.Add(_LVItem); 

(... LOOP ...) 

listView1.DrawColumnHeaderlistView1.DrawSubItem

ためのEventHandlerを作成します。
//You need to draw both Headers... 
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) 
{ 
    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; 
    //Let the system draw these headers, nothing to do here 
    e.DrawDefault = true; 
} 

//... and SubItems 
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) 
{ 
    //If this is the column where the image is shown, draw it 
    if (e.ColumnIndex == 3) 
    { 
     //Position the image in the middle of its Column 
     //This will be re-calculated when the Column is resized 
     int _XLocation = (e.SubItem.Bounds.X + 
         (e.SubItem.Bounds.Width/2) - 
         (e.SubItem.Bounds.Height/2)); 

     //Draw the Image resized to the Height of the row 
     e.Graphics.DrawImage(_image, new Rectangle(_XLocation, e.SubItem.Bounds.Y, e.SubItem.Bounds.Height, e.SubItem.Bounds.Height)); 
    } 

    //Draw the other columns using their pre-defined values 
    e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 
    e.Graphics.DrawString(e.SubItem.Text, 
         e.SubItem.Font, 
         new SolidBrush(e.SubItem.ForeColor), 
         e.SubItem.Bounds.Location.X, 
         e.SubItem.Bounds.Location.Y); 
} 

そして、これが結果です:親愛なる、エラー何

enter image description here

0
// To Convert you byte array to Images you need to convert it to a memory stream first 
    // You will need to add: 
    // using System.IO; 
    // And you need to add an ImageList named "imageList1" to your form. 
    // Then Initialize the ImageList object with images. 

    for (int i = 0; i < read.Length; i++) 
    { 
     try 
     { 
      MemoryStream memStream = new MemoryStream(read[i]); 
      this.imageList1.Images.Add(Image.FromStream(memStream)); 
     } 
     catch 
     { MessageBox.Show("Image at index (" + i.ToString() + ") is not an image file"); } 
    } 

    //Now You can assign the ImageList objects to your ListView. 

    this.listView1.View = View.SmallIcon; 
    this.listView1.SmallImageList = this.imageList1; 

    for (int j = 0; j < this.imageList1.Images.Count; j++) 
    { 
     ListViewItem liv = new ListViewItem(); 
     liv.ImageIndex = j; 
     this.listView1.Items.Add(liv); 
    } 
関連する問題