Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Imaging - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Public Class Form1
  2.     Public Structure ARGB
  3.         Dim Alpha, Red, Green, Blue As Int16
  4.         Sub New(ByVal Col As Color)
  5.             Red = Col.R
  6.             Green = Col.G
  7.             Blue = Col.B
  8.             Alpha = Col.A
  9.         End Sub
  10.         Sub ToGray()
  11.             Dim Mid As Int16
  12.             Mid = (Red + Green + Blue) / 3
  13.             Red = Mid
  14.             Green = Mid
  15.             Blue = Mid
  16.         End Sub
  17.         Sub ToRed()
  18.             Dim Mid As Int16
  19.             Mid = (Red + Green + Blue) / 3
  20.             Red = 255
  21.             Green = Mid
  22.             Blue = Mid
  23.         End Sub
  24.         Sub ToBlue()
  25.             Dim Mid As Int16
  26.             Mid = (Red + Green + Blue) / 3
  27.             Blue = 255
  28.             Green = Mid
  29.             Red = Mid
  30.         End Sub
  31.         Sub ToGreen()
  32.             Dim Mid As Int16
  33.             Mid = (Red + Green + Blue) / 3
  34.             Green = 255
  35.             Red = Mid
  36.             Blue = Mid
  37.         End Sub
  38.         Sub ToNegative()
  39.             Red = 255 - Red
  40.             Green = 255 - Green
  41.             Blue = 255 - Blue
  42.         End Sub
  43.         Sub Brighter(ByVal Factor As Byte)
  44.             Red += Factor
  45.             Green += Factor
  46.             Blue += Factor
  47.             If Red > 255 Then
  48.                 Red = 255
  49.             End If
  50.             If Green > 255 Then
  51.                 Green = 255
  52.             End If
  53.             If Blue > 255 Then
  54.                 Blue = 255
  55.             End If
  56.         End Sub
  57.         Sub Darker(ByVal Factor As Byte)
  58.             Red -= Factor
  59.             Green -= Factor
  60.             Blue -= Factor
  61.             If Red < 0 Then
  62.                 Red = 0
  63.             End If
  64.             If Green < 0 Then
  65.                 Green = 0
  66.             End If
  67.             If Blue < 0 Then
  68.                 Blue = 0
  69.             End If
  70.         End Sub
  71.         Function GetInt32() As Int32
  72.             Dim C As Color
  73.             C = Color.FromArgb(Alpha, Red, Green, Blue)
  74.             Return C.ToArgb
  75.         End Function
  76.         Function GetColor() As Color
  77.             Dim C As Color
  78.             C = Color.FromArgb(Alpha, Red, Green, Blue)
  79.             Return C
  80.         End Function
  81.     End Structure
  82.     Public SaveName As String = Nothing
  83.     Public MouseDownStartPoint As Point
  84.     Public SelectedRegion As Rectangle = Nothing
  85.     Public Sub GetRegion(ByRef X, ByRef Y, ByRef W, ByRef H)
  86.         If SelectedRegion <> Nothing Then
  87.             With SelectedRegion
  88.                 X = .X
  89.                 Y = .Y
  90.                 W = .Width
  91.                 H = .Height
  92.             End With
  93.         Else
  94.             X = 0
  95.             Y = 0
  96.             W = imgP.Image.Width - 1
  97.             H = imgP.Image.Height - 1
  98.         End If
  99.     End Sub
  100.     Public Sub Colorize(ByVal Mode As String)
  101.         Dim X, Y, W, H As Single
  102.         Dim K As ARGB
  103.         Dim B As Drawing.Bitmap = imgP.Image.Clone
  104.  
  105.         GetRegion(X, Y, W, H)
  106.  
  107.         lblStatus.Text = "Colorizzazione in " + Mode + " in corso..."
  108.         Application.DoEvents()
  109.  
  110.         For IW As Single = X To X + W
  111.             For IH As Single = Y To Y + H
  112.                 K = New ARGB(B.GetPixel(IW, IH))
  113.                 If Mode = "scala di grigi" Then
  114.                     K.ToGray()
  115.                 ElseIf Mode = "rosso" Then
  116.                     K.ToRed()
  117.                 ElseIf Mode = "verde" Then
  118.                     K.ToGreen()
  119.                 ElseIf Mode = "blu" Then
  120.                     K.ToBlue()
  121.                 ElseIf Mode = "negativo" Then
  122.                     K.ToNegative()
  123.                 End If
  124.                 B.SetPixel(IW, IH, K.GetColor)
  125.             Next
  126.         Next
  127.         imgP.Image = B.Clone
  128.         lblStatus.Text = "Completato"
  129.     End Sub
  130.     Public Sub Brightness(ByVal Factor As Int16)
  131.         Dim X, Y, W, H As Single
  132.         Dim K As ARGB
  133.         Dim B As Drawing.Bitmap = imgP.Image.Clone
  134.  
  135.         GetRegion(X, Y, W, H)
  136.  
  137.         lblStatus.Text = "Aumento luminosità di " & Factor & " in corso..."
  138.         Application.DoEvents()
  139.  
  140.         For IW As Single = X To X + W
  141.             For IH As Single = Y To Y + H
  142.                 K = New ARGB(B.GetPixel(IW, IH))
  143.                 If Factor > 0 Then
  144.                     K.Brighter(Factor)
  145.                 Else
  146.                     K.Darker(-Factor)
  147.                 End If
  148.                 B.SetPixel(IW, IH, K.GetColor)
  149.             Next
  150.         Next
  151.  
  152.         imgP.Image = B.Clone
  153.         lblStatus.Text = "Completato"
  154.     End Sub
  155.     Public Sub Blend(ByVal Mode As String, ByVal Factor As Int16)
  156.         Dim B As Bitmap = imgP.Image.Clone
  157.         Dim X, Y, W, H As Single
  158.         Dim Incr, Lum As Single
  159.         Dim K As ARGB
  160.  
  161.         GetRegion(X, Y, W, H)
  162.         Incr = Factor / (W + 1)
  163.         Lum = 0
  164.  
  165.         lblStatus.Text = "Sfumatura " + Mode + " in corso..."
  166.         Application.DoEvents()
  167.  
  168.         If Mode = "da sinistra a destra" Then
  169.             For IW As Single = X To X + W
  170.                 For IH As Single = Y To Y + H
  171.                     K = New ARGB(B.GetPixel(IW, IH))
  172.                     If Lum > 0 Then
  173.                         K.Brighter(Lum)
  174.                     Else
  175.                         K.Darker(-Lum)
  176.                     End If
  177.                     B.SetPixel(IW, IH, K.GetColor)
  178.                 Next
  179.                 Lum += Incr
  180.             Next
  181.         ElseIf Mode = "da destra a sinistra" Then
  182.             For IW As Single = X + W To X Step -1
  183.                 For IH As Single = Y To Y + H
  184.                     K = New ARGB(B.GetPixel(IW, IH))
  185.                     If Lum > 0 Then
  186.                         K.Brighter(Lum)
  187.                     Else
  188.                         K.Darker(-Lum)
  189.                     End If
  190.                     B.SetPixel(IW, IH, K.GetColor)
  191.                 Next
  192.                 Lum += Incr
  193.             Next
  194.         ElseIf Mode = "dall'alto in basso" Then
  195.             For IH As Single = Y To Y + H
  196.                 For IW As Single = X To X + W
  197.                     K = New ARGB(B.GetPixel(IW, IH))
  198.                     If Lum > 0 Then
  199.                         K.Brighter(Lum)
  200.                     Else
  201.                         K.Darker(-Lum)
  202.                     End If
  203.                     B.SetPixel(IW, IH, K.GetColor)
  204.                 Next
  205.                 Lum += Incr
  206.             Next
  207.         ElseIf Mode = "dal basso all'alto" Then
  208.             For IH As Single = Y + H To Y Step -1
  209.                 For IW As Single = X To X + W
  210.                     K = New ARGB(B.GetPixel(IW, IH))
  211.                     If Lum > 0 Then
  212.                         K.Brighter(Lum)
  213.                     Else
  214.                         K.Darker(-Lum)
  215.                     End If
  216.                     B.SetPixel(IW, IH, K.GetColor)
  217.                 Next
  218.                 Lum += Incr
  219.             Next
  220.         End If
  221.  
  222.         lblStatus.Text = "Completato"
  223.         imgP.Image = B.Clone
  224.     End Sub
  225.     Private Sub strFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFileOpen.Click
  226.         Dim O As New OpenFileDialog
  227.         O.Filter = "File immagine|*.jpg;*.jpeg;*.bmp;*.tif;*.tiff;*.png;*.wmf;*.gif;*.emf;*.exif"
  228.         If O.ShowDialog = Windows.Forms.DialogResult.OK Then
  229.             imgP.Image = Image.FromFile(O.FileName)
  230.             Me.Width += imgP.Image.Width - imgP.Width
  231.             Me.Height += imgP.Image.Height - imgP.Height
  232.             SaveName = O.FileName
  233.             SelectedRegion = Nothing
  234.             strFileSave.Enabled = True
  235.             strFileSaveAs.Enabled = True
  236.             strPrint.Enabled = True
  237.             strColorize.Enabled = True
  238.             strPortrait.Enabled = True
  239.             strBlend.Enabled = True
  240.         End If
  241.     End Sub
  242.     Private Sub strFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFileSave.Click
  243.         Try
  244.             If SaveName <> Nothing Then
  245.                 imgP.Image.Save(SaveName)
  246.                 lblStatus.Text = "Immagine salvata"
  247.             Else
  248.                 Dim S As New SaveFileDialog
  249.                 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"
  250.                 If S.ShowDialog = Windows.Forms.DialogResult.OK Then
  251.                     imgP.Image.Save(S.FileName)
  252.                     lblStatus.Text = "Immagine salvata"
  253.                     SaveName = S.FileName
  254.                 End If
  255.             End If
  256.         Catch Ex As Exception
  257.             MsgBox("Impossibile salvare l'immagine a causa di un errore generico.", MsgBoxStyle.Exclamation)
  258.         End Try
  259.     End Sub
  260.     Private Sub strFileSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strFileSaveAs.Click
  261.         Dim S As New SaveFileDialog
  262.         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"
  263.         If S.ShowDialog = Windows.Forms.DialogResult.OK Then
  264.             imgP.Image.Save(S.FileName)
  265.             lblStatus.Text = "Immagine salvata"
  266.             SaveName = S.FileName
  267.         End If
  268.     End Sub
  269.     Private Sub strExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strExit.Click
  270.         Me.Close()
  271.     End Sub
  272.     Private Sub imgP_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles imgP.MouseDown
  273.         MouseDownStartPoint = imgP.PointToClient(MousePosition)
  274.     End Sub
  275.     Private Sub imgP_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles imgP.MouseMove
  276.         If MouseDownStartPoint <> Nothing Then
  277.             Dim P As Point = imgP.PointToClient(MousePosition)
  278.             Dim T As Point = MouseDownStartPoint
  279.             lblStatus.Text = "Seleziona regione da (" & T.X & "; " & T.Y & ") a (" & P.X & "; " & P.Y & ")"
  280.         End If
  281.     End Sub
  282.     Private Sub imgP_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles imgP.MouseUp
  283.         If MouseDownStartPoint <> Nothing Then
  284.             Dim P As Point = imgP.PointToClient(MousePosition)
  285.             Dim T As Point = MouseDownStartPoint
  286.             If P.X > T.X Then
  287.                 If P.Y > T.Y Then
  288.                     SelectedRegion = New Rectangle(T.X, T.Y, P.X - T.X, P.Y - T.Y)
  289.                 Else
  290.                     SelectedRegion = New Rectangle(T.X, P.Y, P.X - T.X, T.Y - P.Y)
  291.                 End If
  292.             Else
  293.                 If P.Y > T.Y Then
  294.                     SelectedRegion = New Rectangle(P.X, T.Y, T.X - P.X, P.Y - T.Y)
  295.                 Else
  296.                     SelectedRegion = New Rectangle(P.X, P.Y, T.X - P.X, T.Y - P.Y)
  297.                 End If
  298.             End If
  299.             MouseDownStartPoint = Nothing
  300.         End If
  301.     End Sub
  302.     Private Sub strGrey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strGrey.Click
  303.         Colorize("scala di grigi")
  304.     End Sub
  305.     Private Sub strRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRed.Click
  306.         Colorize("rosso")
  307.     End Sub
  308.     Private Sub strGreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strGreen.Click
  309.         Colorize("verde")
  310.     End Sub
  311.     Private Sub strBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strBlue.Click
  312.         Colorize("blu")
  313.     End Sub
  314.     Private Sub strNegative_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strNegative.Click
  315.         Colorize("negativo")
  316.     End Sub
  317.     Private Sub strBrighter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strBrighter.Click
  318.         Try
  319.             Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra 0 e 255.")
  320.             Brightness(CByte(F))
  321.         Catch ex As Exception
  322.             MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
  323.         End Try
  324.     End Sub
  325.     Private Sub strDarker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDarker.Click
  326.         Try
  327.             Dim F As String = InputBox("Inserire il fattore di diminuzione luminosità. Deve essere compreso tra 0 e 255.")
  328.             Brightness(-CByte(F))
  329.         Catch ex As Exception
  330.             MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
  331.         End Try
  332.     End Sub
  333.     Private Sub imgP_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgP.DoubleClick
  334.         SelectedRegion = New Rectangle(0, 0, imgP.Image.Width, imgP.Image.Height)
  335.         lblStatus.Text = "Selezione estesa a tutta l'immagine"
  336.     End Sub
  337.     Private Sub strLeftToRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strLeftToRight.Click
  338.         Try
  339.             Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
  340.             Blend("da sinistra a destra", CType(F, Int16))
  341.         Catch ex As Exception
  342.             MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
  343.         End Try
  344.     End Sub
  345.     Private Sub strRightToLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strRightToLeft.Click
  346.         Try
  347.             Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
  348.             Blend("da destra a sinistra", CType(F, Int16))
  349.         Catch ex As Exception
  350.             MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
  351.         End Try
  352.     End Sub
  353.     Private Sub strUpToDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strUpToDown.Click
  354.         Try
  355.             Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
  356.             Blend("dall'alto in basso", CType(F, Int16))
  357.         Catch ex As Exception
  358.             MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
  359.         End Try
  360.     End Sub
  361.     Private Sub strDownToUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strDownToUp.Click
  362.         Try
  363.             Dim F As String = InputBox("Inserire il fattore di aumento luminosità. Deve essere compreso tra -255 e 255.", "Sfumatura")
  364.             Blend("dal basso all'alto", CType(F, Int16))
  365.         Catch ex As Exception
  366.             MsgBox("Valore non corretto!", MsgBoxStyle.Exclamation)
  367.         End Try
  368.     End Sub
  369.     Private Sub strAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strAbout.Click
  370.         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)
  371.     End Sub
  372.     Private Sub strPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strPrint.Click
  373.         Dim S As String
  374.         If SaveName <> Nothing Then
  375.             S = SaveName
  376.         Else
  377.             imgP.Image.Save(Application.StartupPath + "\Temp.bmp")
  378.             S = Application.StartupPath + "\Temp.bmp"
  379.         End If
  380.         Dim P As New Process
  381.         P.StartInfo.FileName = SaveName
  382.         P.StartInfo.Verb = "Print"
  383.         P.Start()
  384.     End Sub
  385. End Class