Witam czy ktoś pomoże mi żeby po przesuwaniu wartości w trackbarze widoczne były zmiany w picturebox'ie (tu zmiany na obrazku) i przycisk resetujacy trackbar i obrazek w picturebox'ie.
using System;
namespace PrzetwarzanieObrazow
{
public partial class CmykHsl : Form
{
PictureBox picture1;
PictureBox picture2;
public CmykHsl(ref PictureBox we_pic1, ref PictureBox we_pic2)
{
InitializeComponent();
picture1 = we_pic1;
picture2 = we_pic2;
}
private void CmykHsl_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap mapa_bitowa_we = (Bitmap)picture1.Image;
Bitmap mapa_bitowa = (Bitmap)picture2.Image;
BitmapData bmd_we = mapa_bitowa_we.LockBits(new Rectangle(0, 0,
mapa_bitowa.Width, mapa_bitowa_we.Height), ImageLockMode.ReadWrite,
mapa_bitowa_we.PixelFormat);
BitmapData bmd = mapa_bitowa.LockBits(new Rectangle(0, 0,
mapa_bitowa.Width, mapa_bitowa.Height), ImageLockMode.ReadWrite,
mapa_bitowa.PixelFormat);
unsafe
{
byte* wskWe = (byte*)bmd_we.Scan0;
byte* wskWy = (byte*)bmd.Scan0;
int nOffset = bmd.Stride - mapa_bitowa.Width * 3;
//********************************************
long wielko = ((IntPtr)((byte*)bmd.Scan0.ToInt64()
+ (long)mapa_bitowa.Width * (long)3 * ((long)mapa_bitowa.Height
+ (long)nOffset))).ToInt64();
int r = 0, g = 0, b = 0;
float c = 0, ct = 0, m = 0, mt = 0, y = 0, yt = 0, k = 0, przelicz_kon = (float)1 / 255;
do
{
{
b = wskWe++[0];
g = wskWe++[0];
r = wskWe++[0];
ct = 1 - (przelicz_kon * r);
mt = 1 - (przelicz_kon * g);
yt = 1 - (przelicz_kon * b);
k = Math.Min(ct, Math.Min(mt, yt));
if (k == 1) k = (float)0.9999999;
c = (ct - k) / (1 - k);
m = (mt - k) / (1 - k);
y = (yt - k) / (1 - k);
c = c + ((float)trackBar1.Value / 10);
if (c < 0) c = 0;
if (c > 1) c = 1;
m = m + ((float)trackBar2.Value / 10);
if (m < 0) m = 0;
if (m > 1) m = 1;
y = y + ((float)trackBar3.Value / 10);
if (y < 0) y = 0;
if (y > 1) y = 1;
k = k + ((float)trackBar4.Value / 10);
if (k < 0) k = 0;
if (k > 1) k = 1;
ct = c * (1 - k) + k;
mt = m * (1 - k) + k;
yt = y * (1 - k) + k;
r = (int)Math.Round((1 - ct) * 255);
g = (int)Math.Round((1 - mt) * 255);
b = (int)Math.Round((1 - yt) * 255);
wskWy++[0] = (byte)b;
wskWy++[0] = (byte)g;
wskWy++[0] = (byte)r;
}
} while
((long)wskWy < wielko);
mapa_bitowa.UnlockBits(bmd);
mapa_bitowa_we.UnlockBits(bmd);
picture2.Refresh();
}
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap mapa_bitowa_we = (Bitmap)picture1.Image;
Bitmap mapa_bitowa = (Bitmap)picture2.Image;
BitmapData bmd_we = mapa_bitowa_we.LockBits(new Rectangle(0, 0,
mapa_bitowa.Width, mapa_bitowa_we.Height), ImageLockMode.ReadWrite,
mapa_bitowa_we.PixelFormat);
BitmapData bmd = mapa_bitowa.LockBits(new Rectangle(0, 0,
mapa_bitowa.Width, mapa_bitowa.Height), ImageLockMode.ReadWrite,
mapa_bitowa.PixelFormat);
unsafe
{
byte* wskWe = (byte*)bmd_we.Scan0;
byte* wskWy = (byte*)bmd.Scan0;
int nOffset = bmd.Stride - mapa_bitowa.Width * 3;
//********************************************
long wielko = ((IntPtr)((byte*)bmd.Scan0.ToInt64()
+ (long)mapa_bitowa.Width * (long)3 * ((long)mapa_bitowa.Height
+ (long)nOffset))).ToInt64();
byte r = 0, g = 0, b = 0;
float r_norm = 0, g_norm = 0, b_norm = 0, l = 0, s = 0, h = 0, dm = 0
, min = 0, max = 0, q = 0, p = 0, tr = 0, tg = 0, tb = 0
, przelicz_kon = (float)1 / 255;
do
{
{
b = wskWe++[0];
g = wskWe++[0];
r = wskWe++[0];
//for (int i = 0; i < picture1.Image.Width; i++)
// for (int j = 0; j < picture1.Image.Height; j++)
// {
// r = ((Bitmap)picture1.Image).GetPixel(i, j).R;
// g = ((Bitmap)picture1.Image).GetPixel(i, j).G;
// b = ((Bitmap)picture1.Image).GetPixel(i, j).B;
r_norm = przelicz_kon * r;
g_norm = przelicz_kon * g;
b_norm = przelicz_kon * b;
min = Math.Min(r_norm, Math.Min(g_norm, b_norm));
max = Math.Max(r_norm, Math.Max(g_norm, b_norm));
dm = (float)Math.Abs(max - min);
//L
l = (max + min) / 2f;
//S
if (max == min)
{ s = 0; }
else
{
if ((l > 0) && (l <= 0.5))
{ s = dm / (2 * l); }
else
if (l > 0.5)
{ s = dm / (2 - (2 * l)); }
}
//H
if (min == max)
{ h = 0; }
if ((max == r_norm) && (g_norm >= b_norm))
{ h = 60 * ((g_norm - b_norm) / dm); }
if ((max == r_norm) && (g_norm < b_norm))
{ h = (60 * ((g_norm - b_norm) / dm)) + 360; }
if (max == g_norm)
{ h = (60 * ((b_norm - r_norm) / dm)) + 120; }
if (max == b_norm)
{ h = (60 * ((r_norm - g_norm) / dm)) + 240; }
//
h = h + tb_h.Value;
if (h > 360)
h = h % 360;
if (h < 0)
h = 360 + h;
l = l + (float)tb_l.Value / 100;
if (l > 1) l = 1;
if (l < 0) l = 0;
s = s + (float)tb_s.Value / 100;
if (s > 1) s = 1;
if (s < 0) s = 0;
//
if (s == 0)
{
r_norm = l;
g_norm = l;
b_norm = l;
}
else
{
if (l < 0.5)
{ q = l * (1 + s); }
if (l >= 0.5)
{ q = (l + s) - (l * s); }
p = (2 * l) - q;
h = h / 360;
tr = h + ((float)1 / 3);
tg = h;
tb = h - ((float)1 / 3);
if (tr < 0)
{ tr += 1; }
if (tr > 1)
{ tr -= 1; }
if (tg < 0)
{ tg += 1; }
if (tg > 1)
{ tg -= 1; }
if (tb < 0)
{ tb += 1; }
if (tb > 1)
{ tb -= 1; }
r_norm = Tc_to_RGB(tr, p, q);
g_norm = Tc_to_RGB(tg, p, q);
b_norm = Tc_to_RGB(tb, p, q);
}
r = (byte)Math.Round(r_norm * 255);
g = (byte)Math.Round(g_norm * 255);
b = (byte)Math.Round(b_norm * 255);
// ((Bitmap)picture2.Image).SetPixel(i,j,Color.FromArgb(r,g,b));
//}
wskWy++[0] = b;
wskWy++[0] = g;
wskWy++[0] = r;
}
} while
((long)wskWy < wielko);
mapa_bitowa.UnlockBits(bmd);
mapa_bitowa_we.UnlockBits(bmd);
picture2.Refresh();
}
}
private float Tc_to_RGB(float tc, float p, float q)
{
float wynik = 0;
if (tc < (1 / 6f))
{ wynik = p + ((q - p) * 6f * tc); }
if ((tc >= (1 / 6f)) && (tc < 0.5))
{ wynik = q; }
if ((tc >= (0.5) && (tc < (2 / 3f))))
{ wynik = p + ((q - p) * 6f * ((2 / 3f) - tc)); }
if (tc >= (2 / 3f))
{ wynik = p; }
//Albo tak ja nożej kod ze strony : http://130.113.54.154/~monger/hsl-rgb.html
//if (tc * 6 < 1)
//{ wynik = p + ((q - p) * 6f * tc); }
//else
// if (tc * 2 < 1)
// { wynik = q; }
// else
// if (tc * 3 < 2)
// { wynik = p + ((q - p) * 6f * ((2 / 3f) - tc)); }
// else
// { wynik = p; }
return wynik;
}
private void button3_Click(object sender, EventArgs e)
{
/*
trackBar1.Value = 0;
trackBar2.Value = 0;
trackBar3.Value = 0;
trackBar4.Value = 0;
tb_h.Value = 0;
tb_s.Value = 0;
tb_l.Value = 0;
*/
}
}
}