【问题标题】:How to adjust Brightness and Contrast of an image using a slider?如何使用滑块调整图像的亮度和对比度?
【发布时间】:2023-04-07 02:50:01
【问题描述】:

我有一张图片,我想通过 xaml 中的滑块调整其亮度和对比度。 我不知道从哪里开始。任何帮助将不胜感激。 我正在尝试实现这个

public  WriteableBitmap ChangeBrightness(WriteableBitmap inputBitmap, double brightnessValue )
{
    double Value = brightnessValue;
    var inputPixels = inputBitmap.Pixels;
    for (int i = 0; i < inputPixels.Length; i++)
    {
        // Extract color components
        var c = inputBitmap.Pixels[i];
        var a = (byte)(c >> 24);
        var r = (byte)(c >> 16);
        var g = (byte)(c >> 8);
        var b = (byte)(c);

        int ri = r + (int)Value;
        int gi = g + (int)Value;
        int bi = b + (int)Value;

        // Clamp to byte boundaries
        r = (byte)(ri > 255 ? 255 : (ri < 0 ? 0 : ri));
        g = (byte)(gi > 255 ? 255 : (gi < 0 ? 0 : gi));
        b = (byte)(bi > 255 ? 255 : (bi < 0 ? 0 : bi));
        inputBitmap.Pixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
    }
    return inputBitmap;          
}

但我猜它没有用,我没有找到任何有用的文章。

【问题讨论】:

  • 首先,展示您尝试过的内容。

标签: c# xaml windows-store-apps winrt-xaml


【解决方案1】:

Formula For Adjusting Brightness

使用添加公式,您的程序中缺少 2 个部分。

第 1 部分:创建新的可写位图而不是修改输入位图(返回此新位图)
第 2 部分:您必须保存位图的原始副本(亮度不可逆,如上面的链接所述)


一个非常缓慢的实现让你开始。如果你只是花点时间阅读,大部分这些东西都可以在 MSDN 上找到(如果你想让程序比 Hello World 更先进,这是你需要养成的习惯)。

<StackPanel>
    <Image x:Name="myImage" Stretch="None" Source="Assets/bling.png"/>
    <Slider x:Name="mySilder" ValueChanged="mySilder_ValueChanged"></Slider>
</StackPanel>

// namespaces
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;

// our copy of the image
WriteableBitmap copy;

public MainPage()
{
    this.InitializeComponent();
}

private async void Page_Loaded(object sender, RoutedEventArgs e)
{

    // storage file for the image (load it)
    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/bling.png"));

    // create a bitmap image
    BitmapImage bi = new BitmapImage();

    using (
        // Open a stream for the selected file.
    Windows.Storage.Streams.IRandomAccessStream fileStream =
        await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
    {
        // load the image into the BitmapImage
        await bi.SetSourceAsync(fileStream);

        // create a copy of the image            
        copy = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);

        // load the image into writeablebitmap copy
        fileStream.Seek(0);
        await copy.SetSourceAsync(fileStream);
    }
}

private WriteableBitmap ChangeBrightness(WriteableBitmap source, byte change_value)
{
    WriteableBitmap dest = new WriteableBitmap(source.PixelWidth, source.PixelHeight);        

    byte[] color = new byte[4];

    using (Stream s = source.PixelBuffer.AsStream())
    {
        using (Stream d = dest.PixelBuffer.AsStream())
        {
            // read the pixel color
            while (s.Read(color, 0, 4) > 0)
            {
                // color[0] = b
                // color[1] = g 
                // color[2] = r
                // color[3] = a

                // do the adding algo per byte (skip the alpha)
                for (int i = 0; i < 4; i++)
                {
                    if ((int)color[i] + change_value > 255) color[i] = 255; else color[i] = (byte)(color[i] + change_value);
                }

                // write the new pixel color
                d.Write(color, 0, 4);                        
            }
        }
    }

    // return the new bitmap
    return dest;
}

private void mySilder_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    if (this.mySilder != null)
    {
        // deterime the brightness to add
        byte value_to_add = (byte)((this.mySilder.Value / this.mySilder.Maximum) * 255);

        // get the new bitmap with the new brightness
        WriteableBitmap ret = ChangeBrightness(copy, value_to_add);

        // set it as the source so we can see the change
        this.myImage.Source = ret;
    }
}

代码在行动


如果您正在寻找“bling.png”,可以在这里找到:bling.png

【讨论】:

  • 你能告诉我如何实现它,因为我对此非常陌生。
  • @PranavMahajan 我认为问题在于您正在尝试将 Windows Phone 代码实现到 Window Store 应用程序中。即使它们是相似的东西,它也不会将 1 转换为 1。例如,没有 .Pixel (byte[]) 这样的东西,有一个 .PixelBuffer,它是一个 IBuffer。
  • 我在这一行出现异常 WriteableBitmap dest = new WriteableBitmap(source.PixelWidth, source.PixelHeight);当我尝试增加滑块时
  • @PranavMahajan 确保 bling.png(图像)实际上位于 Assets 文件夹中。如果这不起作用,那么异常的实际错误是什么,只是说你有一个并不意味着帮助。
  • @PranavMahajan 您还需要确保 private async void Page_Loaded 实际上已连接到页面事件。
【解决方案2】:

顺便说一句,增加预乘图像亮度的正确函数是

    public static WriteableBitmap ChangeBrightness(WriteableBitmap source, int increment)
    {
        var dest = new WriteableBitmap(source.PixelWidth, source.PixelHeight);

        byte[] color = new byte[4];

        using (var srcBuffer = source.PixelBuffer.AsStream())
        using (var dstBuffer = dest.PixelBuffer.AsStream())
        {
            while (srcBuffer.Read(color, 0, 4) > 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    var value = (float)color[i];
                    var alpha = color[3] / (float)255;
                    value /= alpha;
                    value += increment;
                    value *= alpha;

                    if (value > 255)
                    {
                        value = 255;
                    }

                    color[i] = (byte)value;
                }

                dstBuffer.Write(color, 0, 4);
            }
        }

        return dest;
    }

【讨论】:

  • 谢谢你的这个sn-p!为了使图像变暗,我需要修改什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
相关资源
最近更新 更多