document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. Imports System.IO
  2. Imports System.Drawing
  3. Imports System.Windows.Forms
  4.  
  5. Module Module1
  6.  
  7.     Private items As New List(Of String)
  8.     Private ItemCount As Integer
  9.     Private processors As New List(Of Imager)
  10.     Private QualityOn As Boolean = False
  11.     Private BenchmarkOn As Boolean = False
  12.     Private Mode As String = String.Empty
  13.     Private CustomThreads As Integer = 0
  14.  
  15.     Sub Main()
  16.         If My.Application.CommandLineArgs.Count = 0 Then
  17.             Console.WriteLine(
  18.                 "Usage : Drag image files or folders" & vbCrLf & _
  19.                 "That contain images onto the executable." & vbCrLf & vbCrLf & _
  20.                 "To benchmark add """ & "-bench""" & " without quotes" & vbCrLf & _
  21.                 "to the end of this execuable file name" & vbCrLf & vbCrLf & _
  22.                 "To process in HighQuality add """ & "-q""" & " without quotes" & vbCrLf & _
  23.                 "to the end of this execuable file name" & vbCrLf & vbCrLf & _
  24.                 "To specify number of threads add """ & "-thread=""" & " followed by" & vbCrLf & _
  25.                 "the number of threads to use (no spaces, without quotes)" & vbCrLf & _
  26.                 "to the end of this execuable file name")
  27.             Console.ReadKey()
  28.             Exit Sub
  29.         End If
  30.         Dim appPath As String = Application.ExecutablePath
  31.         Dim appNameStartIndex As Integer = appPath.LastIndexOf("\\")
  32.         Dim appName As String = _
  33.             appPath.Substring(appNameStartIndex + 1, appPath.Length - appNameStartIndex - 1) _
  34.             .ToLower.Replace(".exe", String.Empty)
  35.         If appName.Contains("-bench") Then
  36.             BenchmarkOn = True
  37.             Mode &= " Benchmark"
  38.         Else
  39.             Mode &= " Normal"
  40.         End If
  41.         If appName.Contains("-q") Then
  42.             QualityOn = True
  43.             Mode &= " HighQuality"
  44.         Else
  45.             Mode &= " HighSpeed"
  46.         End If
  47.         If appName.Contains("-thread=") Then
  48.             Dim nameparts() As String = appName.Split("-")
  49.             For Each part As String In nameparts
  50.                 If part.StartsWith("thread=") Then
  51.                     CustomThreads = part.Split("=")(1)
  52.                     Exit For
  53.                 End If
  54.             Next
  55.             If CustomThreads = 0 Then Mode &= " DefaultThread" Else Mode &= " SetThread=" & CustomThreads
  56.         Else
  57.             Mode &= " DefaultThread"
  58.         End If
  59.         For Each arg As String In My.Application.CommandLineArgs
  60.             Dim attr As FileAttributes = File.GetAttributes(arg)
  61.             If (attr And FileAttributes.Directory) = FileAttributes.Directory _
  62.                 Then checkSubDirs(arg) Else CheckExt(arg)
  63.         Next
  64.         ItemCount = items.Count
  65.         If items.Count > 0 Then
  66.             Dim ItemsCount As Integer = items.Count
  67.             Dim PerThreadMinimal As Integer
  68.             If CustomThreads > 0 Then PerThreadMinimal = CustomThreads Else PerThreadMinimal = _
  69.                 If(ItemsCount >= Environment.ProcessorCount, Environment.ProcessorCount, ItemsCount)
  70.             For i = 1 To PerThreadMinimal
  71.                 Dim temp As New Imager(i, QualityOn, BenchmarkOn)
  72.                 For x = 1 To Math.Floor(ItemsCount / PerThreadMinimal)
  73.                     temp.Items.Add(items(0))
  74.                     items.RemoveAt(0)
  75.                 Next
  76.                 processors.Add(temp)
  77.             Next
  78.             For Each imageproc As Imager In processors
  79.                 If items.Count > 0 Then
  80.                     imageproc.Items.Add(items(0))
  81.                     items.RemoveAt(0)
  82.                 End If
  83.             Next
  84.             Console.WriteLine("Mode :" & Mode & vbCrLf & _
  85.                               "Images to process : " & ItemsCount & vbCrLf & _
  86.                               "Threads used for processing : " & processors.Count & vbCrLf & _
  87.                               "Processing please wait...")
  88.             For Each imgproc As Imager In processors : imgproc.Start() : Next
  89.         End If
  90.         If BenchmarkOn Then Console.ReadKey()
  91.     End Sub
  92.  
  93.     Private Sub checkSubDirs(input As String)
  94.         For Each file As String In My.Computer.FileSystem.GetFiles(input) : CheckExt(file) : Next
  95.         For Each dir As String In My.Computer.FileSystem.GetDirectories(input) : checkSubDirs(dir) : Next
  96.     End Sub
  97.  
  98.     Private Sub CheckExt(input As String)
  99.         Dim imageExt As Integer = input.LastIndexOf(".")
  100.         Dim Ext As String = input.Substring(imageExt, input.Length - imageExt).ToLower
  101.         Select Case Ext
  102.             Case ".jpg", ".jpeg", ".bmp", ".tiff", ".png", ".gif"
  103.                 items.Add(input)
  104.         End Select
  105.     End Sub
  106. End Module
  107.  
  108. Public Class Imager
  109.  
  110.     Public Items As New List(Of String)
  111.     Public ThreadNbr As Integer = 0
  112.     Private NbrProc As Integer = 0
  113.     Private ThreadTimer As Stopwatch
  114.     Private Quality_On As Boolean = False
  115.     Private Benchmark_On As Boolean = False
  116.  
  117.     Public Sub Start()
  118.         Dim WorkThread As New Threading.Thread(AddressOf Work)
  119.         WorkThread.Start()
  120.     End Sub
  121.  
  122.     Private Sub Work()
  123.         If Items.Count > 0 Then
  124.             Dim tempbitmap As New Bitmap(460, 215)
  125.             Dim tempgfx As Graphics
  126.             Dim pathParts() As String
  127.             Dim SavePath As String = String.Empty
  128.             Dim finalPath As String = String.Empty
  129.             Dim indexCount As Integer = 0
  130.             Dim skip As Boolean = False
  131.             tempgfx = Graphics.FromImage(tempbitmap)
  132.             If Quality_On Then
  133.                 tempgfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  134.                 tempgfx.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
  135.                 tempgfx.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
  136.                 tempgfx.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
  137.             Else
  138.                 tempgfx.InterpolationMode = Drawing2D.InterpolationMode.Low
  139.                 tempgfx.SmoothingMode = Drawing2D.SmoothingMode.None
  140.                 tempgfx.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
  141.                 tempgfx.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed
  142.             End If
  143.             If Benchmark_On Then ThreadTimer = Stopwatch.StartNew
  144.             For i = 0 To Items.Count - 1
  145.                 On Error Resume Next
  146.                 indexCount = 0
  147.                 SavePath = String.Empty
  148.                 pathParts = Items(i).Split("\\")
  149.                 For x = 0 To pathParts.Length - 2 : SavePath &= pathParts(x) & "\\" : Next
  150.                 Dim sourceImage = New Bitmap(Items(i))
  151.                 tempgfx.DrawImage(sourceImage, New Rectangle(0, 0, 460, 215))
  152.                 finalPath = SavePath & pathParts(pathParts.Length - 1).Split(".")(0) & " - Resized.png"
  153.                 Do While IO.File.Exists(finalPath)
  154.                     indexCount += 1
  155.                     finalPath = SavePath & pathParts(pathParts.Length - 1).Split(".")(0) & _
  156.                         " - Resized (" & indexCount.ToString & ").png"
  157.                 Loop
  158.                 tempbitmap.Save(finalPath, Imaging.ImageFormat.Png)
  159.                 sourceImage.Dispose()
  160.                 NbrProc += 1
  161.                 System.GC.Collect()
  162.             Next
  163.             ThreadTimer.Stop()
  164.             If Benchmark_On Then _
  165.                 Console.WriteLine("Thread " & ThreadNbr & " : " & NbrProc & " in " & _
  166.                     ThreadTimer.Elapsed.TotalSeconds & " seconds")
  167.         Else
  168.             Console.WriteLine("Thread " & ThreadNbr & " : N/A")
  169.         End If
  170.     End Sub
  171.  
  172.     Public Sub New(Thread_Number As Integer, QualityOn As Boolean, benchmarkOn As Boolean)
  173.         ThreadNbr = Thread_Number
  174.         Quality_On = QualityOn
  175.         Benchmark_On = benchmarkOn
  176.     End Sub
  177. End Class
');