2016年6月2日 星期四

C#-examination-1

題目要求:
  1. 做 button1 → 讀圖。
  2. 做 button2 → 從左上角劃一綠線,至右下角。
  3. 做 button3 → 中間部位畫一50X150之紅色四邊形。
  4. 做 button4 → 存檔。

##ReadMore##

測試圖檔:https://sites.google.com/site/p501labsite/microsoft-virtual-studio/lena.png?attredirects=0&d=1


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.Filters;
using AForge.Imaging.Formats;


namespace examination_1
{
    public partial class exam1 : Form
    {
        //statement
        Bitmap img_L, img_R;
        Color colorPixel;
        int[,] maR = new int[1024, 1024];
        int[,] maG = new int[1024, 1024];
        int[,] maB = new int[1024, 1024];

        public exam1()
        {
            InitializeComponent();
        }

        //Read File
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog readImg = new OpenFileDialog();
            if (readImg.ShowDialog() == DialogResult.OK)
            {
                img_L = new Bitmap(readImg.FileName);
                img_R = img_L;
                pictureBox1.Image = img_L;
                pictureBox2.Image = img_L;

                // 儲存原圖 colorPixel 
                for (int i = 0; i < img_L.Width; i++)
                {
                    for (int j = 0; j < img_L.Height; j++)
                    {
                        colorPixel = img_L.GetPixel(i, j);
                        maR[i, j] = colorPixel.R;
                        maG[i, j] = colorPixel.G;
                        maB[i, j] = colorPixel.B;
                    }
                }
            }
        }

        //Diagonal Top-left to Bottom-right
        private void button2_Click(object sender, EventArgs e)
        {
            // 對角線之 colorPixel
            for (int i = 0; i <= img_L.Height; i++)
            {
                maR[i, i] = 0;
                maG[i, i] = 0;
                maB[i, i] = 0;
            }

            // paint diagonal
            for (int i = 0; i < img_L.Width; i++)
            {
                for (int j = 0; j < img_L.Height; j++)
                {
                    Color diagonalPixel = Color.FromArgb(maR[i, j], maG[i, j], maB[i, j]);
                    img_R.SetPixel(i, j, diagonalPixel);
                }
            }
            pictureBox2.Image = img_R;

        }

        //Rectangle 50 x 150
        private void button3_Click(object sender, EventArgs e)
        {
            int drawWidth = 50;
            int drawHeight = 150;

            // 中間繪圖點
            int originX = img_L.Width / 2 - drawWidth / 2;
            int originY = img_L.Height / 2 - drawHeight / 2;

            for (int i = originX; i <= originX + drawWidth; i++)
            {
                for (int j = originY; j <= originY + drawHeight; j++)
                {
                    maR[i, j] = 255;
                    maG[i, j] = 0;
                    maB[i, j] = 0;
                }
            }

            // paint rectangle
            for (int i = 0; i < img_L.Width; i++)
            {
                for (int j = 0; j < img_L.Height; j++)
                {
                    Color rectanglePixel = Color.FromArgb(maR[i, j], maG[i, j], maB[i, j]);
                    img_R.SetPixel(i, j, rectanglePixel);
                }
            }
            pictureBox2.Image = img_R;
        }

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

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


沒有留言:

張貼留言