将图片添加到图片框中的图像列表中

时间:2017-05-26 20:42:16

标签: vb.net imagelist

如何将图片添加到图片框中的图像列表中。 像这样的东西,但对于图片框 这是代码

Dim img As System.Drawing.Image.FromFile("DSCF00082.jpg")
imageList1.Images.Add(img)

2 个答案:

答案 0 :(得分:0)

PictureBox.ImagePictureBox.ImageLocation属性包含您想要的信息:

Imports System.Drawing

Function DoStuff()

    Dim img As Bitmap = GetPictureBoxImage(pictureBox1.Image)
    imageList1.Images.Add(img)

End Function

Function GetPictureBoxImage(pictureBox As PictureBox) As Bitmap

    If pictureBox.Image Is Not Nothing
        Return New Bitmap(pictureBox1.Image)
    Else
        Return New Bitmap(pictureBox1.ImageLocation)
    End If

End Function

答案 1 :(得分:0)

好吧,经过几个小时的搜索和研究代码,我找到了解决方案,做我想要的 这是我的工作代码

Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports System.Drawing.Bitmap
Imports System.IO

<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Class Form1

Dim imgList As New ImageList
Dim listindexnumber = 1
Dim truelink
Private WithEvents CurrentDocument As HtmlDocument
Dim MousePoint As Point
Dim Ele As HtmlElement

Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
    'Dim linkx1 = "https://searx.me/?q=" + TextBox1.Text + "&categories=images"
    'Dim linkx2 As String = "https://searx.me/?q=" + TextBox1.Text + "&categories=images"

    'Process.Start("https://searx.me/?q=" + TextBox1.Text + "&categories=images")

    'Dim item As ListViewItem = ListView1.Items.Add(linkx1)

    ' Set the index into the image list 
    'item.ImageIndex = listindexnumber

    'WB.Navigate("https://www.google.pt/search?hl=pt-PT&site=imghp&tbm=isch&source=hp&biw=1440&bih=721&q=images+from+https%3A%2F%2Fsearx.me%2F&oq=images+from+https%3A%2F%2Fsearx.me%2F&gs_l=img.12...70311.90487.0.91830.17.16.1.0.0.0.118.1241.12j2.14.0....0...1.1.64.img..2.14.1139.0..0j0i10k1j0i30k1j0i19k1.hAR3FiZi1KU")
    WB.Navigate("https://www.google.pt/search?hl=pt-PT&site=imghp&tbm=isch&source=hp&biw=1440&bih=721&q=" + TextBox1.Text + "&oq=desenhos&gs_l=img.1.1.0l10.17285.21226.0.23260.14.14.0.0.0.0.140.743.4j3.7.0....0...1.1.64.img..7.7.742.0.TbA7KXlvWXM" + TextBox1.Text + "=img.12...70311.90487.0.91830.17.16.1.0.0.0.118.1241.12j2.14.0....0...1.1.64.img..2.14.1139.0..0j0i10k1j0i30k1j0i19k1.hAR3FiZi1KU")

End Sub

' Hides script errors without hiding other dialog boxes.
Private Sub SuppressScriptErrorsOnly(ByVal browser As WebBrowser)

    ' Ensure that ScriptErrorsSuppressed is set to false.
    browser.ScriptErrorsSuppressed = True



End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    btnExtract.Hide()

End Sub

Private Sub SplitContainer1_Panel1_Paint(sender As Object, e As PaintEventArgs)

End Sub

Private Sub btnExtract_Click(sender As Object, e As EventArgs) Handles btnExtract.Click
    'For every link in the current document...         
    For Each ele As HtmlElement In WB.Document.Links
        'Get whatever text there is in the 'href' attribute
        Dim eletarget As String = ele.GetAttribute("href")
        'Add it to the listbox
        LstMain.Items.Add(eletarget)
        'Carry on to the next link
    Next
    For Each ele As HtmlElement In WB.Document.Images
        'Get whatever text there is in the 'href' attribute
        Dim eletarget As String = ele.GetAttribute("img")
        'Add it to the listbox
        LstMain.Items.Add(eletarget)
        'Carry on to the next link
    Next
End Sub

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
    If My.Application.CommandLineArgs IsNot Nothing AndAlso _
    My.Application.CommandLineArgs.Count > 0 Then
        Dim UserFile As String = My.Application.CommandLineArgs(0)


    End If
    Dim tempdir As String = "C:\MediaZip"
    Dim logDirectoryProperties As System.IO.DirectoryInfo
    If My.Computer.FileSystem.DirectoryExists("C:\MediaZip") Then
        IO.Directory.Delete(tempdir, True)
    End If
    If SaveFD.ShowDialog() = DialogResult.OK Then
        Dim sb As New System.Text.StringBuilder()

        For Each o As Object In LstMain.Items
            sb.AppendLine(o)
        Next

        System.IO.File.WriteAllText(SaveFD.FileName, sb.ToString())
    End If
End Sub

Private Sub LstMain_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LstMain.SelectedIndexChanged

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    LstMain.Items.Clear()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs)

End Sub

Private Sub WB_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
    CurrentDocument = WB.Document
    '
    RichTextBox1.Text = WB.DocumentText.ToString 'Gets the source of the current website loaded
    '
    For Each ele As HtmlElement In WB.Document.Links
        Dim eletarget As String = ele.GetAttribute("href")
        ListBox1.Items.Add(eletarget) 'Adds the Links to the ListBox
        LstMain.Items.Add(eletarget) 'Adds the Links to the ListBox
    Next
    '
    For Each ele As HtmlElement In WB.Document.All
        '
        If ele.GetAttribute("src").ToLower.Contains(".jpg") Then
            Dim eletarget As String = ele.GetAttribute("href")
            Dim imgsrc As String = ele.GetAttribute("src")
            ListBox2.Items.Add(imgsrc) 'Adds all .jpg images to the ListBox
            LstMain.Items.Add(eletarget) 'Adds the Links to the ListBox
        End If
        '
        If ele.GetAttribute("src").ToLower.Contains(".png") Then
            Dim eletarget As String = ele.GetAttribute("href")
            Dim imgsrc As String = ele.GetAttribute("src")
            ListBox2.Items.Add(imgsrc) 'Adds all .png images to the ListBox
            LstMain.Items.Add(eletarget) 'Adds the Links to the ListBox
        End If
        '
        If ele.GetAttribute("src").ToLower.Contains(".gif") Then
            Dim eletarget As String = ele.GetAttribute("href")
            Dim imgsrc As String = ele.GetAttribute("src")
            ListBox2.Items.Add(imgsrc) 'Adds all .gif images to the ListBox
            LstMain.Items.Add(eletarget) 'Adds the Links to the ListBox
        End If
        '
        If ele.GetAttribute("src").ToLower.Contains(".bmp") Then
            Dim eletarget As String = ele.GetAttribute("href")
            Dim imgsrc As String = ele.GetAttribute("src")
            ListBox2.Items.Add(imgsrc) 'Adds all .bmp images to the ListBox
            LstMain.Items.Add(eletarget) 'Adds the Links to the ListBox
        End If
        '
    Next
End Sub

Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
    Dim imgList As New ImageList
    'WB.Document.ExecCommand("SelectAll", True, vbNull)
    'WB.Document.ExecCommand("Copy", True, vbNull)
    Clipboard.GetImage()
    If My.Computer.Clipboard.ContainsImage Then
        PictureBox1.Image = My.Computer.Clipboard.GetImage

        LstMain.Items.Add(My.Computer.Clipboard.GetText)
        For Each ele As HtmlElement In WB.Document.Links
            Dim eletarget As String = ele.GetAttribute("href")
            ListBox1.Items.Add(eletarget) 'Adds the Links to the ListBox
        Next
    End If



End Sub

Private Sub PictureBox1_Click(sender As Object, e As EventArgs)

End Sub


Private Sub ListBox2_Click(sender As Object, e As System.EventArgs) Handles ListBox2.Click
    '
    PictureBox1.ImageLocation = ListBox2.SelectedItem.ToString 'Gets the selected image in the ListBox and previews it in the PictureBox
    '
End Sub
Private Sub ListBox2_DoubleClick(sender As Object, e As System.EventArgs) Handles ListBox2.DoubleClick
    '
    WB.Navigate(ListBox2.SelectedItem.ToString) 'Selected Image in ListBox navigates to the Image URL in the WebBrowser
    '
End Sub
Private Sub ListBox1_DoubleClick(sender As Object, e As System.EventArgs) Handles ListBox1.DoubleClick
    '
    WB.Navigate(ListBox1.SelectedItem.ToString) 'Selected Link in ListBox navigates to the link in the WebBrowser
    '
End Sub
End Class