Удалить один из цветов (пикселей) с картинки - C#
Формулировка задачи:
теперь возник другой вопрос.....как мне удалить один из цветов или один из пикселей с картинки, которая содержится в picturebox2......смотрел пример на msdn, там есть что-то подобное, но только он удаляет с формы(то есть картинка загружена на форму), а мне надо из picturebox2(в нем я содержу картинку)
вот пример кода из msdn:
то есть мне надо, чтобы я кликнул по кнопочке, произошла обработка изображения и срзу же вывелся результат в picturebox...и как это сделать?
Листинг программы
- // The .NET Compact Framework supports transparency,
- // but with only one transparency color.
- // The SetColorKey method must have the same color
- // specified for the low color and high color range.
- // Note that you must uncomment lines of code
- // as indicated for the desired transparency effect.
- protected override void OnPaint(PaintEventArgs e)
- {
- // Create a red and black bitmap to demonstrate transparency.
- Bitmap bmp = new Bitmap(75,75);
- Graphics g = Graphics.FromImage(bmp);
- g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);
- g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);
- g.DrawLine(new Pen(Color.Black), bmp.Width, 0, 0, bmp.Width);
- g.Dispose();
- ImageAttributes attr = new ImageAttributes();
- // Set the transparency color key based on the upper-left pixel
- // of the image.
- // Uncomment the following line to make all black pixels transparent:
- // attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));
- // Set the transparency color key based on a specified value.
- // Uncomment the following line to make all red pixels transparent:
- // attr.SetColorKey(Color.Red, Color.Red);
- // Draw the image using the image attributes.
- Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
- e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
- GraphicsUnit.Pixel, attr);
- }
Решение задачи: «Удалить один из цветов (пикселей) с картинки»
textual
Листинг программы
- using System.Drawing.Imaging;
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- this.TopMost = true;
- this.pictureBox2.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox2_Paint);
- this.button1.Click += new System.EventHandler(this.button1_Click);
- }
- ImageAttributes attr = new ImageAttributes();
- Bitmap bmp = new Bitmap(75, 75);
- private void pictureBox2_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = Graphics.FromImage(bmp);
- g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);
- g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);
- g.DrawLine(new Pen(Color.Aqua), bmp.Width, 0, 0, bmp.Width);
- g.Dispose();
- // Draw the image using the image attributes.
- Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
- e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
- GraphicsUnit.Pixel, attr);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // Set the transparency color key based on the upper-left pixel
- // of the image.
- // Uncomment the following line to make all black pixels transparent:
- attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));
- // Set the transparency color key based on a specified value.
- // Uncomment the following line to make all red pixels transparent:
- attr.SetColorKey(Color.Red, Color.Red);
- Hide();
- Show();
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д