图像"/>
C# 读取图像
改色方案一:
private void button1_Click(object sender, EventArgs e)
{try{// Retrieve the image.【读取图像】Bitmap image1 = new Bitmap(@"输入图像文件", true); //"true" to use color correction for this Bitmap; otherwise, "false".pictureBox1.Image = image1;pictureBox1.Refresh(); //更新显示内容int x, y;// Loop through the images pixels to reset color.【改色、调色】for (x = 0; x < 200; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(pixelColor.R, 0, 0); //纯红image1.SetPixel(x, y, newColor);}}for (x = 200; x < 400; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(0, pixelColor.G, 0); //纯绿image1.SetPixel(x, y, newColor);}}for (x = 400; x < 600; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(0, 0, pixelColor.B); //纯蓝image1.SetPixel(x, y, newColor);}}for (x = 600; x < 800; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(pixelColor.B, 0, 0); //蓝转红image1.SetPixel(x, y, newColor);}}Random rand = new Random();int test = rand.Next(0, 255);for (x = 800; x < image1.Width; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255)); //随机色(noise)image1.SetPixel(x, y, newColor);}}// Set the PictureBox to display the image. 【输出】pictureBox1.Image = image1;pictureBox1.Refresh();image1.Save(@"输出图像文件.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);// Display the pixel format in Label1.MessageBox.Show("Pixel format: " + image1.PixelFormat.ToString());}catch (ArgumentException){MessageBox.Show("There was an error." +"Check the path to the image file.");}
}
改色方案二:
private void button1_Click(object sender, EventArgs e)
{try{// Retrieve the image.【读取图像】Bitmap image1 = new Bitmap(@"输入图像文件", true); //"true" to use color correction for this Bitmap; otherwise, "false".pictureBox1.Image = image1;pictureBox1.Refresh(); //更新显示内容int x, y;// Loop through the images pixels to reset color.【改色、调色】for (x = 0; x < 200; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(pixelColor.R, pixelColor.G, 0); //红绿image1.SetPixel(x, y, newColor);}}for (x = 200; x < 400; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(pixelColor.R, 0, pixelColor.B); //红蓝image1.SetPixel(x, y, newColor);}}for (x = 400; x < 600; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(0, pixelColor.G, pixelColor.B); //绿蓝image1.SetPixel(x, y, newColor);}}for (x = 600; x < 800; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B); //红绿蓝image1.SetPixel(x, y, newColor);}}Random rand = new Random();int test = rand.Next(0, 255);for (x = 800; x < image1.Width; x++){for (y = 0; y < image1.Height; y++){Color pixelColor = image1.GetPixel(x, y);Color newColor = Color.FromArgb(0, pixelColor.B, 0); //蓝转绿image1.SetPixel(x, y, newColor);}}// Set the PictureBox to display the image. 【输出】pictureBox1.Image = image1;pictureBox1.Refresh();image1.Save(@"输出图像文件.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);// Display the pixel format in Label1.MessageBox.Show("Pixel format: " + image1.PixelFormat.ToString());}catch (ArgumentException){MessageBox.Show("There was an error." +"Check the path to the image file.");}
}
示例
输入图片:
改色方案一:
改色方案二:
更多推荐
C# 读取图像
发布评论