I should note first that I just spent 4 hours exploring various variations on this question. Most of the answers references tutorials on MSDN which dealt with taking a block of color and drawing a new rectangle with a different hue. This is very different than what I'm looking to do. I want to take an image in a picturebox and rotate the hue for the entire image (anywhere from 1 to 359 degrees).
This question shows perfectly what I want to do: Rotate Hue in C#. Unfortunately, the answers reference C/C++, not C#. This is a first, I have no code to post, since nothing came close to accomplishing what I'm trying to accomplish. Thank you.
EDIT: I was just working with some code that I thought was unsuccessfully, turns out my results were just hidden, some hue changes were being made, and rather quickly, just not correctly. Here's what I have thus far:
private void ColorRotation_Click(object sender, EventArgs e)
{
if (pictureBoxMain.Image != null)
{
Bitmap image = new Bitmap(pictureBoxMain.Image);
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
float degrees = 60f;
double r = degrees * System.Math.PI / 180; // degrees to radians
float[][] colorMatrixElements = {
new float[] {(float)System.Math.Cos(r), (float)System.Math.Sin(r), 0, 0, 0},
new float[] {(float)-System.Math.Sin(r), (float)-System.Math.Cos(r), 0, 0, 0},
new float[] {0, 0, 2, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
Graphics myGraphics = CreateGraphics();
Rectangle myRectangle = new Rectangle();
pictureBoxMain.Image = image;
myRectangle.Width = pictureBoxMain.Width;
myRectangle.Height = pictureBoxMain.Height;
myGraphics.DrawImage(pictureBoxMain.Image, myRectangle, 30, 50, myRectangle.Width, myRectangle.Height, GraphicsUnit.Pixel, imageAttributes);
pictureBoxMain.Refresh();
}
}
There are two problems here:
I thought "float degrees = 60f;" would give me 60 degrees of rotation, it turns out I'm getting about 180 degrees of rotation. How do I correct this so I can get the correct amount of hue rotation? My expecting 60 degrees and getting 180 degrees is a huge error.
The result is not getting added to the pictureBox as a new image. It's getting painted on the form as a rectangle. I cannot find an overload (I tried all 30) for "myGraphics.DrawImage()" that accepts a pictureBox along with the required "GraphicsUnit.Pixel" and "imageAttributes". How can I update this code so that the changes will be made to the image in the picture box instead of drawn on the form? Perhaps myGraphics.DrawImage() is not the answer.
Many Thanks