Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Option Strict
- Imports System
- Imports System.Drawing
- Imports System.Drawing.Imaging
- Imports System.IO
- Imports System.Windows.Forms
- Imports System.Windows.Forms.VisualStyles
- Public Class MirrorForm
- Inherits Form
- Private filename As String
- Private WithEvents OpenButton As New Button
- Private WithEvents SaveButton As New Button
- Private WithEvents FlipCheckBox As New CheckBox
- Private WithEvents TrackBar1 as New Trackbar
- Private WithEvents ScaleCheckBox As New CheckBox
- Private WithEvents Panel1 as New Panel
- Private PictureBox1 as New PictureBox
- Private OriginalImage As Bitmap
- Private MirrorImage As Bitmap
- <STAThread()> _
- Public Shared Sub Main()
- Application.EnableVisualStyles()
- Application.Run(New MirrorForm())
- End Sub
- Public Sub New()
- Dim bpad As Integer = (TrackBar1.Height - OpenButton.Height) \ 2 ' integer division
- ' moved to top because control location calculations, -1h
- MinimumSize = New Size(480, 360)
- Size = New Size(640, 480)
- Text = "Mirror"
- With OpenButton
- .Anchor = AnchorStyles.Top Or AnchorStyles.Left
- .Location = New Point(bpad, bpad)
- .Text = "&Open"
- End With
- With FlipCheckBox
- .Anchor = AnchorStyles.Top Or AnchorStyles.Left
- ' on the right of the open button
- .Location = New Point(OpenButton.Right + bpad, bpad)
- .Text = "&Flip"
- .Width = OpenButton.Width
- .Enabled = False
- End With
- With SaveButton
- .Anchor = AnchorStyles.Top Or AnchorStyles.Right
- ' in the right rop corner of the form
- .Location = New Point(ClientSize.Width - .Width - bpad, bpad)
- .Text = "&Save"
- .Enabled = False
- End With
- With ScaleCheckBox
- .Anchor = AnchorStyles.Top Or AnchorStyles.Right
- ' on the left of the save button
- .Location = New Point(SaveButton.Left - SaveButton.Width, bpad)
- .Text = "S&cale"
- .Width = SaveButton.Width
- .Enabled = False
- '.Checked = True
- End With
- With TrackBar1
- .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
- .Location = New Point(FlipCheckBox.Right, 0)
- .TickStyle = TickStyle.Both
- ' inbetween the flip and scale checkboxes
- .Width = ScaleCheckBox.Left - FlipCheckBox.Right - bpad
- .SmallChange = 1
- .LargeChange = 10
- .Minimum = 1
- .Maximum = 100
- .TickFrequency = 100
- .Value = 50
- .Enabled = False
- End With
- ' inside the panel
- With PictureBox1
- .Anchor = AnchorStyles.Top Or AnchorStyles.Left
- .Location = New Point(0, 0)
- .SizeMode = PictureBoxSizeMode.AutoSize
- End With
- With Panel1
- .Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
- .Location = New Point(0, TrackBar1.Height)
- .AutoScroll = True
- .Width = ClientSize.Width
- .Height = ClientSize.Height - TrackBar1.Height
- .Controls.Add(PictureBox1)
- End With
- With Controls
- .Add(OpenButton)
- .Add(FlipCheckBox)
- .Add(TrackBar1)
- .Add(ScaleCheckBox)
- .Add(SaveButton)
- .Add(Panel1)
- End With
- End Sub
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' event handlers
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Private Sub OpenButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OpenButton.Click
- Using dlg as New OpenFileDialog
- dlg.Filter = "Image Files|*.bmp;*.gif;*.jpg;*.png"
- If dlg.ShowDialog() = DialogResult.OK Then
- ' fix bug with bad image murdering the existing image
- Try
- Dim tmp as New Bitmap(dlg.FileName)
- If OriginalImage IsNot Nothing Then OriginalImage.Dispose()
- OriginalImage = tmp
- tmp = Nothing
- Catch ex As Exception
- MessageBox.Show("Something went wrong opening that image.")
- Exit Sub
- End Try
- filename = Path.GetFileNameWithoutExtension(dlg.Filename)
- If FlipCheckBox.Checked Then
- OriginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX)
- OriginalImage = New Bitmap(OriginalImage)
- End If
- With TrackBar1
- .Maximum = OriginalImage.Width
- .Value = OriginalImage.Width \ 2
- .Enabled = True
- .Focus()
- End With
- SaveButton.Enabled = True
- FlipCheckBox.Enabled = True
- ScaleCheckBox.Enabled = True
- DoMirror()
- If ScaleCheckBox.Checked Then DoScale()
- End If
- End Using
- End Sub
- Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles TrackBar1.Scroll
- If OriginalImage IsNot Nothing Then DoMirror
- End Sub
- Private Sub FlipCheckBox_Click(ByVal sender As Object, ByVal e As EventArgs) Handles FlipCheckBox.Click
- If OriginalImage Is Nothing Then Exit Sub
- ' just keep flipping the image instead of reloading
- OriginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX)
- OriginalImage = New Bitmap(OriginalImage)
- TrackBar1.Focus()
- DoMirror
- End Sub
- Private Sub ScaleCheckBox_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ScaleCheckBox.Click
- If OriginalImage Is Nothing Then Exit Sub
- If ScaleCheckBox.Checked Then
- ' fix bug where if scrolled and then scale is enabled ...
- If Panel1.AutoScrollPosition.X < 0 OR Panel1.AutoScrollPosition.Y < 0 Then
- Panel1.AutoScrollPosition = New Point(0, 0)
- PictureBox1.Location = New Point(0, 0)
- End If
- Panel1.AutoScroll = False
- PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
- DoScale()
- Else
- Panel1.AutoScroll = True
- PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
- End If
- TrackBar1.Focus()
- End Sub
- Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
- If MirrorImage Is Nothing Then Exit Sub
- Dim fmt As String = "{0}_mirror{1}"
- If FlipCheckBox.Checked Then fmt &= "flip"
- Using dlg As New SaveFileDialog
- dlg.Filter = "Bitmap File|*.bmp" ' good enough for me
- dlg.FileName = String.Format(fmt, filename, TrackBar1.Value)
- If dlg.ShowDialog() = DialogResult.OK Then
- MirrorImage.Save(dlg.FileName, ImageFormat.Bmp)
- TrackBar1.Focus()
- End If
- End Using
- End Sub
- Private Sub Panel1_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles Panel1.Resize
- If ScaleCheckBox.Checked Then DoScale()
- End Sub
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- ' helpers
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Private Sub DoScale()
- If MirrorImage Is Nothing Then Exit Sub
- If PictureBox1.SizeMode <> PictureBoxSizeMode.Zoom Then Exit Sub
- Dim pw as Double = 1d
- Dim ph as Double = 1d
- If MirrorImage.Width > Panel1.Width Then
- pw = Panel1.Width / MirrorImage.Width
- End If
- If MirrorImage.Height > Panel1.Height Then
- ph = Panel1.Height / MirrorImage.Height
- End If
- Dim p as Double = Math.Min(pw, ph)
- Dim w as Integer = CType(Math.Ceiling(MirrorImage.Width * p), Integer)
- Dim h as Integer = CType(Math.Ceiling(MirrorImage.Height * p), Integer)
- PictureBox1.Size = New Size(w, h)
- End Sub
- Private Sub DoMirror()
- If OriginalImage Is Nothing Then Exit Sub
- If MirrorImage IsNot Nothing Then MirrorImage.Dispose()
- Dim rect as New Rectangle(0, 0, TrackBar1.Value, OriginalImage.Height)
- Dim frect as New Rectangle(TrackBar1.Value, 0, TrackBar1.Value, OriginalImage.Height)
- ' create the flipped
- Using flipped as New Bitmap(rect.Width, rect.Height)
- Using g As Graphics = Graphics.FromImage(flipped)
- g.DrawImage(OriginalImage, rect, rect, GraphicsUnit.Pixel)
- End Using
- flipped.RotateFlip(RotateFlipType.RotateNoneFlipX)
- ' create the mirror
- MirrorImage = New Bitmap(rect.Width * 2, rect.Height)
- Using g As Graphics = Graphics.FromImage(MirrorImage)
- g.DrawImage(OriginalImage, rect, rect, GraphicsUnit.Pixel) ' left
- g.DrawImage(flipped, frect, rect, GraphicsUnit.Pixel) ' right
- End Using
- End Using ' flipped
- PictureBox1.Image = DirectCast(MirrorImage, Image)
- If ScaleCheckBox.Checked Then DoScale()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement