Cameron Kloot

hi,
is there a way to programmatically change an image's hue
thanks




Re: Visual C# Express Edition change image hue?

Christopher Fleming

Hello Cameron...I believe the following thread will help you

http://www.thescripts.com/forum/thread236124.html




Re: Visual C# Express Edition change image hue?

JohnWein

Put a button at the top left of a form , a picturebox under the button and play with this:

Code Snippet

Imports System.Drawing

Imports System.Drawing.Imaging

Public Class Form1

Private Lbl(2) As Label, Txt(2) As TextBox

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

PictureBox1.Image = Image.FromFile("C:\Test.jpg")

PictureBox1.SizeMode = PictureBoxSizeMode.Zoom

For I As Int32 = 0 To 2

Lbl(I) = New Label

Lbl(I).AutoSize = True

Txt(I) = New TextBox

Txt(I).Width = 30

Txt(I).RightToLeft = Windows.Forms.RightToLeft.Yes

Txt(I).Text = "100"

Controls.Add(Lbl(I))

Controls.Add(Txt(I))

Next

Lbl(0).Left = Button1.Left + Button1.Width

Lbl(0).Text = "RED"

Txt(0).Left = Lbl(0).Left + Lbl(0).Width

Lbl(1).Left = Txt(0).Left + Txt(0).Width

Lbl(1).Text = "GREEN"

Txt(1).Left = Lbl(1).Left + Lbl(1).Width

Lbl(2).Left = Txt(1).Left + Txt(1).Width

Lbl(2).Text = "BlUE"

Txt(2).Left = Lbl(2).Left + Lbl(2).Width

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim IA As New ImageAttributes

Dim G As Graphics = Graphics.FromImage(PictureBox1.Image)

Dim CM As New ColorMatrix

CM.Matrix00 = CSng(Txt(0).Text) / 100

CM.Matrix11 = CSng(Txt(1).Text) / 100

CM.Matrix22 = CSng(Txt(2).Text) / 100

CM.Matrix33 = 1

CM.Matrix44 = 1

IA.SetColorMatrix(CM)

Dim R As New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)

G.DrawImage(PictureBox1.Image, R, R.X, R.Y, R.Width, R.Height, GraphicsUnit.Pixel, IA)

PictureBox1.Invalidate()

End Sub

End Class





Re: Visual C# Express Edition change image hue?

JohnWein

Whoops! Forgot where I was.

Same deal as the VB code but use the following code in the Form Load and Button Click events:

Code Snippet

using System;

using System.Windows.Forms;

using System.IO;

using System.Drawing;

using System.Drawing.Imaging;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private Label[] Lbl = new Label[3];

private TextBox[] Txt = new TextBox[3];

private void Form1_Load(object sender, EventArgs e)

{

pictureBox1.Image = Image.FromFile("C:\\Test.jpg");

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

for (Int32 I = 0; I <= 2; I++)

{

Lbl[I] = new Label();

Lbl[I].AutoSize = true;

Txt[I] = new TextBox();

Txt[I].Width = 30;

Txt[I].RightToLeft = RightToLeft;

Txt[I].Text = "100";

Controls.Add(Lbl[I]);

Controls.Add(Txt[I]);

}

Lbl[0].Left = button1.Left + button1.Width;

Lbl[0].Text = "RED";

Txt[0].Left = Lbl[0].Left + Lbl[0].Width;

Lbl[1].Left = Txt[0].Left + Txt[0].Width;

Lbl[1].Text = "GREEN";

Txt[1].Left = Lbl[1].Left + Lbl[1].Width;

Lbl[2].Left = Txt[1].Left + Txt[1].Width;

Lbl[2].Text = "BlUE";

Txt[2].Left = Lbl[2].Left + Lbl[2].Width;

}

private void button1_Click(object sender, EventArgs e)

{

ImageAttributes IA = new ImageAttributes();

Graphics G = Graphics.FromImage(pictureBox1.Image);

ColorMatrix CM = new ColorMatrix();

CM.Matrix00 = System.Convert.ToSingle(Txt[0].Text) / 100;

CM.Matrix11 = System.Convert.ToSingle(Txt[1].Text) / 100;

CM.Matrix22 = System.Convert.ToSingle(Txt[2].Text) / 100;

CM.Matrix33 = 1;

CM.Matrix44 = 1;

IA.SetColorMatrix(CM);

Rectangle R = new Rectangle(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height);

G.DrawImage(pictureBox1.Image, R, R.X, R.Y, R.Width, R.Height, GraphicsUnit.Pixel, IA);

pictureBox1.Invalidate();

}

}

}





Re: Visual C# Express Edition change image hue?

Cameron Kloot

well, i tried that and changed all the textboxes to 101 and clicked button one but it did nothing but slowly turn it black everytime i clicked the button Sad





Re: Visual C# Express Edition change image hue?

JohnWein

The code is designed to assist you in learning to use the color matrix of the ImageAttributes class to change the hue of an image. The numbers you enter in the Textboxes are percentages. It is not practical to limit the maximum value to 100 because if you entered 50 one time, you could enter 200 the next time to get back to approximately the same levels as originally.





Re: Visual C# Express Edition change image hue?

Citizen on the earth

Christopher Fleming wrote:
Hello Cameron...I believe the following thread will help you

http://www.thescripts.com/forum/thread236124.html

Hi Cameron,

Just as Christopher suggested, RGB colorspace to HSV (Hue,Saturation,Value) colorspace is a non-linear
transformation, and unfortunately, the ColorMatrix is strictly linear. The simple answer is that System.Drawing (GDI+) cannot change image hue. However, there are a couple of options for you.

1) You could lock the pixels of the image, and do the RGB -> HSV conversion mathematically

2) You can approximate a change of hue by implementing a "tinting" effect.

You can take a look at this thread as Christopher suggested for details - http://www.thescripts.com/forum/thread236124.html

Hope this helps,

Regards,

Citizens on the earth