2015年12月31日 星期四

C# - 影像處理圖片像素 RGB 值

練習:
  • 使用 getPixel() 方法來截取圖片每點像素,進一步得到該點 RGB 值。
  • 使用 setPixel () 方法來重設圖片。
  • 結合 for-loop。
  • 練習一、將圖片一半斜角變成黑色。
 
  • 練習二、指定一座標點(單位 pixel),將該點變成黑色。
##ReadMore##

練習一、
Step1. 檔案 → 新增專案 → Windows Form 應用程式 → 拉物件 Form1.cs[設計] → 撰寫 Form1.cs

Step2. 使用圖檔 lena.png 

Step3. 方案總管 → 參考 → 右鍵加入參考 → 瀏灠(預設路徑) C:\Program Files (x86)\AForge.NET\Framework\Release → 加入參考
  • AForge.Imaging.dll
  • AForge.Imaging.Formats.dll

Step4. Windows Form 拉入需要工具:
  • pictureBox × 2
  • button × 4
  • openFileDialog × 1 
  • saveFileDialog × 1

Step5. Coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AForge.Imaging;
using AForge.Imaging.Formats;

namespace Lesson_p49
{
    public partial class Form1 : Form
    {
        Bitmap imgone;
        int[,] maR = new int[1024, 1024];
        int[,] maG = new int[1024, 1024];
        int[,] maB = new int[1024, 1024];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "所有檔案(*.*)|*.*"; 
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imgone = ImageDecoder.DecodeFromFile(openFileDialog1.FileName);
                
                pictureBox1.Image = imgone;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < imgone.Width; i++)
            {
                for (int j = 1; j < imgone.Height; j++)
                {
                    Color pixelColor = imgone.GetPixel(i, j); 
                    maR[i, j] = pixelColor.R;
                    maG[i, j] = pixelColor.G;
                    maB[i, j] = pixelColor.B;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < imgone.Width; i++)
            {
                for (int j = 1; j < imgone.Height; j++)
                {
                    if (i < j)
                    {
                        maR[i, j] = 0; 
                        maG[i, j] = 0; 
                        maB[i, j] = 0;
                    }
                    Color pixelColor = Color.FromArgb(maR[i, j], maG[i, j], maB[i, j]);
                    imgone.SetPixel(i, j, pixelColor);
                }
            }
            pictureBox2.Image = imgone; 
        }

        private void button4_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = @"imgone.bmp"; 
            saveFileDialog1.Filter = "Bitmap file (*.bmp)|*.bmp"; 
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imgone.Save(saveFileDialog1.FileName);
            } 
        }
    }
}

---
練習二、
Step1. 檔案 → 新增專案 → Windows Form 應用程式 → 拉物件 Form1.cs[設計] → 撰寫 Form1.cs

Step2. 方案總管 → 參考 → 右鍵加入參考 → 瀏灠(預設路徑) C:\Program Files (x86)\AForge.NET\Framework\Release → 加入參考
  • AForge.Imaging.dll
  • AForge.Imaging.Formats.dll
Step3. 編輯 Windows Form
  • pictureBox × 2
  • button × 3
  • openFileDialog × 1 
  • saveFileDialog × 1
  • Label × 2
  • textBox × 2

Step4. coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AForge.Imaging.Formats;
using AForge.Imaging;


namespace RGBshow
{
    public partial class Form1 : Form
    {
        Bitmap imgInput, imgone, imgtwo;
        int[,] maR = new int[1024, 1024];
        int[,] maG = new int[1024, 1024]; 
        int[,] maB = new int[1024, 1024];
        
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "所有檔案(*.*)|*.*";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imgone = ImageDecoder.DecodeFromFile(openFileDialog1.FileName);
                imgtwo = imgone;
                pictureBox1.Image = imgone;
                pictureBox2.Image = imgtwo;
                label4.Text = string.Format("({0} × {1})", imgone.Width, imgone.Height);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = @"imgtwo.bmp";
            saveFileDialog1.Filter = "Bitmap file (*.bmp)|*.bmp";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                imgtwo.Save(saveFileDialog1.FileName);
            } 
        }

      

        private void button2_Click(object sender, EventArgs e)
        {
            //Get textBox input value
            int x = int.Parse(textBox1.Text);
            int y = int.Parse(textBox2.Text);
                        
            //保存原圖像素顏色            
            for (int i = 1; i < imgone.Width; i++)
            {
                for (int j = 1; j < imgone.Height; j++)
                {
                    Color pixelColor = imgone.GetPixel(i, j);
                    maR[i, j] = pixelColor.R;
                    maG[i, j] = pixelColor.G;
                    maB[i, j] = pixelColor.B;
                }
            }
           
            
            //繪製單點座標(x, y)
            maR[x, y] = 0;
            maG[x, y] = 0;
            maB[x, y] = 0;

            for (int i = 1; i < imgone.Width; i++)
            {
                for (int j = 1; j < imgone.Height; j++)
                {
                    Color pixelColor = Color.FromArgb(maR[i, j], maG[i, j], maB[i, j]);
                    imgtwo.SetPixel(i, j, pixelColor);
                }
            }
            pictureBox2.Image = imgtwo; 
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

     
      
    }
}

沒有留言:

張貼留言