Advertisement
Guest User

mirror.vb

a guest
Jul 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. Option Explicit
  2. Option Strict
  3.  
  4. Imports System
  5. Imports System.Drawing
  6. Imports System.Drawing.Imaging
  7. Imports System.IO
  8. Imports System.Windows.Forms
  9. Imports System.Windows.Forms.VisualStyles
  10.  
  11. Public Class MirrorForm
  12. Inherits Form
  13.  
  14. Private filename As String
  15.  
  16. Private WithEvents OpenButton As New Button
  17. Private WithEvents SaveButton As New Button
  18. Private WithEvents FlipCheckBox As New CheckBox
  19. Private WithEvents TrackBar1 as New Trackbar
  20. Private WithEvents ScaleCheckBox As New CheckBox
  21. Private WithEvents Panel1 as New Panel
  22. Private PictureBox1 as New PictureBox
  23.  
  24. Private OriginalImage As Bitmap
  25. Private MirrorImage As Bitmap
  26.  
  27. <STAThread()> _
  28. Public Shared Sub Main()
  29. Application.EnableVisualStyles()
  30. Application.Run(New MirrorForm())
  31. End Sub
  32.  
  33. Public Sub New()
  34. Dim bpad As Integer = (TrackBar1.Height - OpenButton.Height) \ 2 ' integer division
  35.  
  36. ' moved to top because control location calculations, -1h
  37. MinimumSize = New Size(480, 360)
  38. Size = New Size(640, 480)
  39. Text = "Mirror"
  40.  
  41. With OpenButton
  42. .Anchor = AnchorStyles.Top Or AnchorStyles.Left
  43. .Location = New Point(bpad, bpad)
  44. .Text = "&Open"
  45. End With
  46.  
  47. With FlipCheckBox
  48. .Anchor = AnchorStyles.Top Or AnchorStyles.Left
  49. ' on the right of the open button
  50. .Location = New Point(OpenButton.Right + bpad, bpad)
  51. .Text = "&Flip"
  52. .Width = OpenButton.Width
  53. .Enabled = False
  54. End With
  55.  
  56. With SaveButton
  57. .Anchor = AnchorStyles.Top Or AnchorStyles.Right
  58. ' in the right rop corner of the form
  59. .Location = New Point(ClientSize.Width - .Width - bpad, bpad)
  60. .Text = "&Save"
  61. .Enabled = False
  62. End With
  63.  
  64. With ScaleCheckBox
  65. .Anchor = AnchorStyles.Top Or AnchorStyles.Right
  66. ' on the left of the save button
  67. .Location = New Point(SaveButton.Left - SaveButton.Width, bpad)
  68. .Text = "S&cale"
  69. .Width = SaveButton.Width
  70. .Enabled = False
  71. '.Checked = True
  72. End With
  73.  
  74. With TrackBar1
  75. .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
  76. .Location = New Point(FlipCheckBox.Right, 0)
  77. .TickStyle = TickStyle.Both
  78. ' inbetween the flip and scale checkboxes
  79. .Width = ScaleCheckBox.Left - FlipCheckBox.Right - bpad
  80. .SmallChange = 1
  81. .LargeChange = 10
  82. .Minimum = 1
  83. .Maximum = 100
  84. .TickFrequency = 100
  85. .Value = 50
  86. .Enabled = False
  87. End With
  88.  
  89. ' inside the panel
  90. With PictureBox1
  91. .Anchor = AnchorStyles.Top Or AnchorStyles.Left
  92. .Location = New Point(0, 0)
  93. .SizeMode = PictureBoxSizeMode.AutoSize
  94. End With
  95.  
  96. With Panel1
  97. .Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
  98. .Location = New Point(0, TrackBar1.Height)
  99. .AutoScroll = True
  100. .Width = ClientSize.Width
  101. .Height = ClientSize.Height - TrackBar1.Height
  102. .Controls.Add(PictureBox1)
  103. End With
  104.  
  105. With Controls
  106. .Add(OpenButton)
  107. .Add(FlipCheckBox)
  108. .Add(TrackBar1)
  109. .Add(ScaleCheckBox)
  110. .Add(SaveButton)
  111. .Add(Panel1)
  112. End With
  113.  
  114. End Sub
  115.  
  116. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  117. ' event handlers
  118. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  119.  
  120. Private Sub OpenButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OpenButton.Click
  121. Using dlg as New OpenFileDialog
  122. dlg.Filter = "Image Files|*.bmp;*.gif;*.jpg;*.png"
  123. If dlg.ShowDialog() = DialogResult.OK Then
  124. ' fix bug with bad image murdering the existing image
  125. Try
  126. Dim tmp as New Bitmap(dlg.FileName)
  127. If OriginalImage IsNot Nothing Then OriginalImage.Dispose()
  128. OriginalImage = tmp
  129. tmp = Nothing
  130. Catch ex As Exception
  131. MessageBox.Show("Something went wrong opening that image.")
  132. Exit Sub
  133. End Try
  134. filename = Path.GetFileNameWithoutExtension(dlg.Filename)
  135.  
  136. If FlipCheckBox.Checked Then
  137. OriginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX)
  138. OriginalImage = New Bitmap(OriginalImage)
  139. End If
  140.  
  141. With TrackBar1
  142. .Maximum = OriginalImage.Width
  143. .Value = OriginalImage.Width \ 2
  144. .Enabled = True
  145. .Focus()
  146. End With
  147. SaveButton.Enabled = True
  148. FlipCheckBox.Enabled = True
  149. ScaleCheckBox.Enabled = True
  150.  
  151. DoMirror()
  152. If ScaleCheckBox.Checked Then DoScale()
  153. End If
  154. End Using
  155. End Sub
  156.  
  157. Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles TrackBar1.Scroll
  158. If OriginalImage IsNot Nothing Then DoMirror
  159. End Sub
  160.  
  161. Private Sub FlipCheckBox_Click(ByVal sender As Object, ByVal e As EventArgs) Handles FlipCheckBox.Click
  162. If OriginalImage Is Nothing Then Exit Sub
  163.  
  164. ' just keep flipping the image instead of reloading
  165. OriginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX)
  166. OriginalImage = New Bitmap(OriginalImage)
  167.  
  168. TrackBar1.Focus()
  169. DoMirror
  170. End Sub
  171.  
  172. Private Sub ScaleCheckBox_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ScaleCheckBox.Click
  173. If OriginalImage Is Nothing Then Exit Sub
  174.  
  175. If ScaleCheckBox.Checked Then
  176. ' fix bug where if scrolled and then scale is enabled ...
  177. If Panel1.AutoScrollPosition.X < 0 OR Panel1.AutoScrollPosition.Y < 0 Then
  178. Panel1.AutoScrollPosition = New Point(0, 0)
  179. PictureBox1.Location = New Point(0, 0)
  180. End If
  181.  
  182. Panel1.AutoScroll = False
  183. PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
  184. DoScale()
  185. Else
  186. Panel1.AutoScroll = True
  187. PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
  188. End If
  189.  
  190. TrackBar1.Focus()
  191. End Sub
  192.  
  193. Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
  194. If MirrorImage Is Nothing Then Exit Sub
  195.  
  196. Dim fmt As String = "{0}_mirror{1}"
  197. If FlipCheckBox.Checked Then fmt &= "flip"
  198.  
  199. Using dlg As New SaveFileDialog
  200. dlg.Filter = "Bitmap File|*.bmp" ' good enough for me
  201. dlg.FileName = String.Format(fmt, filename, TrackBar1.Value)
  202. If dlg.ShowDialog() = DialogResult.OK Then
  203. MirrorImage.Save(dlg.FileName, ImageFormat.Bmp)
  204. TrackBar1.Focus()
  205. End If
  206. End Using
  207. End Sub
  208.  
  209. Private Sub Panel1_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles Panel1.Resize
  210. If ScaleCheckBox.Checked Then DoScale()
  211. End Sub
  212.  
  213. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  214. ' helpers
  215. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  216.  
  217. Private Sub DoScale()
  218. If MirrorImage Is Nothing Then Exit Sub
  219. If PictureBox1.SizeMode <> PictureBoxSizeMode.Zoom Then Exit Sub
  220.  
  221. Dim pw as Double = 1d
  222. Dim ph as Double = 1d
  223. If MirrorImage.Width > Panel1.Width Then
  224. pw = Panel1.Width / MirrorImage.Width
  225. End If
  226. If MirrorImage.Height > Panel1.Height Then
  227. ph = Panel1.Height / MirrorImage.Height
  228. End If
  229. Dim p as Double = Math.Min(pw, ph)
  230.  
  231. Dim w as Integer = CType(Math.Ceiling(MirrorImage.Width * p), Integer)
  232. Dim h as Integer = CType(Math.Ceiling(MirrorImage.Height * p), Integer)
  233. PictureBox1.Size = New Size(w, h)
  234. End Sub
  235.  
  236. Private Sub DoMirror()
  237. If OriginalImage Is Nothing Then Exit Sub
  238. If MirrorImage IsNot Nothing Then MirrorImage.Dispose()
  239.  
  240. Dim rect as New Rectangle(0, 0, TrackBar1.Value, OriginalImage.Height)
  241. Dim frect as New Rectangle(TrackBar1.Value, 0, TrackBar1.Value, OriginalImage.Height)
  242.  
  243. ' create the flipped
  244. Using flipped as New Bitmap(rect.Width, rect.Height)
  245. Using g As Graphics = Graphics.FromImage(flipped)
  246. g.DrawImage(OriginalImage, rect, rect, GraphicsUnit.Pixel)
  247. End Using
  248. flipped.RotateFlip(RotateFlipType.RotateNoneFlipX)
  249.  
  250. ' create the mirror
  251. MirrorImage = New Bitmap(rect.Width * 2, rect.Height)
  252. Using g As Graphics = Graphics.FromImage(MirrorImage)
  253. g.DrawImage(OriginalImage, rect, rect, GraphicsUnit.Pixel) ' left
  254. g.DrawImage(flipped, frect, rect, GraphicsUnit.Pixel) ' right
  255. End Using
  256.  
  257. End Using ' flipped
  258.  
  259. PictureBox1.Image = DirectCast(MirrorImage, Image)
  260. If ScaleCheckBox.Checked Then DoScale()
  261. End Sub
  262.  
  263. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement