如何更改图像中坐标为 x 和 y 的像素的颜色?
How can I change the color of a pixel with coordinates x and y in the image?
enter image description here
如何更改图像中坐标为 x 和 y 的像素的颜色?
如何仅使用我拥有的图像的坐标 (x, y) 更改像素的颜色? (只有 1 个像素)。
private static bool boyalganmi(int x, int y)
{
//ushbu nuqtalar 2480x3508 o'lchamdagi list uchun
(int, int)[] nuqta = new[] { (x, y), (x, y - 18), (x + 13, y - 13), (x + 18, y),
(x + 13, y + 13), (x, y + 18), (x - 13, y + 13), (x - 18, y), (x - 13, y - 13) };
int oq = 0, qora = 0, boshqa = 0;
foreach ((int, int) nuqtaItem in nuqta)
{
var imageXY = Image.GetPixel(nuqtaItem.Item1%2480, nuqtaItem.Item2%3508);
if (imageXY.R > 225 && imageXY.G > 225 && imageXY.B > 225)
{
oq++;
Color green = Color.FromArgb(255, 0, 128, 0);
Image.SetPixel(nuqtaItem.Item1, nuqtaItem.Item2, Color.Green);
}
else if (imageXY.R < 10 && imageXY.G < 10 && imageXY.B < 10)
{
qora++;
Color red = Color.FromArgb(255, 225, 0, 0);
Image.SetPixel(nuqtaItem.Item1, nuqtaItem.Item2, Color.Red);
}
else { boshqa++; MessageBox.Show($"RGB({imageXY.R},{imageXY.G},{imageXY.B})"); }
}
//MessageBox.Show($"oq={oq}, qora={qora}, boshqa={boshqa}");
return oq > qora;
}
您需要将图像更改为位图。您可以在进行更改后再次将其作为图片拍摄。让我们看看我的代码。效果很好)
源代码:https://github.com/Nodirbek-Abdulaxadov/SetPixelColor
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ColorPixel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog ofd = new OpenFileDialog();
Color clr;
private void button1_Click(object sender, EventArgs e)
{
try
{
ofd.Filter = "jpg files(*.jpg)|*.jpg|png files(*.png)|*.png|jpeg files(*.jpeg)|*.jpeg|All Files(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = ofd.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
clr = colorDialog1.Color;
}
private void button3_Click(object sender, EventArgs e)
{
if (ofd.FileName is not null)
{
Bitmap image = new Bitmap(ofd.FileName);
Random random = new Random();
for (int i = 0; i < 50000; i++)
{
int X = random.Next(image.Width);
int Y = random.Next(image.Height);
image.SetPixel(X, Y, clr);
}
pictureBox2.Image = image;
}
}
}
}
enter image description here
如何更改图像中坐标为 x 和 y 的像素的颜色?
如何仅使用我拥有的图像的坐标 (x, y) 更改像素的颜色? (只有 1 个像素)。
private static bool boyalganmi(int x, int y)
{
//ushbu nuqtalar 2480x3508 o'lchamdagi list uchun
(int, int)[] nuqta = new[] { (x, y), (x, y - 18), (x + 13, y - 13), (x + 18, y),
(x + 13, y + 13), (x, y + 18), (x - 13, y + 13), (x - 18, y), (x - 13, y - 13) };
int oq = 0, qora = 0, boshqa = 0;
foreach ((int, int) nuqtaItem in nuqta)
{
var imageXY = Image.GetPixel(nuqtaItem.Item1%2480, nuqtaItem.Item2%3508);
if (imageXY.R > 225 && imageXY.G > 225 && imageXY.B > 225)
{
oq++;
Color green = Color.FromArgb(255, 0, 128, 0);
Image.SetPixel(nuqtaItem.Item1, nuqtaItem.Item2, Color.Green);
}
else if (imageXY.R < 10 && imageXY.G < 10 && imageXY.B < 10)
{
qora++;
Color red = Color.FromArgb(255, 225, 0, 0);
Image.SetPixel(nuqtaItem.Item1, nuqtaItem.Item2, Color.Red);
}
else { boshqa++; MessageBox.Show($"RGB({imageXY.R},{imageXY.G},{imageXY.B})"); }
}
//MessageBox.Show($"oq={oq}, qora={qora}, boshqa={boshqa}");
return oq > qora;
}
您需要将图像更改为位图。您可以在进行更改后再次将其作为图片拍摄。让我们看看我的代码。效果很好) 源代码:https://github.com/Nodirbek-Abdulaxadov/SetPixelColor
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ColorPixel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog ofd = new OpenFileDialog();
Color clr;
private void button1_Click(object sender, EventArgs e)
{
try
{
ofd.Filter = "jpg files(*.jpg)|*.jpg|png files(*.png)|*.png|jpeg files(*.jpeg)|*.jpeg|All Files(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = ofd.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
clr = colorDialog1.Color;
}
private void button3_Click(object sender, EventArgs e)
{
if (ofd.FileName is not null)
{
Bitmap image = new Bitmap(ofd.FileName);
Random random = new Random();
for (int i = 0; i < 50000; i++)
{
int X = random.Next(image.Width);
int Y = random.Next(image.Height);
image.SetPixel(X, Y, clr);
}
pictureBox2.Image = image;
}
}
}
}