Другие языки программирования и технологии

Помогите с C# Forms. Как изображению установить красный цвет?

Т. е. задание такое: разбить изображение на 4 части. Одной части установить градации серого цвета, а трём другим значение канала Red, Green или Blue. Равные части сделал, а вот как установить цвета непонятно.
Зомби И Мир
Зомби И Мир
314
public partial class Form1 : Form
{
private List < ColorMatrix > matrixList = new List < ColorMatrix > ()
{
new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
}),
new ColorMatrix(
new float[][]
{
new float[] {1, 0, 0, 0, 0},
new float[] {.5f, .5f, 0, 0, 0},
new float[] {.5f, 0, .5f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
}),
new ColorMatrix(
new float[][]
{
new float[] {.5f, .5f, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, .5f, .5f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
}),
new ColorMatrix(
new float[][]
{
new float[] {.5f, 0, .5f, 0, 0},
new float[] {0, .5f, .5f, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
})
};

public Form1()
{
InitializeComponent();
}

private void BApplyModifications_Click(object sender, EventArgs e)
{
if (DialogResult.OK == ofdChooseImage.ShowDialog(this))
{
ProcessImage(ofdChooseImage.FileName);
}
}

private void ProcessImage(string path)
{
Bitmap sourceBitmap = new Bitmap(path);
List < Rectangle > imageParts = new List()
{
new Rectangle(0, 0, sourceBitmap.Width / 2, sourceBitmap.Height / 2),
new Rectangle(sourceBitmap.Width / 2, 0, sourceBitmap.Width / 2, sourceBitmap.Height / 2),
new Rectangle(0, sourceBitmap.Height / 2, sourceBitmap.Width / 2, sourceBitmap.Height / 2),
new Rectangle(sourceBitmap.Width / 2, sourceBitmap.Height / 2, sourceBitmap.Width / 2, sourceBitmap.Height / 2)
};

for (int i = 0; i < 4; i++)
{
ModifyImagePart(sourceBitmap, imageParts.ElementAt(i), matrixList.ElementAt(i), Path.Combine(Path.GetDirectoryName(path), $"output{i + 1}.jpg"));
}
}

private void ModifyImagePart(Bitmap sourceBitmap, Rectangle rect, ColorMatrix colorMatrix, string path)
{
Bitmap targetBitmap = new Bitmap(sourceBitmap.Width / 2, sourceBitmap.Height / 2);
Graphics g = Graphics.FromImage(targetBitmap);

ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(sourceBitmap, new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), rect.X, rect.Y, rect.Width, rect.Height, GraphicsUnit.Pixel, attributes);

g.Dispose();

targetBitmap.Save(path);
}
}
Иван Осипов
Иван Осипов
5 002
Лучший ответ