Public Class Form1
Public Structure ARGB
Dim Alpha, Red, Green, Blue As Int16
Sub New(ByVal Col As Color)
Red = Col.R
Green = Col.G
Blue = Col.B
Alpha = Col.A
End Sub
Sub ToGray()
Dim Mid As Int16
Mid = (Red + Green + Blue) / 3
Red = Mid
Green = Mid
Blue = Mid
End Sub
Sub ToRed()
Dim Mid As Int16
Mid = (Red + Green + Blue) / 3
Red = 255
Green = Mid
Blue = Mid
End Sub
Sub ToBlue()
Dim Mid As Int16
Mid = (Red + Green + Blue) / 3
Blue = 255
Green = Mid
Red = Mid
End Sub
Sub ToGreen()
Dim Mid As Int16
Mid = (Red + Green + Blue) / 3
Green = 255
Red = Mid
Blue = Mid
End Sub
Sub ToNegative()
Red = 255 - Red
Green = 255 - Green
Blue = 255 - Blue
End Sub
Sub Brighter(ByVal Factor As Byte)
Red += Factor
Green += Factor
Blue += Factor
If Red > 255 Then
Red = 255
End If
If Green > 255 Then
Green = 255
End If
If Blue > 255 Then
Blue = 255
End If
End Sub
Sub Darker(ByVal Factor As Byte)
Red -= Factor
Green -= Factor
Blue -= Factor
If Red < 0 Then
Red = 0
End If
If Green < 0 Then
Green = 0
End If
If Blue < 0 Then
Blue = 0
End If
End Sub
Function GetInt32() As Int32
Dim C As Color
C = Color.FromArgb(Alpha, Red, Green, Blue)
Return C.ToArgb
End Function
Function GetColor() As Color
Dim C As Color
C = Color.FromArgb(Alpha, Red, Green, Blue)
Return C
End Function
End Structure
Public SaveName As String = Nothing
Public MouseDownStartPoint As Point
Public SelectedRegion As Rectangle = Nothing
Public Sub GetRegion(ByRef X, ByRef Y, ByRef W, ByRef H)
If SelectedRegion <> Nothing Then
With SelectedRegion
X = .X
Y = .Y
W = .Width
H = .Height
End With
Else
X = 0
Y = 0
W = imgP.Image.Width - 1
H = imgP.Image.Height - 1
End If
End Sub
Public Sub Colorize(ByVal Mode As String)
Dim X, Y, W, H As Single
Dim K As ARGB
Dim B As Drawing.Bitmap = imgP.Image.Clone
GetRegion(X, Y, W, H)
lblStatus.Text = "Colorizzazione in " + Mode + " in corso..."
Application.DoEvents()
For IW As Single = X To X + W
For IH As Single = Y To Y + H
K = New ARGB(B.GetPixel(IW, IH))
If Mode = "scala di grigi" Then
K.ToGray()
ElseIf Mode = "rosso" Then
K.ToRed()
ElseIf Mode = "verde" Then
K.ToGreen()
ElseIf Mode = "blu" Then
K.ToBlue()
ElseIf Mode = "negativo" Then
K.ToNegative()
End If
B.SetPixel(IW, IH, K.GetColor)
Next
Next
imgP.Image = B.Clone
lblStatus.Text = "Completato"
End Sub
Public Sub Brightness(ByVal Factor As Int16)
Dim X, Y, W, H As Single
Dim K As ARGB
Dim B As Drawing.Bitmap = imgP.Image.Clone
GetRegion(X, Y, W, H)
lblStatus.Text = "Aumento luminosità di " & Factor & " in corso..."
Application.DoEvents()
For IW As Single = X To X + W
For IH As Single = Y To Y + H
K = New ARGB(B.GetPixel(IW, IH))
If Factor > 0 Then
K.Brighter(Factor)
Else
K.Darker(-Factor)
End If
B.SetPixel(IW, IH, K.GetColor)
Next
Next
imgP.Image = B.Clone
lblStatus.Text = "Completato"
End Sub
Public Sub Blend(ByVal Mode As String, ByVal Factor As Int16)
Dim B As Bitmap = imgP.Image.Clone
Dim X, Y, W, H As Single
Dim Incr, Lum As Single
Dim K As ARGB
GetRegion(X, Y, W, H)
Incr = Factor / (W + 1)
Lum = 0
lblStatus.Text = "Sfumatura " + Mode + " in corso..."
Application.DoEvents()
If Mode = "da sinistra a destra" Then
For IW As Single = X To X + W
For IH As Single = Y To Y + H
K = New ARGB(B.GetPixel(IW, IH))
If Lum > 0 Then
K.Brighter(Lum)
Else
K.Darker(-Lum)
End If
B.SetPixel(IW, IH, K.GetColor)
Next
Lum += Incr
Next
ElseIf Mode = "da destra a sinistra" Then
For IW As Single = X + W To X Step -1
For IH As Single = Y To Y + H
K = New ARGB(B.GetPixel(IW, IH))
If Lum > 0 Then
K.Brighter(Lum)
Else
K.Darker(-Lum)
End If
B.SetPixel(IW, IH, K.GetColor)
Next
Lum += Incr
Next
ElseIf Mode = "dall'alto in basso" Then
For IH As Single = Y To Y + H
For IW As Single = X To X + W
K = New ARGB(B.GetPixel(IW, IH))
If Lum > 0 Then
K.Brighter(Lum)
Else
K.Darker(-Lum)
End If
B.SetPixel(IW, IH, K.GetColor)
Next
Lum += Incr
Next
ElseIf Mode = "dal basso all'alto" Then
For IH As Single = Y + H To Y Step -1
For IW As Single = X To X + W
K = New ARGB(B.GetPixel(IW, IH))
If Lum > 0 Then
K.Brighter(Lum)
Else
K.Darker(-Lum)
End If
B.SetPixel(IW, IH, K.GetColor)
Next
Lum += Incr
Next
End If
lblStatus.Text = "Completato"
imgP.Image = B.Clone
End Sub
Private Sub strFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFileOpen.Click
Dim O As New OpenFileDialog
O.Filter = "File immagine|*.jpg;*.jpeg;*.bmp;*.tif;*.tiff;*.png;*.wmf;*.gif;*.emf;*.exif"
If O.ShowDialog = Windows.Forms.DialogResult.OK Then
imgP.Image = Image.FromFile(O.FileName)
Me.Width += imgP.Image.Width - imgP.Width
Me.Height += imgP.Image.Height - imgP.Height
SaveName = O.FileName
SelectedRegion = Nothing
strFileSave.Enabled = True
strFileSaveAs.Enabled = True
strPrint.Enabled = True
strColorize.Enabled = True
strPortrait.Enabled = True
strBlend.Enabled = True
End If
End Sub
Private Sub strFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFileSave.Click
Try
If SaveName <> Nothing Then
imgP.Image.Save(SaveName)
lblStatus.Text = "Immagine salvata"
Else
Dim S As New SaveFileDialog
S.Filter = "File JPEG|*.jpg;*.jpeg|File BitMap|*.bmp|File TIF|*.tif;*.tiff|File PNG|*.png|File WMF|*.wmf|File GIF|*.gif|File EMF|*.emf|File EXIF|*.exif"
If S.ShowDialog = Windows.Forms.DialogResult.OK Then
imgP.Image.Save(S.FileName)
lblStatus.Text = "Immagine salvata"
SaveName = S.FileName
End If
End If
Catch Ex As Exception
MsgBox("Impossibile salvare l'immagine a causa di un errore generico.", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub strFileSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFileSaveAs.Click
Dim S As New SaveFileDialog
S.Filter = "File JPEG|*.jpg;*.jpeg|File BitMap|*.bmp|File TIF|*.tif;*.tiff|File PNG|*.png|File WMF|*.wmf|File GIF|*.gif|File EMF|*.emf|File EXIF|*.exif"
If S.ShowDialog = Windows.Forms.DialogResult.OK Then
imgP.Image.Save(S.FileName)
lblStatus.Text = "Immagine salvata"
SaveName = S.FileName
End If
End Sub
Private Sub strExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strExit.Click
Me.Close()
End Sub
Private Sub imgP_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles imgP.MouseDown
MouseDownStartPoint = imgP.PointToClient(MousePosition)
End Sub
Private Sub imgP_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles imgP.MouseMove
If MouseDownStartPoint <> Nothing Then
Dim P As Point = imgP.PointToClient(MousePosition)
Dim T As Point = MouseDownStartPoint
lblStatus.Text = "Seleziona regione da (" & T.X & "; " & T.Y & ") a (" & P.X & "; " & P.Y & ")"
End If
End Sub
Private Sub imgP_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles imgP.MouseUp
If MouseDownStartPoint <> Nothing Then
Dim P As Point = imgP.PointToClient(MousePosition)
Dim T As Point = MouseDownStartPoint
If P.X > T.X Then
If P.Y > T.Y Then
SelectedRegion = New Rectangle(T.X, T.Y, P.X - T.X, P.Y - T.Y)
Else
SelectedRegion = New Rectangle(T.X, P.Y, P.X - T.X, T.Y - P.Y)
End If
Else
If P.Y > T.Y Then
SelectedRegion = New Rectangle(P.X, T.Y, T.X - P.X, P.Y - T.Y)
Else
SelectedRegion = New Rectangle(P.X, P.Y, T.X - P.X, T.Y - P.Y)
End If
End If
MouseDownStartPoint = Nothing
End If
End Sub
Private Sub strGrey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strGrey.Click
Colorize("scala di grigi")
End Sub
Private Sub strRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRed.Click
Colorize("rosso")
End Sub
Private Sub strGreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strGreen.Click
Colorize("verde")
End Sub
Private Sub strBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strBlue.Click
Colorize("blu")
End Sub
Private Sub strNegative_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strNegative.Click
Colorize("negativo")
End Sub
Private Sub strBrighter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strBrighter.Click
Try
Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra 0 e 255.")
Brightness(CByte(F))
Catch ex As Exception
MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub strDarker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDarker.Click
Try
Dim F As String = InputBox("Inserire il fattore di diminuzione luminosità. Deve essere compreso tra 0 e 255.")
Brightness(-CByte(F))
Catch ex As Exception
MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub imgP_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgP.DoubleClick
SelectedRegion = New Rectangle(0, 0, imgP.Image.Width, imgP.Image.Height)
lblStatus.Text = "Selezione estesa a tutta l'immagine"
End Sub
Private Sub strLeftToRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strLeftToRight.Click
Try
Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
Blend("da sinistra a destra", CType(F, Int16))
Catch ex As Exception
MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub strRightToLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRightToLeft.Click
Try
Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
Blend("da destra a sinistra", CType(F, Int16))
Catch ex As Exception
MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub strUpToDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strUpToDown.Click
Try
Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
Blend("dall'alto in basso", CType(F, Int16))
Catch ex As Exception
MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub strDownToUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDownToUp.Click
Try
Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
Blend("dal basso all'alto", CType(F, Int16))
Catch ex As Exception
MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub strAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAbout.Click
MsgBox("Nome prodotto: Imaging" + vbCrLf + "Società: Piero Tofy's Community" + vbCrLf + "Sviluppatore: Totem" + vbCrLf + "E-mail: nicolo1990@yahoo.it" + vbCrLf + "Licenza: parte del contenuto di questo programma è distribuito sotto la licenza LGPL senza scopo di lucro, pertanto lo è anche il programma stesso.", MsgBoxStyle.Information)
End Sub
Private Sub strPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strPrint.Click
Dim S As String
If SaveName <> Nothing Then
S = SaveName
Else
imgP.Image.Save(Application.StartupPath + "\Temp.bmp")
S = Application.StartupPath + "\Temp.bmp"
End If
Dim P As New Process
P.StartInfo.FileName = SaveName
P.StartInfo.Verb = "Print"
P.Start()
End Sub
End Class