Удалить один из цветов (пикселей) с картинки - C#

Узнай цену своей работы

Формулировка задачи:

теперь возник другой вопрос.....как мне удалить один из цветов или один из пикселей с картинки, которая содержится в picturebox2......смотрел пример на msdn, там есть что-то подобное, но только он удаляет с формы(то есть картинка загружена на форму), а мне надо из picturebox2(в нем я содержу картинку) вот пример кода из msdn:
Листинг программы
  1. // The .NET Compact Framework supports transparency,
  2. // but with only one transparency color.
  3. // The SetColorKey method must have the same color
  4. // specified for the low color and high color range.
  5. // Note that you must uncomment lines of code
  6. // as indicated for the desired transparency effect.
  7. protected override void OnPaint(PaintEventArgs e)
  8. {
  9. // Create a red and black bitmap to demonstrate transparency.
  10. Bitmap bmp = new Bitmap(75,75);
  11. Graphics g = Graphics.FromImage(bmp);
  12. g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);
  13. g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);
  14. g.DrawLine(new Pen(Color.Black), bmp.Width, 0, 0, bmp.Width);
  15. g.Dispose();
  16. ImageAttributes attr = new ImageAttributes();
  17. // Set the transparency color key based on the upper-left pixel
  18. // of the image.
  19. // Uncomment the following line to make all black pixels transparent:
  20. // attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));
  21. // Set the transparency color key based on a specified value.
  22. // Uncomment the following line to make all red pixels transparent:
  23. // attr.SetColorKey(Color.Red, Color.Red);
  24. // Draw the image using the image attributes.
  25. Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  26. e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
  27. GraphicsUnit.Pixel, attr);
  28. }
то есть мне надо, чтобы я кликнул по кнопочке, произошла обработка изображения и срзу же вывелся результат в picturebox...и как это сделать?

Решение задачи: «Удалить один из цветов (пикселей) с картинки»

textual
Листинг программы
  1.  using System.Drawing.Imaging;
  2.     public partial class Form1 : Form
  3.     {
  4.         public Form1()
  5.         {
  6.             InitializeComponent();
  7.             this.TopMost = true;
  8.             this.pictureBox2.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox2_Paint);
  9.             this.button1.Click += new System.EventHandler(this.button1_Click);
  10.         }
  11.  
  12.         ImageAttributes attr = new ImageAttributes();
  13.         Bitmap bmp = new Bitmap(75, 75);
  14.  
  15.         private void pictureBox2_Paint(object sender, PaintEventArgs e)    
  16.         {
  17.                        
  18.             Graphics g = Graphics.FromImage(bmp);        
  19.        
  20.  
  21.             g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);
  22.             g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);
  23.             g.DrawLine(new Pen(Color.Aqua), bmp.Width, 0, 0, bmp.Width);
  24.             g.Dispose();        
  25.  
  26.            
  27.  
  28.             // Draw the image using the image attributes.
  29.             Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  30.             e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
  31.                 GraphicsUnit.Pixel, attr);
  32.         }
  33.  
  34.         private void button1_Click(object sender, EventArgs e)
  35.         {
  36.             // Set the transparency color key based on the upper-left pixel
  37.             // of the image.
  38.             // Uncomment the following line to make all black pixels transparent:
  39.             attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));
  40.  
  41.             // Set the transparency color key based on a specified value.
  42.             // Uncomment the following line to make all red pixels transparent:
  43.             attr.SetColorKey(Color.Red, Color.Red);
  44.             Hide();
  45.             Show();
  46.         }
  47.  
  48.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 3.857 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы