vb.Net在执行期间使用进度更新编写更好的代码

时间:2015-06-22 06:47:17

标签: vb.net visual-studio-2013

你好堆栈溢出天才!我从这个网站上寻求答案中学到了很多,并且非常感谢它的存在。我是一名新手(充其量),在编程方面完全自学。谷歌教授的可能更准确。从来没有,我总是寻求变得更好,每天编码。这是我最新的询问:

我正在编写一个vb.net Windows Forms应用程序,它在相应的目录中获取图像并为它们创建一些缩略图。在大多数情况下,我已经使这个代码工作,但有一些我想做的更好。

1

我希望应用程序在图像进展时提供实时进度条更新。目前我正在使用For循环,所以我不确定它是如何工作的。我并不认为我真的理解程序如何提供更新,而不是像For Loops那样立刻提供所有信息。

2

性能。我该怎么做才能让它更高效,更快速地运行?

3

我正在尝试创建保留纵横比的缩略图,并将新的(较小的图像)插入到重新调整大小的画布中。我相信我可以解决这个问题,但只是想将其添加为流程功能,以防它与实时进度更新相关。

这是我的代码,因为它显示在btnRun点击处理程序中:

Imports System
Imports System.IO
Imports System.Collections

Public Class fmMain

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRun.Click

    Dim x As Integer
    Dim s As String
    Dim path As String = tbPath.Text
    Dim pathL As String
    Dim pathF As String = Application.StartupPath() & "\done"

    For i As Integer = 0 To Convert.ToInt32(tbRange.Text)

        path = tbPath.Text
        pathF = Application.StartupPath() & "\done"

        x = i
        s = x.ToString

        Dim a(s.Length) As String

        For j As Integer = 0 To s.Length - 1
            a(j) = s.Substring(j, 1)
            path += "\" & a(j)
        Next

        pathL = path
        path += "\" & s & ".jpg"

        If File.Exists(path) Then

            pbMain.Image = Image.FromFile(path)

            Dim bmp As Bitmap = Image.FromFile(path)

            ' bmpt is the new thumb
            Dim bmpHome As New Bitmap(124, 124)
            Dim bmpLarge As New Bitmap(256, 256)
            Dim bmpMedium As New Bitmap(58, 58)
            Dim bmpSmall As New Bitmap(45, 45)
            Dim bmpThick As New Bitmap(600, 600)

            ' DRAW HOME
            Using g As Graphics = Graphics.FromImage(bmpHome)

                ' bmp == the original bitmap 
                g.DrawImage(bmp, 0, 0, bmpHome.Width + 1, bmpHome.Height + 1)

            End Using

            ' DRAW LARGE
            Using L As Graphics = Graphics.FromImage(bmpLarge)

                ' bmp == the original bitmap 
                L.DrawImage(bmp, 0, 0, bmpLarge.Width + 1, bmpLarge.Height + 1)

            End Using

            ' DRAW MEDIUM
            Using M As Graphics = Graphics.FromImage(bmpMedium)

                ' bmp == the original bitmap 
                M.DrawImage(bmp, 0, 0, bmpMedium.Width + 1, bmpMedium.Height + 1)

            End Using

            ' DRAW SMALL
            Using SM As Graphics = Graphics.FromImage(bmpSmall)

                ' bmp == the original bitmap 
                SM.DrawImage(bmp, 0, 0, bmpSmall.Width + 1, bmpSmall.Height + 1)

            End Using

            ' DRAW THICK
            Using T As Graphics = Graphics.FromImage(bmpThick)

                ' bmp == the original bitmap 
                T.DrawImage(bmp, 0, 0, bmpThick.Width + 1, bmpThick.Height + 1)

            End Using

            For p As Integer = 0 To s.Length - 1
                a(p) = s.Substring(p, 1)
                pathF += "\" & a(p)

                If Not File.Exists(pathF) Then
                    Directory.CreateDirectory(pathF)
                End If

            Next

            bmp.Save(pathF & "\" & s & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            bmpHome.Save(pathF & "\" & s & "-home_default.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            bmpLarge.Save(pathF & "\" & s & "-large_default.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            bmpMedium.Save(pathF & "\" & s & "-medium_default.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            bmpSmall.Save(pathF & "\" & s & "-small_default.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            bmpThick.Save(pathF & "\" & s & "-thickbox_default.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

            bmp.Dispose()
            bmpHome.Dispose()
            bmpLarge.Dispose()
            bmpSmall.Dispose()
            bmpThick.Dispose()

        End If

    Next

    MessageBox.Show("All Done!")
    ProgressBar1.Value = 100

End Sub
End Class

我在这里学习,所以任何建议,提示或链接都将非常感激和乐于助人。一如既往地感谢您查看我的问题和代码。我希望有一天能够有足够的经验来回答别人的问题!

1 个答案:

答案 0 :(得分:1)

您可能需要拆分原始帖子(OP)以获得所有答案。

1)是关于如何使用进度条:将.Maximum设置为要处理的文件数,在FOR..LOOP中增加.Value。在某些时候重置.Value - 可能在完成时或至少在下一次按钮点击期间。

2)您可能希望寻找其他标签以增加良好回复的机会

3)似乎多余