从 vb.net 中的文件夹打印图像,每页 8 张图像
print images from a folder in vb .net with 8 images per page
i select 使用 vb 的文件夹。net.my 代码如下--
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 张图像)
VB.Net 版本:
既然你想创建打印预览,我建议你为打印预览创建一个新的Form
并跟踪用户输入的PageNumber
Dim pageNumber As Integer = 0 'start from 0, but change this according to the user input accordingly
您可以使用 System.IO
中的 Directory.GetFiles
从给定文件夹中获取文件列表
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
来显示图片安全
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 等...)
i select 使用 vb 的文件夹。net.my 代码如下--
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 张图像)
VB.Net 版本:
既然你想创建打印预览,我建议你为打印预览创建一个新的
Form
并跟踪用户输入的PageNumber
Dim pageNumber As Integer = 0 'start from 0, but change this according to the user input accordingly
您可以使用
中的System.IO
Directory.GetFiles
从给定文件夹中获取文件列表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
来显示图片安全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 等...)