-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiCalc-CL.vb
79 lines (66 loc) · 2.89 KB
/
DiCalc-CL.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.VisualBasic.FileIO
Imports System.Drawing.Imaging
Module Module1
Sub Main(args As String())
If args.Length = 0 Then
Console.WriteLine("Please provide a file path.")
Return
End If
Dim filePath As String = args(0)
If Not System.IO.File.Exists(filePath) Then
Console.WriteLine("File does not exist.")
Return
End If
Dim imageFormat As ImageFormat = GetImageFormat(filePath)
If imageFormat Is Nothing Then
Console.WriteLine("File is not an image.")
Return
End If
Dim image As Image = Image.FromFile(filePath)
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim Image_aspectRatio As Double = width / height
' Read aspect ratio from INI file
Dim iniFilePath As String = AppDomain.CurrentDomain.BaseDirectory & "DiCalc-CL.ini"
Dim aspectRatio As Double = 16 / 9
If System.IO.File.Exists(iniFilePath) Then
Using parser As New TextFieldParser(iniFilePath)
parser.TextFieldType = FieldType.Delimited
parser.SetDelimiters("=")
While Not parser.EndOfData
Dim fields() As String = parser.ReadFields()
If fields(0) = "AspectRatio" Then
Dim ratioParts() As String = fields(1).Split(":"c)
aspectRatio = Double.Parse(ratioParts(0)) / Double.Parse(ratioParts(1))
Exit While
End If
End While
End Using
End If
' Calculate missing dimension
If Image_aspectRatio < aspectRatio Then
Dim calculatedWidth As Integer = width
Dim calculatedHeight As Integer = CInt(width / aspectRatio)
' Copy the result to clipboard
Clipboard.SetText(calculatedWidth.ToString() & " " & calculatedHeight.ToString())
' Console.WriteLine(calculatedWidth.ToString() & " " & calculatedHeight.ToString())
Else
Dim calculatedWidth As Integer = CInt(height * aspectRatio)
Dim calculatedHeight As Integer = height
' Copy the result to clipboard
Clipboard.SetText(calculatedWidth.ToString() & " " & calculatedHeight.ToString())
' Console.WriteLine(calculatedWidth.ToString() & " " & calculatedHeight.ToString())
End If
End Sub
Private Function GetImageFormat(filePath As String) As ImageFormat
Try
Using image As Image = Image.FromFile(filePath)
Return image.RawFormat
End Using
Catch ex As OutOfMemoryException
Return Nothing
End Try
End Function
End Module