C#调整图片亮度和对比度

BitmapSource bitmap = null;
int degreeBrightness = 0;
int degreeContrast = 0;

private void SetBrightness(int degree)
{
degree = degree * 255 / 100;
WriteableBitmap wb = new WriteableBitmap(bitmap);
uint[] PixelData = new uint[wb.PixelWidth * wb.PixelHeight];
wb.CopyPixels(PixelData, 4 * wb.PixelWidth, 0);
for (uint y = 0; y < wb.PixelHeight; y++)
{
for (uint x = 0; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
int B = (int)dd[0] + degree;
int G = (int)dd[1] + degree;
int R = (int)dd[2] + degree;
if (B < 0) B = 0;
if (B > 255) B = 255;
if (G < 0) G = 0;
if (G > 255) G = 255;
if (R < 0) R = 0;
if (R > 255) R = 255;
dd[0] = (byte)B;
dd[1] = (byte)G;
dd[2] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, 0);
}
}
wb.WritePixels(new Int32Rect(0, 0, wb.PixelWidth, wb.PixelHeight), PixelData, 4 * wb.PixelWidth, 0);
//this.image.Source = wb;
}
private void SetContrast(int degree)
{
double contrast = (100.0 + degree) / 100.0;
WriteableBitmap wb = new WriteableBitmap(bitmap);
uint[] PixelData = new uint[wb.PixelWidth * wb.PixelHeight];
wb.CopyPixels(PixelData, 4 * wb.PixelWidth, 0);
for (uint y = 0; y < wb.PixelHeight; y++)
{
for (uint x = 0; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
double B = (((double)dd[0] / 255 - 0.5) * contrast + 0.5) * 255;
double G = (((double)dd[1] / 255 - 0.5) * contrast + 0.5) * 255;
double R = (((double)dd[2] / 255 - 0.5) * contrast + 0.5) * 255;
if (B < 0) B = 0;
if (B > 255) B = 255;
if (G < 0) G = 0;
if (G > 255) G = 255;
if (R < 0) R = 0;
if (R > 255) R = 255;
dd[0] = (byte)B;
dd[1] = (byte)G;
dd[2] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, 0);
}
}
wb.WritePixels(new Int32Rect(0, 0, wb.PixelWidth, wb.PixelHeight), PixelData, 4 * wb.PixelWidth, 0);
//this.image.Source = wb;
}
private void SetCaptureImageCurrent(int degreeContrast, int degreeBrightness)
{
if (bitmap == null)
{
return;
}
WriteableBitmap wb = new WriteableBitmap(bitmap);
uint[] PixelData = new uint[wb.PixelWidth * wb.PixelHeight];
wb.CopyPixels(PixelData, 4 * wb.PixelWidth, 0);

if (degreeBrightness != 0)
{
degreeBrightness = degreeBrightness * 255 / 100;
for (uint y = 0; y < wb.PixelHeight; y++)
{
for (uint x = 0; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
int B = (int)dd[0] + degreeBrightness;
int G = (int)dd[1] + degreeBrightness;
int R = (int)dd[2] + degreeBrightness;
if (B < 0) B = 0;
if (B > 255) B = 255;
if (G < 0) G = 0;
if (G > 255) G = 255;
if (R < 0) R = 0;
if (R > 255) R = 255;
dd[0] = (byte)B;
dd[1] = (byte)G;
dd[2] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, 0);
}
}
}

if (degreeContrast != 0)
{
double contrast = (100.0 + degreeContrast) / 100.0;
for (uint y = 0; y < wb.PixelHeight; y++)
{
for (uint x = 0; x < wb.PixelWidth; x++)
{
uint pixel = PixelData[y * wb.PixelWidth + x];
byte[] dd = BitConverter.GetBytes(pixel);
double B = (((double)dd[0] / 255 - 0.5) * contrast + 0.5) * 255;
double G = (((double)dd[1] / 255 - 0.5) * contrast + 0.5) * 255;
double R = (((double)dd[2] / 255 - 0.5) * contrast + 0.5) * 255;
if (B < 0) B = 0;
if (B > 255) B = 255;
if (G < 0) G = 0;
if (G > 255) G = 255;
if (R < 0) R = 0;
if (R > 255) R = 255;
dd[0] = (byte)B;
dd[1] = (byte)G;
dd[2] = (byte)R;
PixelData[y * wb.PixelWidth + x] = BitConverter.ToUInt32(dd, 0);
}
}
}

wb.WritePixels(new Int32Rect(0, 0, wb.PixelWidth, wb.PixelHeight), PixelData, 4 * wb.PixelWidth, 0);

PngBitmapEncoder pE = new PngBitmapEncoder();
pE.Frames.Add(BitmapFrame.Create(wb));
using (Stream stream = File.Create(SelectedImage))
{
pE.Save(stream);
}
Messenger.Default.Send(wb, "PatientImageView");
}

 

posted @ 2013-07-05 15:58  wxing67  阅读(2293)  评论(0编辑  收藏  举报