我使用vb .net选择一个文件夹。代码如下 -
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
但问题是我对显示图像一无所知(每页8张图片)
答案 0 :(得分:0)
VB.Net版本:
由于您要创建打印预览,我建议您仅为打印预览创建新的Form
并跟踪用户输入的PageNumber
Dim pageNumber As Integer = 0 'start from 0, but change this according to the user input accordingly
您可以使用Directory.GetFiles
System.IO
从指定文件夹中获取文件列表
Dim rawpaths As List(Of String) = Directory.GetFiles(folder).ToList() 'This gets all files, not only images
您可能需要根据this提供可接受的图片扩展名列表。
Dim validImageFormat As New List(Of String) From {"jpg", "bmp", "png", "jpeg", "gif", "tiff"}
并过滤您的文件结果,使其仅包含可接受的图像结果(使用Split
方法,Contains
等...)
Dim paths As List(Of String) = New List(Of String)
For Each rawpath As String In rawpaths
Dim rawwords As String() = rawpath.Split(".") 'split the rawpath, the important is the last element, which is the file extension
If (validImageFormat.Contains(rawwords(rawwords.Length - 1))) Then 'the rawpath is a valid image path
paths.Add(rawpath) 'this is a valid image path
End If
Next
然后,在您的打印预览Form
中,您可能需要列出您的8 ListBox
以便于您稍后控制显示以及处理数字的情况图像不是8的乘法
Dim pbList As List(Of PictureBox) = New List(Of PictureBox)
Private Sub printPreviewForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pbList.Add(PictureBox1) 'list your picture box will help you later
pbList.Add(PictureBox2)
pbList.Add(PictureBox3)
pbList.Add(PictureBox4)
pbList.Add(PictureBox5)
pbList.Add(PictureBox6)
pbList.Add(PictureBox7)
pbList.Add(PictureBox8)
End Sub
然后,您需要确定要显示的图片数量(最小值为1,最多为8,基于PageNumber
)
If (pageNumber * 8 > paths.Count) Then 'exceeds the possible image display
Return 'this is not allowed, do something
End If
Dim minPathNo As Integer = pageNumber * 8 'get the min and max for later display
Dim maxPathNo As Integer = Math.Min(pageNumber * 8 + 7, paths.Count - 1)
最后,要显示,您可以使用Image.FromFile()
方法从文件夹中加载图像文件,并使用之前声明的minPathNo
和maxPathNo
来安全地显示图像< / p>
If (pageNumber * 8 > paths.Count) Then 'exceeds the possible image display
Return 'this is not allowed, do something
End If
Dim minPathNo As Integer = pageNumber * 8
Dim maxPathNo As Integer = Math.Min(pageNumber * 8 + 7, paths.Count - 1)
For i As Integer = minPathNo To minPathNo + 7
Dim pbIndex As Integer = i - minPathNo
If i <= maxPathNo Then
pbList(pbIndex).Image = Image.FromFile(paths(i)) 'display existing image
Else
pbList(pbIndex).Image = Nothing 'don't display non-existing image
End If
Next
编辑:
假设您的表单如下所示:
然后,这就是您的代码在一个表单中的样子。在您的情况下,您必须制作两种形式:
Imports System.IO
Public Class Form1
Dim folder As String = "C:\MyPics"
Dim validImageFormat As New List(Of String) From {"jpg", "bmp", "png", "jpeg", "gif", "tiff"}
Dim pbList As List(Of PictureBox) = New List(Of PictureBox)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawpaths As List(Of String) = Directory.GetFiles(folder).ToList() 'Assuming all the file is image
Dim paths As List(Of String) = New List(Of String)
For Each rawpath As String In rawpaths
Dim rawwords As String() = rawpath.Split(".") 'split the rawpath, the important is the last element, which is the file extension
If (validImageFormat.Contains(rawwords(rawwords.Length - 1))) Then 'the rawpath is a valid image path
paths.Add(rawpath) 'this is a valid image path
End If
Next
Dim pageNumber As Integer = NumericUpDown1.Value
If (pageNumber * 8 > paths.Count) Then 'exceeds the possible image display
Return 'this is not allowed, do something
End If
Dim minPathNo As Integer = pageNumber * 8
Dim maxPathNo As Integer = Math.Min(pageNumber * 8 + 7, paths.Count - 1)
For i As Integer = minPathNo To maxPathNo
Dim currentPictureBox As Integer = i - minPathNo
pbList(currentPictureBox).Image = Image.FromFile(paths(i))
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pbList.Add(PictureBox1)
pbList.Add(PictureBox2)
pbList.Add(PictureBox3)
pbList.Add(PictureBox4)
pbList.Add(PictureBox5)
pbList.Add(PictureBox6)
pbList.Add(PictureBox7)
pbList.Add(PictureBox8)
End Sub
End Class
你只需要将numericUpDown
(模拟你的页面`改为0,1,2等......)