private void FormGray_Load(object sender, EventArgs e) { // 灰階 try { pictureBox1.Image = pcxOrigin.pcxImg; pcxAfter = new ImgPcx(pcxOrigin); pcxAfter.Gray(); pictureBox2.Image = pcxAfter.pcxImg; // 顯示SNR值 (到小數後2位) textBox1.Text = pcxOrigin.GetSNR(pcxOrigin, pcxAfter).ToString("f2"); // 清除圖表資料 chart1.Series.Clear(); // 設定圖表的高度與圖片相同 chart1.Size = new Size(chart1.Width, pictureBox1.Image.Height); // 繪製histogram Bitmap origin = (Bitmap)pictureBox2.Image; Bitmap target = origin.Clone(new Rectangle(0, 0, origin.Width, origin.Height), PixelFormat.Format24bppRgb); int w = target.Width; int h = target.Height; double[] yValue1 = new double[256]; Array.Clear(yValue1, 0, yValue1.Length); for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { Color c = target.GetPixel(x, y); int r = c.R; int g = c.G; int b = c.B; ++yValue1[r]; ++yValue1[g]; ++yValue1[b]; } } // 將點個數轉換為頻率 int total = w * h * 3; for (int i = 0; i < 256; ++i) { yValue1[i] /= total; } // Range(start, cnt) string[] xValue = Enumerable.Range(0, 256).ToArray().Select(x => x.ToString()).ToArray(); // 設定x軸區間 chart1.ChartAreas["ChartArea1"].AxisX.Interval = 32; // Intensities - 強度 chart1.Series.Add("Intensities"); // 資料序列集合 chart1.Series["Intensities"].ChartType = SeriesChartType.Column; // 直方圖 chart1.Series["Intensities"].Points.DataBindXY(xValue, yValue1); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// 加入浮水印 private void button3_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "開啟圖檔"; openFileDialog.Filter = "pcx files (*.pcx)|*.pcx"; if (openFileDialog.ShowDialog() == DialogResult.OK && openFileDialog.FileName.Length > 0) { pcxMark = new ImgPcx(openFileDialog.FileName); // 浮水印轉灰階 pcxMark.Gray(); pictureBox1.Image = null; pictureBox2.Image = null; pictureBox3.Image = null; pictureBox4.Image = null; pictureBox5.Image = null; pictureBox6.Image = null; pictureBox7.Image = null; pictureBox8.Image = null; markFlag = true; } else { // 還原 pcxGray = new ImgPcx(pcxBackup); markFlag = false; pictureBox1.Image = null; pictureBox2.Image = null; pictureBox3.Image = null; pictureBox4.Image = null; pictureBox5.Image = null; pictureBox6.Image = null; pictureBox7.Image = null; pictureBox8.Image = null; pictureBox10.Image = null; label1.Text = ""; label2.Text = ""; // 隱藏SNR label3.Visible = false; textBox1.Visible = false; } }
// 讀檔 private void menuOpen_Click(object sender, EventArgs e) { try { // 清空顯示圖片與資訊 pictureBox1.Image = null; pictureBox2.Image = null; pictureBox3.Image = null; dataGridView1.Visible = false; labelXY.Text = ""; labelR.Text = ""; labelG.Text = ""; labelB.Text = ""; labelDim.Text = ""; chart1.Visible = false; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "開啟圖檔"; openFileDialog.Filter = "pcx files (*.pcx)|*.pcx"; if (openFileDialog.ShowDialog() == DialogResult.OK && openFileDialog.FileName.Length > 0) { pcxOrigin = new ImgPcx(openFileDialog.FileName); // test Save() //pcx.Save("Sample_save"); // 將載入的圖像放到pictureBox1 pictureBox1.Image = pcxOrigin.pcxImg; // 顯示圖像header資訊 string[,] info = pcxOrigin.GetInfo(); for (int i = 0; i < 16; ++i) { dataGridView1.Rows.Add(new object[] { info[i, 0], info[i, 1] }); } dataGridView1.Visible = true; // nPlanes = 3, 無調色盤 if (pcxOrigin.head.nPlanes == 1) { // 畫出調色盤 var colors = pcxOrigin.pcxImg.Palette.Entries; // 取得調色盤 int cntColors = colors.Count(); // 計算顏色總數 (ex: 256) int row = cntColors / 16; // (ex: 256 / 16 = 16) int column = cntColors / 16; // (ex: 256 / 16 = 16) Bitmap palette = new Bitmap(row * 15, column * 15); // 建立調色盤圖像 Graphics g = Graphics.FromImage(palette); // 繪圖介面 /* * @curX : 圖像x座標 * @curY : 圖像y座標 * @cnt : 計算此列已畫好的顏色數 (用來判斷何時換行) */ int curX = 0, curY = 0, cnt = 0; for (int i = 0; i < cntColors; ++i) { // 挑出顏色給筆刷 SolidBrush brush = new SolidBrush(Color.FromArgb(colors[i].A, colors[i].R, colors[i].G, colors[i].B)); // 畫矩形 g.FillRectangle(brush, curX, curY, 15, 15); ++cnt; if (cnt == row) // 換行繼續畫 { curX = 0; curY += 15; cnt = 0; } else // 右移繼續畫 { curX += 15; } } // 顯示調色盤 pictureBox3.Image = palette; } // 顯示圖像長寬 labelDim.Text = pcxOrigin.head.width.ToString() + " x " + pcxOrigin.head.height.ToString(); // 開檔後, 開放使用存檔與工具 menuSave.Enabled = true; menuTool.Enabled = true; menuFilter.Enabled = true; menuCoding.Enabled = true; // 預先製作並保存灰階影像 pcxGray = new ImgPcx(pcxOrigin); pcxGray.Gray(); // 清除圖表資料(可重複使用) chart1.Series.Clear(); // 繪製histogram Bitmap origin = (Bitmap)pictureBox1.Image; Bitmap target = origin.Clone(new Rectangle(0, 0, origin.Width, origin.Height), PixelFormat.Format24bppRgb); int w = target.Width; int h = target.Height; double[] yValue1 = new double[256]; double[] yValue2 = new double[256]; double[] yValue3 = new double[256]; Array.Clear(yValue1, 0, yValue1.Length); Array.Clear(yValue2, 0, yValue2.Length); Array.Clear(yValue3, 0, yValue3.Length); for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { Color c = target.GetPixel(x, y); int r = c.R; int g = c.G; int b = c.B; ++yValue1[r]; ++yValue2[g]; ++yValue3[b]; } } // 將點個數轉換為頻率 int total = w * h * 3; for (int i = 0; i < 256; ++i) { yValue1[i] /= total; yValue2[i] /= total; yValue3[i] /= total; } // Range(start, cnt) string[] xValue = Enumerable.Range(0, 256).ToArray().Select(x => x.ToString()).ToArray(); // 設定x軸區間 chart1.ChartAreas["ChartArea1"].AxisX.Interval = 32; chart1.Series.Add("R"); chart1.Series.Add("G"); chart1.Series.Add("B"); chart1.Series["R"].ChartType = SeriesChartType.Column; chart1.Series["R"].Points.DataBindXY(xValue, yValue1); chart1.Series["R"].Color = Color.FromArgb(150, Color.Red); // 半透明顏色 chart1.Series["G"].ChartType = SeriesChartType.Column; chart1.Series["G"].Points.DataBindXY(xValue, yValue2); chart1.Series["G"].Color = Color.FromArgb(150, Color.Green); chart1.Series["B"].ChartType = SeriesChartType.Column; chart1.Series["B"].Points.DataBindXY(xValue, yValue3); chart1.Series["B"].Color = Color.FromArgb(150, Color.Blue); chart1.Visible = true; } else { // 開檔失敗, 無法使用存檔與工具 menuSave.Enabled = false; menuTool.Enabled = false; menuFilter.Enabled = false; menuCoding.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }