private void button1_Click(object sender, EventArgs e)
{
UpdatePicBoxEventHandle UpdatePicBox = new UpdatePicBoxEventHandle(SetContrast);
Image img = pictureBox1.Image;
for (double i = -255; i < 255; i++)
{
UpdatePicBox.BeginInvoke(img.Clone() as Bitmap, i, new AsyncCallback(UpdatePicBoxCallBack), UpdatePicBox);
}
}
delegate Image UpdatePicBoxEventHandle(Image img, double contrast);
public void UpdatePicBoxCallBack(IAsyncResult result)
{
if (result.IsCompleted)
{
UpdatePicBoxEventHandle updatepicbox = result.AsyncState as UpdatePicBoxEventHandle;
using (Image img = updatepicbox.EndInvoke(result) as Image)
{
//img.Save(Guid.NewGuid().ToString() + ".jpg");
pictureBox2.Image = new Bitmap(img);
}
}
}
//设置对比度
public Image SetContrast(Image oImg, double contrast)
{
using (Bitmap tImg = oImg as Bitmap)
{
if (contrast < -255) contrast = -255;
if (contrast > 255) contrast = 255;
contrast = (255.0 + contrast) / 255.0;//对比度值
contrast *= contrast;
Color c;
//遍历图像中的像素
for (int i = 0; i < tImg.Width; i++)
for (int j = 0; j < tImg.Height; j++)
{
//获取像素点的颜色信息
c = tImg.GetPixel(i, j);
//红色分量值
double R = c.R / 255.0;
R -= 0.5;
R *= contrast;
R += 0.5;
R *= 255;
if (R < 0) R = 0;
if (R > 255) R = 255;
//绿色分量值
double G = c.G / 255.0;
G -= 0.5;
G *= contrast;
G += 0.5;
G *= 255;
if (G < 0) G = 0;
if (G > 255) G = 255;
//蓝色分量值
double B = c.B / 255.0;
B -= 0.5;
B *= contrast;
B += 0.5;
B *= 255;
if (B < 0) B = 0;
if (B > 255) B = 255;
//重新设置像素点的颜色
tImg.SetPixel(i, j, Color.FromArgb((byte)R, (byte)G, (byte)B));
}
return tImg.Clone() as Image;
}
}
