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");
    }
  • 相关阅读:
    linux 防火墙相关
    .net安装windows服务和生产webservice
    linq小实例
    C# DataTable 总结
    document对象
    结对-四则运算-开发过程
    课后作业-阅读任务-阅读笔记-1
    课后作业-阅读任务-阅读提问-2
    课后作业-阅读任务-阅读提问-3
    团对-象棋游戏-开发环境搭建过程
  • 原文地址:https://www.cnblogs.com/wxjing67/p/3173861.html
Copyright © 2011-2022 走看看