Изменение яркости изображения - C# (191754)
Формулировка задачи:
Хочу написать приложение, которое меняет яркость. Но получается лишь следующее, проблема в том, что я не могу понять как менять каждый пиксель
Помогите, пожалуйста, либо наведите на мысль, как это сделать
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace D
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonLoad_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
picture1.Load(openFileDialog1.FileName);
}
private void buttonPer_Click(object sender, EventArgs e)
{
var bitmapStart = (Bitmap)picture1.Image;
var bitmapBase = new Bitmap(picture1.Image.Width, picture1.Image.Height);
picture2.Image = bitmapBase;
var countPixel = bitmapStart.Width * bitmapStart.Height;
var index = 0;
for (var x = 0; x < picture1.Image.Width; x++)
for (var y = 0; y < picture1.Image.Height; y++)
{
var colorStart = bitmapStart.GetPixel(x, y);
byte[] data = new[] {colorStart.R, colorStart.G, colorStart.B};
byte[] result = Class1.CalculateBrightness(data);
var colorBase = Color.FromArgb(result[0], result[1], result[2]);
bitmapBase.SetPixel(x, y, colorBase);
progressBar1.Value = Convert.ToInt32(100*((double) index++/countPixel));
}
picture2.Update();
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace D
{
class Class1
{
public static byte[] CalculateBrightness(byte[] rgb)
{
var resultBrightness = new double[widthMatrixBrightness];
for (var x = 0; x < heightMatrixBrightness; x++)
for (var y = 0; y < widthMatrixBrightness; y++)
resultBrightness[x] += matrixBrightness[x][y] * rgb[y];
return new[]
{
Convert.ToByte(Math.Round(resultBrightness[0], 0)),
Convert.ToByte(Math.Round(resultBrightness[1], 0) + 128),
Convert.ToByte(Math.Round(resultBrightness[2], 0) + 128)
};
}
private static readonly byte widthMatrixBrightness = 3;
private static readonly byte heightMatrixBrightness = 3;
private static readonly double[][] matrixBrightness = new[] { new[] {.299, .587, 0.114},
new[] {-.168736, -.331264, .5},
new[] {.5, -.418688, -.081312}};
}
}Решение задачи: «Изменение яркости изображения»
textual
Листинг программы
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Windows.Forms;
namespace WindowsFormsApplication290
{
public partial class Form1 : Form
{
private Bitmap source;
private TrackBar tb;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
source = (Bitmap)Image.FromStream(new WebClient().OpenRead("http://img-fotki.yandex.ru/get/5114/36014149.81/0_6cbc9_f3de6b6_XL"));
source.SetResolution(96, 96);
tb = new TrackBar(){Parent = this, Value = 5};
tb.ValueChanged += delegate { Invalidate(); };
}
private Bitmap ChangeBrightness(Bitmap img, float k)
{
var imageAttributes = new ImageAttributes();
float[][] colorMatrixElements = {
new float[] {k, 0, 0, 0, 0},
new float[] {0, k, 0, 0, 0},
new float[] {0, 0, k, 0, 0},
new float[] {0, 0, 0, 1f, 0},
new float[] {0f, 0f, 0f, 0, 1f}};
var colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
var result = new Bitmap(img.Width, img.Height);
using (var gr = Graphics.FromImage(result))
gr.DrawImage(img,
new Rectangle(0, 0, img.Width, img.Height),
0, 0,
img.Width,
img.Height,
GraphicsUnit.Pixel,
imageAttributes);
return result;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawImage(source, 0, 0);
var brightness = (tb.Value - 5f) / 5f;
if (brightness <= 0)
brightness = 1f / (-brightness + 1);
else
brightness = 1 + brightness;
var res = ChangeBrightness(source, brightness);
e.Graphics.DrawImage(res, source.Width + 3, 0);
res.Dispose();
}
}
}