Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads
 
Add your own
alternative version
Go to top

Color Scale Filter

, 17 Jul 2007
Grayscale and color scale filters.
' Demonstration project for gray and color scale image filter.
' 
' Copyright (c) 2007 Miran Uhan
' 
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the
'    Free Software Foundation, Inc.
'    51 Franklin Street, Fifth Floor
'    Boston, MA 02110-1301 USA
'
' For questions you can contact author at
'    miran.uhan@gmail.com
' Sugestions and bug reports are also welcome.
' 

Imports System.Drawing
Imports System.Drawing.Imaging

Public Class frmMain

   Private _imgOriginal As Image
   Private _imgFiltered As Image
   Private _imgResized As Image
   Private _filterGray As GrayScaleFilter = New GrayScaleFilter
   Private _filterColor As ColorScaleFilter = New ColorScaleFilter
   Private _mouseDown As Boolean = False

   Public Shared Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
      Dim j As Integer
      Dim encoders() As ImageCodecInfo
      encoders = ImageCodecInfo.GetImageEncoders()
      j = 0
      While j < encoders.Length
         If encoders(j).FormatID = format.Guid Then
            Return encoders(j)
         End If
         j += 1
      End While
      Return Nothing
   End Function

   Private Sub FilterImageGray()
      If (Not IsNothing(pnlOriginal.BackgroundImage)) Then
         pnlFiltered.BackgroundImage = Nothing
         pnlFiltered.BackgroundImage = _filterGray.ExecuteFilter(_imgResized)
      End If
   End Sub

   Private Sub FilterImageColor()
      If (Not IsNothing(pnlOriginal.BackgroundImage)) Then
         pnlFiltered.BackgroundImage = Nothing
         pnlFiltered.BackgroundImage = _filterColor.ExecuteFilter(_imgResized)
      End If
   End Sub

   Private Sub WhenTextChange(ByVal textBox As TextBox, ByVal slider As TrackBar)
      Try
         Dim value As Integer = 0
         If (textBox.Text = "") Or (textBox.Text = "-") Or (textBox.Text = "+") Then
            value = 0
         Else
            value = CInt(textBox.Text)
         End If
         slider.Value = value
      Catch ex As Exception
         textBox.Text = slider.Value.ToString()
      End Try
   End Sub

   Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      SaveAsToolStripMenuItem.Enabled = False
      picStartColor.BackColor = Color.FromArgb(102, 51, 51)
      pnlOriginal.BackgroundImage = Nothing
      pnlFiltered.BackgroundImage = Nothing
   End Sub

   Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
      Dim result As DialogResult = dlgFileOpen.ShowDialog()
      If result = Windows.Forms.DialogResult.OK Then
         _imgOriginal = Image.FromFile(dlgFileOpen.FileName)
         Dim scale As Double = 1
         If (_imgOriginal.Width > 280) Or (_imgOriginal.Height > 210) Then
            scale = System.Math.Min(280 / _imgOriginal.Width, 210 / _imgOriginal.Height)
         End If
         Dim imgFilter As ResizeFilter = New ResizeFilter
         imgFilter.Width = CInt(scale * _imgOriginal.Width)
         imgFilter.Height = CInt(scale * _imgOriginal.Height)
         _imgResized = imgFilter.ExecuteFilter(_imgOriginal)
         pnlOriginal.BackgroundImage = _imgResized
         If tabMain.SelectedTab.Equals(tabGray) Then
            pnlFiltered.BackgroundImage = _filterGray.ExecuteFilter(_imgResized)
         Else
            pnlFiltered.BackgroundImage = _filterColor.ExecuteFilter(_imgResized)
         End If
         SaveAsToolStripMenuItem.Enabled = True
      End If
   End Sub

   Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
      dlgFileSave.FileName = ""
      Dim result As DialogResult = dlgFileSave.ShowDialog()
      If result = Windows.Forms.DialogResult.OK Then
         Me.Cursor = Cursors.WaitCursor
         Dim ext As String = Mid(dlgFileSave.FileName, dlgFileSave.FileName.LastIndexOf(".") + 2)
         Dim myEncoderParameters As EncoderParameters = Nothing
         Dim myImageCodecInfo As ImageCodecInfo
         Select Case ext.ToLower()
            Case "jpg"
               myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg)
               myEncoderParameters = New EncoderParameters(1)
               myEncoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, CType(90L, Int32))
            Case "gif"
               myImageCodecInfo = GetEncoderInfo(ImageFormat.Gif)
            Case "png"
               myImageCodecInfo = GetEncoderInfo(ImageFormat.Png)
            Case "bmp"
               myImageCodecInfo = GetEncoderInfo(ImageFormat.Bmp)
            Case Else
               MsgBox("Image format " & ext & " is not suported!", MsgBoxStyle.Exclamation)
               Exit Sub
         End Select
         If tabMain.SelectedTab.Equals(tabGray) Then
            _imgFiltered = _filterGray.ExecuteFilter(_imgOriginal)
         Else
            _imgFiltered = _filterColor.ExecuteFilter(_imgOriginal)
         End If
         _imgFiltered.Save(dlgFileSave.FileName, myImageCodecInfo, myEncoderParameters)
         Me.Cursor = Cursors.Default
      End If
   End Sub

   Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
      Me.Close()
   End Sub

   Private Sub rdbGrayNormal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbGrayNormal.CheckedChanged
      _filterGray.Brightness = Brightness.Normal
      _sliderGrayRed.Enabled = False
      _sliderGrayRed.Value = 30
      _txtGrayRed.Enabled = False
      _txtGrayRed.Text = "30"
      _sliderGrayGreen.Enabled = False
      _sliderGrayGreen.Value = 59
      _txtGrayGreen.Enabled = False
      _txtGrayGreen.Text = "59"
      _sliderGrayBlue.Enabled = False
      _sliderGrayBlue.Value = 11
      _txtGrayBlue.Enabled = False
      _txtGrayBlue.Text = "11"
      FilterImageGray()
   End Sub

   Private Sub rdbGrayBright_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbGrayBright.CheckedChanged
      _filterGray.Brightness = Brightness.Bright
      _sliderGrayRed.Enabled = False
      _sliderGrayRed.Value = 50
      _txtGrayRed.Enabled = False
      _txtGrayRed.Text = "50"
      _sliderGrayGreen.Enabled = False
      _sliderGrayGreen.Value = 50
      _txtGrayGreen.Enabled = False
      _txtGrayGreen.Text = "50"
      _sliderGrayBlue.Enabled = False
      _sliderGrayBlue.Value = 50
      _txtGrayBlue.Enabled = False
      _txtGrayBlue.Text = "50"
      FilterImageGray()
   End Sub

   Private Sub rdbGrayCustom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbGrayCustom.CheckedChanged
      _filterGray.Brightness = Brightness.Custom
      _sliderGrayRed.Enabled = True
      _txtGrayRed.Enabled = True
      _sliderGrayGreen.Enabled = True
      _txtGrayGreen.Enabled = True
      _sliderGrayBlue.Enabled = True
      _txtGrayBlue.Enabled = True
   End Sub

   Private Sub sliderGrayRed_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderGrayRed.MouseDown
      _mouseDown = True
   End Sub

   Private Sub sliderGrayRed_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderGrayRed.MouseUp
      _mouseDown = False
      If (CDbl(sliderGrayRed.Value) <> _filterGray.FactorRed) Then
         _filterGray.FactorRed = CDbl(sliderGrayRed.Value)
         FilterImageGray()
      End If
   End Sub

   Private Sub sliderGrayRed_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sliderGrayRed.Scroll
      txtGrayRed.Text = sliderGrayRed.Value.ToString()
   End Sub

   Private Sub sliderGrayGreen_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderGrayGreen.MouseDown
      _mouseDown = True
   End Sub

   Private Sub sliderGrayGreen_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderGrayGreen.MouseUp
      _mouseDown = False
      If (CDbl(sliderGrayGreen.Value) <> _filterGray.FactorGreen) Then
         _filterGray.FactorGreen = CDbl(sliderGrayGreen.Value)
         FilterImageGray()
      End If
   End Sub

   Private Sub sliderGrayGreen_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sliderGrayGreen.Scroll
      txtGrayGreen.Text = sliderGrayGreen.Value.ToString()
   End Sub

   Private Sub sliderGrayBlue_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderGrayBlue.MouseDown
      _mouseDown = True
   End Sub

   Private Sub sliderGrayBlue_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderGrayBlue.MouseUp
      _mouseDown = False
      If (CDbl(sliderGrayBlue.Value) <> _filterGray.FactorBlue) Then
         _filterGray.FactorBlue = CDbl(sliderGrayBlue.Value)
         FilterImageGray()
      End If
   End Sub

   Private Sub sliderGrayBlue_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sliderGrayBlue.Scroll
      txtGrayBlue.Text = sliderGrayBlue.Value.ToString()
   End Sub

   Private Sub txtGrayRed_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGrayRed.TextChanged
      If (Not _mouseDown) Then
         WhenTextChange(txtGrayRed, sliderGrayRed)
         If (CDbl(sliderGrayRed.Value) <> _filterGray.FactorRed) Then
            _filterGray.FactorRed = CDbl(sliderGrayRed.Value)
            FilterImageGray()
         End If
      End If
   End Sub

   Private Sub txtGrayGreen_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGrayGreen.TextChanged
      If (Not _mouseDown) Then
         WhenTextChange(txtGrayGreen, sliderGrayGreen)
         If (CDbl(sliderGrayGreen.Value) <> _filterGray.FactorGreen) Then
            _filterGray.FactorGreen = CDbl(sliderGrayGreen.Value)
            FilterImageGray()
         End If
      End If
   End Sub

   Private Sub txtGrayBlue_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGrayBlue.TextChanged
      If (Not _mouseDown) Then
         WhenTextChange(txtGrayBlue, sliderGrayBlue)
         If (CDbl(sliderGrayBlue.Value) <> _filterGray.FactorBlue) Then
            _filterGray.FactorBlue = CDbl(sliderGrayBlue.Value)
            FilterImageGray()
         End If
      End If
   End Sub

   Private Sub rdbColorNormal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbColorNormal.CheckedChanged
      _filterColor.Brightness = Brightness.Normal
      _sliderColorRed.Enabled = False
      _sliderColorRed.Value = 30
      _txtColorRed.Enabled = False
      _txtColorRed.Text = "30"
      _sliderColorGreen.Enabled = False
      _sliderColorGreen.Value = 59
      _txtColorGreen.Enabled = False
      _txtColorGreen.Text = "59"
      _sliderColorBlue.Enabled = False
      _sliderColorBlue.Value = 11
      _txtColorBlue.Enabled = False
      _txtColorBlue.Text = "11"
      FilterImageColor()
   End Sub

   Private Sub rdbColorBright_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbColorBright.CheckedChanged
      _filterColor.Brightness = Brightness.Bright
      _sliderColorRed.Enabled = False
      _sliderColorRed.Value = 50
      _txtColorRed.Enabled = False
      _txtColorRed.Text = "50"
      _sliderColorGreen.Enabled = False
      _sliderColorGreen.Value = 50
      _txtColorGreen.Enabled = False
      _txtColorGreen.Text = "50"
      _sliderColorBlue.Enabled = False
      _sliderColorBlue.Value = 50
      _txtColorBlue.Enabled = False
      _txtColorBlue.Text = "50"
      FilterImageColor()
   End Sub

   Private Sub rdbColorCustom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbColorCustom.CheckedChanged
      _filterColor.Brightness = Brightness.Custom
      _sliderColorRed.Enabled = True
      _txtColorRed.Enabled = True
      _sliderColorGreen.Enabled = True
      _txtColorGreen.Enabled = True
      _sliderColorBlue.Enabled = True
      _txtColorBlue.Enabled = True
   End Sub

   Private Sub sliderColorRed_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderColorRed.MouseDown
      _mouseDown = True
   End Sub

   Private Sub sliderColorRed_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderColorRed.MouseUp
      _mouseDown = False
      If (CDbl(sliderColorRed.Value) <> _filterColor.FactorRed) Then
         _filterColor.FactorRed = CDbl(sliderColorRed.Value)
         FilterImageColor()
      End If
   End Sub

   Private Sub sliderColorRed_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sliderColorRed.Scroll
      txtColorRed.Text = sliderColorRed.Value.ToString()
   End Sub

   Private Sub sliderColorGreen_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderColorGreen.MouseDown
      _mouseDown = True
   End Sub

   Private Sub sliderColorGreen_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderColorGreen.MouseUp
      _mouseDown = False
      If (CDbl(sliderColorGreen.Value) <> _filterColor.FactorGreen) Then
         _filterColor.FactorGreen = CDbl(sliderColorGreen.Value)
         FilterImageColor()
      End If
   End Sub

   Private Sub sliderColorGreen_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sliderColorGreen.Scroll
      txtColorGreen.Text = sliderColorGreen.Value.ToString()
   End Sub

   Private Sub sliderColorBlue_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderColorBlue.MouseDown
      _mouseDown = True
   End Sub

   Private Sub sliderColorBlue_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles sliderColorBlue.MouseUp
      _mouseDown = False
      If (CDbl(sliderColorBlue.Value) <> _filterColor.FactorBlue) Then
         _filterColor.FactorBlue = CDbl(sliderColorBlue.Value)
         FilterImageColor()
      End If
   End Sub

   Private Sub sliderColorBlue_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sliderColorBlue.Scroll
      txtColorBlue.Text = sliderColorBlue.Value.ToString()
   End Sub

   Private Sub txtColorRed_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorRed.TextChanged
      If (Not _mouseDown) Then
         WhenTextChange(txtColorRed, sliderColorRed)
         If (CDbl(sliderColorRed.Value) <> _filterColor.FactorRed) Then
            _filterColor.FactorRed = CDbl(sliderColorRed.Value)
            FilterImageColor()
         End If
      End If
   End Sub

   Private Sub txtColorGreen_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorGreen.TextChanged
      If (Not _mouseDown) Then
         WhenTextChange(txtColorGreen, sliderColorGreen)
         If (CDbl(sliderColorGreen.Value) <> _filterColor.FactorGreen) Then
            _filterColor.FactorGreen = CDbl(sliderColorGreen.Value)
            FilterImageColor()
         End If
      End If
   End Sub

   Private Sub txtColorBlue_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtColorBlue.TextChanged
      If (Not _mouseDown) Then
         WhenTextChange(txtColorBlue, sliderColorBlue)
         If (CDbl(sliderColorBlue.Value) <> _filterColor.FactorBlue) Then
            _filterColor.FactorBlue = CDbl(sliderColorBlue.Value)
            FilterImageColor()
         End If
      End If
   End Sub

   Private Sub tabMain_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabMain.SelectedIndexChanged
      If tabMain.SelectedTab.Equals(tabGray) Then
         FilterImageGray()
      Else
         FilterImageColor()
      End If
   End Sub

   Private Sub btnStartColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartColor.Click
      Dim result As DialogResult = dlgColor.ShowDialog()
      If result = Windows.Forms.DialogResult.OK Then
         Dim c As Color = dlgColor.Color
         picStartColor.BackColor = c
         _filterColor.StartColor = c
         FilterImageColor()
      End If
   End Sub

   Private Sub btnEndColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEndColor.Click
      Dim result As DialogResult = dlgColor.ShowDialog()
      If result = Windows.Forms.DialogResult.OK Then
         Dim c As Color = dlgColor.Color
         picEndColor.BackColor = c
         _filterColor.EndColor = c
         FilterImageColor()
      End If
   End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Miran.Uhan
Engineer
Slovenia Slovenia
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 17 Jul 2007
Article Copyright 2007 by Miran.Uhan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid