C# 读取图像

编程入门 行业动态 更新时间:2024-10-04 21:26:56

C# 读取<a href=https://www.elefans.com/category/jswz/34/1771430.html style=图像"/>

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# 读取图像

本文发布于:2024-03-23 17:32:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1740906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!