如何在运行时使用VB.NET将输入图像添加到列表视图 [英] How to add Input Images to List view at runtime using VB.NET

查看:153
本文介绍了如何在运行时使用VB.NET将输入图像添加到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从用户那里输入了5张图片,并将其显示在相册中,现在我也希望这些图片也显示在列表视图中.如何在列表视图中同时传递它们.我尝试了以下代码,但它在相册中显示的图像不在listview中.

I have taken input of 5 images from user and showed it in photogallery now I want these images in list view too. How should I pass them in list view simultaneously. I tried the below code but it shows images in photogallery not in listview.

Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.Filter =
    "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
    "All files (*.*)|*.*"
    OpenFileDialog1.Multiselect = True
    Dim index As New Integer
    OpenFileDialog1.Title = "Select Photos"
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    If OpenFileDialog1.FileNames.Length > 5 Then
    MessageBox.Show("Please select no more than 5 files")
    Exit Sub
    End If
    If OpenFileDialog1.FileNames.Length < 5 Then
    MessageBox.Show("Please select 5 files")
    Exit Sub
    End If
    For Each file As String In OpenFileDialog1.FileNames
    Dim imageControl As New PictureBox()
    imageControl.Height = 100
    imageControl.Width = 100
    Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
    Dim myBitmap As New Bitmap(file)
    Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
    imageControl.Image = myThumbnail
    PhotoGallary.Controls.Add(imageControl)
    Dim imageListLarge As New ImageList()
    imageListLarge.Images.Add(myBitmap)
    ListView_images.LargeImageList = imageListLarge
    Me.Controls.Add(ListView_images)
    Next
    btn_Save.Enabled = True
    BrowseMultipleFilesButton.Enabled = False
    End If
    End Sub

推荐答案

您当然可以通过同步更新适当的图像列表和ListView内容来做到这一点(这是什么问题?),但是为什么呢?问题不是您不能做到这一点,问题在于图像显示机制是为不同功能设计的:显示一组固定图像,并根据图像列表中的索引对其进行选择.在这种方法中,图像本身不被视为数据的一部分.

对于照相馆而言,图像本身是数据的关键部分.因此,与其进行繁琐的工作,不如改变设计.如果您想使用ListView(但是为什么?确定吗?),一种合适的技术是自定义绘制:
https://msdn.microsoft. com/en-us/library/system.windows.forms.listview.ownerdraw%28v = vs.110%29.aspx [ https://msdn.microsoft. com/en-us/library/system.windows.forms.listview.drawitem%28v = vs.110%29.aspx [ https://msdn.microsoft.com/en -us/library/system.windows.forms.datagridviewimagecolumn(v = vs.110).aspx [ https://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewimagecell%28v = vs.110%29.aspx [ https://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview%28v = vs.110%29.aspx [
Of course you can do it by updating the appropriate image list and ListView content in sync (what''s the problem?) but why? The problem is not that you cannot do it, the problem is that the image showing mechanism is designed for a very different functionality: showing a set of fixed images choosing them by their indices in a list of images. In this approach, the images themselves are not considered as part of data.

With a photo gallery, the image itself is the key part of data. So, instead of building a cumbersome work around, better change the design. If you want to stay with ListView (but why? are you sure?), one appropriate technique would be custom draw:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem%28v=vs.110%29.aspx[^].

But who told you it should be the ListView (itself quite a cumbersome control)?
It would be way more natural to use System.Windows.Forms.DataGridView with some instances of System.Windows.Forms.DataGridViewImageCell, preferably by having one or more whole columns of the type System.Windows.Forms.DataGridViewImageColumn:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecolumn(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewimagecell%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview%28v=vs.110%29.aspx[^].

Much simpler, isn''t it?

—SA


这篇关于如何在运行时使用VB.NET将输入图像添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆