Results 1 to 12 of 12

Thread: Change color HUE of picturebox ?!

  1. #1

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Question Change color HUE of picturebox ?!

    Hello there.

    I found this to do the hue changing stuff in vb6. i need to do it in vb.net for now.

    How can I find one ?

    Thanks in advance

  2. #2
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Re: Change color HUE of picturebox ?!

    Assuming your Picture Box is PictureBox1, try this:

    PictureBox1.BackColor = Color.Bisque When you type the equal sign, you will get a list of colors to pick from.

  3. #3

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Change color HUE of picturebox ?!

    Quote Originally Posted by Whizzo View Post
    Assuming your Picture Box is PictureBox1, try this:

    PictureBox1.BackColor = Color.Bisque When you type the equal sign, you will get a list of colors to pick from.
    I do not want the static backcolor setting. i need to change hue via a trackbar. Take a close look at this

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,294

    Re: Change color HUE of picturebox ?!

    Quote Originally Posted by Whizzo View Post
    Assuming your Picture Box is PictureBox1, try this:

    PictureBox1.BackColor = Color.Bisque When you type the equal sign, you will get a list of colors to pick from.
    Changing the backcolor of a picturebox won't change the hue of an image at all. I assume you didn't even look at the link.
    "How can I find one?"
    Probably the way you find anything, by looking. Its pretty rare that you find something you want without looking for it.
    Have you already done a search along the lines of: Vb.Net change hue

    An alternative would be to understand what the VB6 code is doing and replicate the functionality by writing your own code in VB.Net, but that would probably be an inferior result as there may be some capabilities added to .Net accessible libraries that make the job easier than it was in VB6, so you wouldn't want to replicate the VB6 implementation.
    Last edited by passel; Jan 24th, 2017 at 02:24 PM.

  5. #5

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Change color HUE of picturebox ?!

    Quote Originally Posted by passel View Post
    Changing the backcolor of a picturebox won't change the hue of an image at all. I assume you didn't even look at the link.

    Probably the way you find anything, by looking. Its pretty rare that you find something you want without looking for it.
    Have you already done a search along the lines of: Vb.Net change hue

    An alternative would be to understand what the VB6 code is doing and replicate the functionality by writing your own code in VB.Net, but that would probably be an inferior result as there may be some capabilities added to .Net accessible libraries that make the job easier than it was in VB6, so you wouldn't want to replicate the VB6 implementation.
    If I could make head of the code , i would code it myself in vb.net and I wouldn't post a thread here!
    This is really difficult for me. By the way , I searched before posting but I couldn't find any case similar to mine.

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,163

    Re: Change color HUE of picturebox ?!

    I couldn't find a good VB.Net example of recoloring an image (which clearly what your vb6 example is about) on a quick search. So I ran up a quick-and-dirty example of how it can be done in VB.Net. Basically, it uses a ColorMatrix to transform the R, G and B components of all the colors.

    Start a Form (let's call it Form1), and throw on a PictureBox (PictureBox1). Set the picture box's SizeMode to Zoom (or to StretchImage if you prefer) and the Image to whatever you want to recolor.

    Add a TrackBar (TrackBar1) and set its properties like this:
    - AutoSize = False
    - LargeChange = 6
    - Maximum = 100
    - SmallChange = 2
    - TickStyle = None
    - Value = 50.
    Leave other properties default. With Autosize=False you can make the trackbar fairly thin and stack up 3 of them under the PictureBox.

    Now copy/paste 2 copies of TrackBar1. Set the Backcolor of TrackBar1 to Red, TrackBar2 to Green and Trackbar3 to Blue

    Then code Form1 as follows:
    Code:
      Private sourceImage As Image
       Private red As Integer = 50
       Private green As Integer = 50
       Private blue As Integer = 50
    
       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          sourceImage = New Bitmap(PictureBox1.Image)
       End Sub
    
       Private Sub TrackBars_Scroll(sender As Object, e As EventArgs) Handles TrackBar3.Scroll, TrackBar2.Scroll, TrackBar1.Scroll
          Dim rChange As Integer = TrackBar1.Value - red
          Dim gChange As Integer = TrackBar2.Value - green
          Dim bChange As Integer = TrackBar3.Value - blue
          red += rChange - gChange \ 2 - bChange \ 2
          green += -rChange \ 2 + gChange - bChange \ 2
          blue += -rChange \ 2 - gChange \ 2 + bChange
          TrackBar1.Value = Math.Min(Math.Max(red, 0), 100)
          TrackBar2.Value = Math.Min(Math.Max(green, 0), 100)
          TrackBar3.Value = Math.Min(Math.Max(blue, 0), 100)
          RecolorImage()
       End Sub
    
       Private Sub RecolorImage()
          Dim bmp As Bitmap = New Bitmap(sourceImage)
          Using g As Graphics = Graphics.FromImage(bmp)
             Using ia As New ImageAttributes
                Dim cm As New ColorMatrix
                cm.Matrix00 = red / 50.0F
                cm.Matrix11 = green / 50.0F
                cm.Matrix22 = blue / 50.0F
                ia.SetColorMatrix(cm)
                Dim destRect As New Rectangle(Point.Empty, bmp.Size)
                g.DrawImage(bmp, destRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia)
             End Using
          End Using
          PictureBox1.Image = bmp
       End Sub
    Drag the trackbars around to change the color. Give it a try and say if it does roughly what you want. Note that it doesn't use Hue (in the technical sense of a property in the HSB color space) but it could be done on a Hue basis with a fair bit of extra code, if that's really needed.

    BB

  7. #7

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Change color HUE of picturebox ?!

    Quote Originally Posted by boops boops View Post
    I couldn't find a good VB.Net example of recoloring an image (which clearly what your vb6 example is about) on a quick search. So I ran up a quick-and-dirty example of how it can be done in VB.Net. Basically, it uses a ColorMatrix to transform the R, G and B components of all the colors.

    Start a Form (let's call it Form1), and throw on a PictureBox (PictureBox1). Set the picture box's SizeMode to Zoom (or to StretchImage if you prefer) and the Image to whatever you want to recolor.

    Add a TrackBar (TrackBar1) and set its properties like this:
    - AutoSize = False
    - LargeChange = 6
    - Maximum = 100
    - SmallChange = 2
    - TickStyle = None
    - Value = 50.
    Leave other properties default. With Autosize=False you can make the trackbar fairly thin and stack up 3 of them under the PictureBox.

    Now copy/paste 2 copies of TrackBar1. Set the Backcolor of TrackBar1 to Red, TrackBar2 to Green and Trackbar3 to Red.

    Then code Form1 as follows:
    Code:
      Private sourceImage As Image
       Private red As Integer = 50
       Private green As Integer = 50
       Private blue As Integer = 50
    
       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          sourceImage = New Bitmap(PictureBox1.Image)
       End Sub
    
       Private Sub TrackBars_Scroll(sender As Object, e As EventArgs) Handles TrackBar3.Scroll, TrackBar2.Scroll, TrackBar1.Scroll
          Dim rChange As Integer = TrackBar1.Value - red
          Dim gChange As Integer = TrackBar2.Value - green
          Dim bChange As Integer = TrackBar3.Value - blue
          red += rChange - gChange \ 2 - bChange \ 2
          green += -rChange \ 2 + gChange - bChange \ 2
          blue += -rChange \ 2 - gChange \ 2 + bChange
          TrackBar1.Value = Math.Min(Math.Max(red, 0), 100)
          TrackBar2.Value = Math.Min(Math.Max(green, 0), 100)
          TrackBar3.Value = Math.Min(Math.Max(blue, 0), 100)
          RecolorImage()
       End Sub
    
       Private Sub RecolorImage()
          Dim bmp As Bitmap = New Bitmap(sourceImage)
          Using g As Graphics = Graphics.FromImage(bmp)
             Using ia As New ImageAttributes
                Dim cm As New ColorMatrix
                cm.Matrix00 = red / 50.0F
                cm.Matrix11 = green / 50.0F
                cm.Matrix22 = blue / 50.0F
                ia.SetColorMatrix(cm)
                Dim destRect As New Rectangle(Point.Empty, bmp.Size)
                g.DrawImage(bmp, destRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia)
             End Using
          End Using
          PictureBox1.Image = bmp
       End Sub
    Drag the trackbars around to change the color. Give it a try and say if it does roughly what you want. Note that it doesn't use Hue (in the technical sense of a property in the HSB color space) but it could be done on a Hue basis with a fair bit of extra code, if that's really needed.

    BB
    this part of your code needs a bit of change :
    Code:
     Private Sub RecolorImage()
            Dim bmp As Bitmap = New Bitmap(sourceImage)
            Using g As Graphics = Graphics.FromImage(bmp)
                Using ia As New Imaging.ImageAttributes
                    Dim cm As New Imaging.ColorMatrix
                    cm.Matrix00 = red / 50.0F
                    cm.Matrix11 = green / 50.0F
                    cm.Matrix22 = blue / 50.0F
                    ia.SetColorMatrix(cm)
                    Dim destRect As New Rectangle(Point.Empty, bmp.Size)
                    g.DrawImage(bmp, destRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia)
                End Using
            End Using
            PictureBox1.Image = bmp
        End Sub
    That's good , but I need a single trackbar of hue value to change stuff or If you can add up saturation and lightness trackbars , it would be great

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,163

    Re: Change color HUE of picturebox ?!

    You can find the formula for converting HSL values to RGB here. It shouldn't be hard to transform it into VB.Net code, or perhaps to find something ready-made on the web.

    Unfortunately I won't have time to do this in the near future. I assume it will be possible to apply the resulting RGB values to the ColorMatrix Matrix00, Matrix11 and Matrix22 fields in a similar way to my quick-and-dirty code, possibly with a normalization factor to keep the grayscale equivalent constant.

    There may of course be other ways to do it. But I don't know if there is an established theoretical model for recolouring an existing image, so I would simply do it by trial and error: if I can produce a satisfactory range of colour variation, it's good enough for me. Something I would also do would be to add a slider for opacity (the Matrix33 field), since that would make it easy to adjust the strength of the recoloring effect (by superimposing the partly transparent recolored image on the original).

    BB

  9. #9

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Change color HUE of picturebox ?!

    Quote Originally Posted by boops boops View Post
    Unfortunately I won't have time to do this in the near future. I assume it will be possible to apply the resulting RGB values to the ColorMatrix Matrix00, Matrix11 and Matrix22 fields in a similar way to my quick-and-dirty code, possibly with a normalization factor to keep the grayscale equivalent constant.

    There may of course be other ways to do it. But I don't know if there is an established theoretical model for recolouring an existing image, so I would simply do it by trial and error: if I can produce a satisfactory range of colour variation, it's good enough for me. Something I would also do would be to add a slider for opacity (the Matrix33 field), since that would make it easy to adjust the strength of the recoloring effect (by superimposing the partly transparent recolored image on the original).

    BB
    Replying to the thread is appreciable , let alone spending your time and do coding stuff for someone

    Any other one pro to help me round off this thread ?

  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,163

    Re: Change color HUE of picturebox ?!

    Confession time I just took a another look at the VB6 site you linked to and I realized that I had misunderstood the idea. The webpage explains clearly what it means by colorization: to keep the saturation and value of each pixel but replace its hue by the value set with the trackbar. It shouldn't be too hard to implement that. I wouldn't do it now using ColorMatrix (not sure if it's even possible) but with pixel data arrays. I'll see if I can throw something together in the next few days if no one else offers a solution meanwhile.

    BB

  11. #11

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Change color HUE of picturebox ?!

    Quote Originally Posted by boops boops View Post
    Confession time I just took a another look at the VB6 site you linked to and I realized that I had misunderstood the idea. The webpage explains clearly what it means by colorization: to keep the saturation and value of each pixel but replace its hue by the value set with the trackbar. It shouldn't be too hard to implement that. I wouldn't do it now using ColorMatrix (not sure if it's even possible) but with pixel data arrays. I'll see if I can throw something together in the next few days if no one else offers a solution meanwhile.

    BB
    God bless you buddy. I would be really thankful to you

  12. #12
    Lively Member
    Join Date
    Jan 2020
    Posts
    121

    Re: Change color HUE of picturebox ?!

    Code:
    Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click '<--This is where we append the objects
        ActivePbox.BackColor = sender.backcolor
        Me.Close()
    End Sub
    Last edited by Prahlad; Sep 13th, 2020 at 11:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width