zoukankan      html  css  js  c++  java
  • 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");
    }
  • 相关阅读:
    boost::asio在VS2008下的编译错误
    Java集合框架——接口
    ACM POJ 3981 字符串替换(简单题)
    ACM HDU 1042 N!(高精度计算阶乘)
    OneTwoThree (Uva)
    ACM POJ 3979 分数加减法(水题)
    ACM HDU 4004 The Frog's Games(2011ACM大连赛区第四题)
    Hexadecimal View (2011ACM亚洲大连赛区现场赛D题)
    ACM HDU 4002 Find the maximum(2011年大连赛区网络赛第二题)
    ACM HDU 4001 To Miss Our Children Time (2011ACM大连赛区网络赛)
  • 原文地址:https://www.cnblogs.com/wxjing67/p/3173861.html
Copyright © 2011-2022 走看看