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");
    }
  • 相关阅读:
    为什么今天的L4无人驾驶无法到达终局(转)
    各种卷积类型Convolution
    关于快速、深入理解需求
    测试注意事项及工作标准
    测试工作指引
    测试验收工作指引
    Jmeter的json提取器使用
    高等数学:第一章 函数与极限
    vscode
    Python模块查找路径
  • 原文地址:https://www.cnblogs.com/wxjing67/p/3173861.html
Copyright © 2011-2022 走看看