【问题标题】:print images from a folder in vb .net with 8 images per page从 vb .net 中的文件夹中打印图像,每页 8 张图像
【发布时间】:2016-04-06 11:37:29
【问题描述】:

我使用 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 张图片)

【问题讨论】:

  • 您有page 代码吗?您需要有 8 个图片框,当用户更改 page 时,只需将它们替换为新图像即可。一旦你有了你的页面,事情就会变得容易得多。
  • 对不起,我没有页面代码。
  • 你有表格吗? form 是您的page(除非您在form 中拥有像Panel 这样的较小控件)只需将8 个PictureBoxes 放入您的Form 和两个按钮Prev PageNext Page。然后通过int index 追踪您所在的page。基于index,可以根据用户输入的内容正确加载要放入8个PictureBoxes的图片(分别为:Next Pageclick或Prev Pageclick)
  • 是的,我确实有一个表格。但我的要求是我将选择一个包含一些图像的文件夹。单击某个按钮后,它将生成每页 8 张图片的打印预览(每行 4 张图片,合法纸张大小)。我是一个自学的新手。谢谢
  • 那么您可能首先需要创建另一个表单(除了主表单),您在用户加载文件夹时调用该表单。在该表单中,您使用我提到的所有按钮创建 8 个图片框,以创建打印预览。

标签: vb.net datagridview picturebox


【解决方案1】:

VB.Net 版本:

  1. 由于您要创建打印预览,我建议您为打印预览创建一个新的Form,并跟踪用户输入的PageNumber

    Dim pageNumber As Integer = 0 'start from 0, but change this according to the user input accordingly
    
  2. 您可以通过在System.IO 中使用Directory.GetFiles 来获取给定文件夹中的文件列表

    Dim rawpaths As List(Of String) = Directory.GetFiles(folder).ToList() 'This gets all files, not only images
    
  3. 您可能需要根据this 提供可接受的图片扩展名列表。

    Dim validImageFormat As New List(Of String) From {"jpg", "bmp", "png", "jpeg", "gif", "tiff"}
    
  4. 并过滤您的文件结果,使其仅包含这样的可接受的图像结果(使用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
    
  5. 然后,在您的打印预览Form 中,您可能需要列出您的 8 个ListBoxes,以方便您稍后控制显示以及处理图像数量不是 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
    
  6. 然后,您需要确定要显示多少张图片(最小为 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)
    
  7. 最后,要显示,您可以使用Image.FromFile() 方法从您的文件夹中加载图像文件,并使用之前声明的minPathNomaxPathNo 来安全地显示图像

    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等...)

【讨论】:

  • 在 form1 中,我浏览了文件夹并选择了 form2,然后按照您所说的使用设计工具加载了 form2,然后我添加了以下代码:
  • Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 MyBase.Load List listImages = new List() listImages.Add(imageFromFolder) pictureBox0 .Image = listImages[pageNumber * 8] pictureBox1.Image = listImages[pageNumber * 8 + 1] pictureBox2.Image = listImages[pageNumber * 8 + 2] pictureBox3.Image = listImages[pageNumber * 8 + 3] pictureBox4.Image = listImages [pageNumber * 8 + 4] ---------- 我有错误。对不起我的无知我初学者尝试心灵和灵魂结束子
  • @Jannat 我已经更新了 VB.Net 版本的代码示例。使用它而不是 C# 版本
  • 感谢您的帮助。同时我做了一些编码。你会看我的代码吗?[链接]pastebin.com/F30fXT1c
  • 是的,我接受你的想法,我只是按照我能做到的方式做到了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
相关资源
最近更新 更多