【问题标题】:I need to increase the CPU usage of my program in VB.net我需要在 VB.net 中增加我的程序的 CPU 使用率
【发布时间】:2023-01-12 06:22:40
【问题描述】:

我有一个程序可以拍摄图像并将其更改为 1 位黑白。它使用 Lockbyte 软件来提高速度。但是,在运行它时,处理一个图像需要一分钟多的时间。查看 CPU 使用率时,它在运行后最多只有 5%。有没有办法让计算机使用更多的 CPU 时间?指示灯显示电脑电量低于50%,低至25%。

我只是有一个 DUH 想法,我忘了将调整大小功能添加到我的程序中。它应该有所帮助,但我知道我需要加快转换速度。该程序将用于每批处理 100 到 300 张图像。

我见过的大多数其他程序都可以在几秒钟内完成每张图像的转换。我也想做这样的事情。

这是程序。主要由样品拼凑而成。我只理解一半,但可以阅读。对贡献者表示抱歉,我无法给予他们荣誉。我没有跟踪他们。

Public Class Form1

    Public Shared Function ConvertTo1Bit(ByVal input As Bitmap) As Bitmap
        Dim masks = New Byte() {&H80, &H40, &H20, &H10, &H8, &H4, &H2, &H1}
        Dim output = New Bitmap(input.Width, input.Height, PixelFormat.Format1bppIndexed)
        Dim data = New SByte(input.Width - 1, input.Height - 1) {}
        Dim inputData = input.LockBits(New Rectangle(0, 0, input.Width, input.Height), ImageLockMode.[ReadOnly], PixelFormat.Format24bppRgb)

        Try
            Dim scanLine = inputData.Scan0
            Dim line = New Byte(inputData.Stride - 1) {}
            Dim y = 0

            While y < inputData.Height
                Marshal.Copy(scanLine, line, 0, line.Length)

                For x = 0 To input.Width - 1
                    data(x, y) = CSByte((64 * (GetGreyLevel(line(x * 3 + 2), line(x * 3 + 1), line(x * 3 + 0)) - 0.5)))
                Next

                y += 1
                scanLine += inputData.Stride
            End While

        Finally
            input.UnlockBits(inputData)
        End Try

        Dim outputData = output.LockBits(New Rectangle(0, 0, output.Width, output.Height), ImageLockMode.[WriteOnly], PixelFormat.Format1bppIndexed)

        Try
            Dim scanLine = outputData.Scan0
            Dim y = 0

            While y < outputData.Height
                Dim line = New Byte(outputData.Stride - 1) {}

                For x = 0 To input.Width - 1
                    Dim j = data(x, y) > 0
                    Try
                        If j Then
                            line(x / 8) = line(x / 8) Or masks(x Mod 8)
                        End If
                    Catch ex As Exception

                    End Try


                    Dim [error] = CSByte((data(x, y) - (If(j, 32, -32))))
                    If x < input.Width - 1 Then data(x + 1, y) += CSByte((7 * [error] / 16))

                    If y < input.Height - 1 Then
                        If x > 0 Then data(x - 1, y + 1) += CSByte((3 * [error] / 16))
                        data(x, y + 1) += CSByte((5 * [error] / 16))
                        If x < input.Width - 1 Then data(x + 1, y + 1) += CSByte((1 * [error] / 16))
                    End If
                Next

                Marshal.Copy(line, 0, scanLine, outputData.Stride)
                y += 1
                scanLine += outputData.Stride
            End While

        Finally
            output.UnlockBits(outputData)
        End Try

        Return output
    End Function

    Public Shared Function GetGreyLevel(ByVal r As Byte, ByVal g As Byte, ByVal b As Byte) As Double
        Return (r * 0.299 + g * 0.587 + b * 0.114) / 255
    End Function

    Private Sub btBrowesIn_Click(sender As Object, e As EventArgs) Handles btBrowesIn.Click
        FolderBrowserDialog1.ShowDialog()
        tbInPic.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub btBrowesOut_Click(sender As Object, e As EventArgs) Handles btBrowesOut.Click
        FolderBrowserDialog2.ShowDialog()
        tbInPic.Text = FolderBrowserDialog2.SelectedPath
    End Sub

    Private Sub btGo_Click(sender As Object, e As EventArgs) Handles btGo.Click
        Dim Infiles As Array
        Dim opf As New OpenFileDialog

        opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"
        If opf.ShowDialog = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(opf.FileName)
            Dim MyBitmap As New Bitmap(PictureBox1.Image)
            PictureBox2.Image = ConvertTo1Bit(MyBitmap)
        End If
    End Sub

End Class

【问题讨论】:

  • 你试过在这里搜索这个吗?
  • 是的,我看到的一切都是为了监控 CPU 使用率或降低它。
  • 也许用处理这些图像的方法更新您的问题。考虑到 CPU 使用率,我同意这听起来很慢。你也可以看看多线程,你可以在它自己的线程上处理每个图像
  • 没有看到您的代码,我们怎么可能希望让它更快?...
  • 赫西,这看起来很有趣。谷歌搜索以了解如何将多线程应用于程序

标签: vb.net time cpu


【解决方案1】:

该程序将用于每批处理 100 到 300 张图像。

您可以异步处理图像。 .Net 提供了几种方法来做到这一点:Async/Await、raw Tasks、ThreadPool、raw Threads、BackgroundWorker,可能还有更多。此处哪个最合适取决于应用程序的上下文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多