LISTING PROGRAM
FORM EDGE---
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 System.IO;
using System.Drawing.Imaging;
namespace CompassEdgeDetection {
publicpartialclassfrmEdges : Form {
privateBitmap originalBitmap = null; privateBitmap previewBitmap = null; privateBitmap resultBitmap = null;
public frmEdges() {
InitializeComponent();
cmbEdgeDetection.SelectedIndex = 0; }
privatevoid btnOpenOriginal_Click(object sender, EventArgs e) {
OpenFileDialog ofd = newOpenFileDialog(); ofd.Title = "Pilih file citra";
ofd.Filter = "Jpeg Images(*.jpg)|*.jpg"; ofd.Filter += "|Bitmap Images(*.bmp)|*.bmp"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
StreamReader streamReader = newStreamReader(ofd.FileName); Bitmap img = newBitmap(ofd.FileName);
var imageHeight = img.Height; var imageWidth = img.Width;
originalBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream); streamReader.Close();
lblNmFile.Text = ofd.FileName;
lblSize.Text = imageWidth.ToString() + " x " + imageHeight.ToString() + " Piksel";
previewBitmap = originalBitmap.CopyToSquareCanvas(PicAsli.Width); PicAsli.Image = previewBitmap;
//ApplyFilter(true) } }
privatevoid btnSaveNewImage_Click(object sender, EventArgs e) {
// TextWriter tw = new StreamWriter("Perhitungan.text"); // tw.WriteLine(textBox1.Text);
// tw.Close();
ApplyFilter(false);
if (resultBitmap != null) {
SaveFileDialog sfd = newSaveFileDialog();
sfd.Title = "Masukkan Nama File Citra hasil"; sfd.Filter = "Jpeg Images(*.jpg)|*.jpg"; sfd.Filter += "|Bitmap Images(*.bmp)|*.bmp"; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
string fileExtension = Path.GetExtension(sfd.FileName).ToUpper(); ImageFormat imgFormat = ImageFormat.Png;
if (fileExtension == "BMP") {
imgFormat = ImageFormat.Bmp; }
elseif (fileExtension == "JPG") {
imgFormat = ImageFormat.Jpeg; }
StreamWriter streamWriter = newStreamWriter(sfd.FileName, false);
resultBitmap.Save(streamWriter.BaseStream, imgFormat); lblNmFile1.Text = sfd.FileName;
FileInfo fi = newFileInfo(sfd.FileName); long size = fi.Length / 1024;
lblSize1.Text = size.ToString(); streamWriter.Flush();
streamWriter.Close();
resultBitmap = null;
btnSaveNewImage.Enabled = false; }
} }
privatevoid ApplyFilter(bool preview) {
if (previewBitmap == null || cmbEdgeDetection.SelectedIndex == -1) {
return; }
Bitmap selectedSource = null; Bitmap bitmapResult = null;
if (preview == true) {
selectedSource = previewBitmap; }
else
{
selectedSource = originalBitmap; }
if (selectedSource != null) {
if (cmbEdgeDetection.SelectedItem.ToString() == "Sobel 3x3 Grayscale") {
resultBitmap = selectedSource.Sobel3x3Filter(); picSobel.Image = bitmapResult;
btnSaveNewImage.Enabled = true; }
elseif (cmbEdgeDetection.SelectedItem.ToString() == "Kombi 3x3 Grayscale") {
bitmapResult = selectedSource.Kombi3x3Filter(); resultBitmap = selectedSource.Kombi3x3Filter(); picKombi.Image = bitmapResult;
btnSaveKombi.Enabled = true; }
elseif (cmbEdgeDetection.SelectedItem.ToString() == "Prewitt 3x3 Grayscale") {
bitmapResult = selectedSource.PrewittFilter(); picPrewitt.Image = bitmapResult;
resultBitmap = selectedSource.PrewittFilter(); btnSavePrewitt.Enabled = true;
}
}
if (bitmapResult != null) {
// if (preview == true) // {
// picSobel.Image = bitmapResult; // }
// else // {
// resultBitmap = bitmapResult; // }
}
}
privatevoid NeighbourCountValueChangedEventHandler(object sender, EventArgs e) {
ApplyFilter(true);
btnOpenOriginal.Enabled = false; // cmbEdgeDetection.SelectedItem = 1;
// cmbEdgeDetection.SelectedIndex = 1;
// ApplyFilter(false); }
privatevoid btnClear_Click(object sender, EventArgs e) {
cmbEdgeDetection.SelectedItem = 0; cmbEdgeDetection.SelectedIndex = 0; picSobel.Image = null;
lblSize1.Text = ""; lblSize2.Text = ""; lblSize3.Text = "";
btnSaveNewImage.Enabled = false; btnSavePrewitt.Enabled = false; btnSaveKombi.Enabled = false; btnOpenOriginal.Enabled = true; // textBox1.Text = "";
// TextWriter tw = new StreamWriter("Perhitungan.text"); // tw.WriteLine("Perhitungan nilai piksel adalah"); // tw.Close();
// TextReader baca = new StreamReader("Perhitungan.text"); // String teks = baca.ReadLine();
// MessageBox.Show(teks, "Baca text", MessageBoxButtons.OK, MessageBoxIcon.Information);
// baca.Close();
}
privatevoid btnExit_Click(object sender, EventArgs e) {
Dispose(); }
privatevoid GetPixel(int pixelBuffer) {
int nilaiPixel = 256; int nilR;
int nilG; int nilB;
nilR = nilaiPixel & 255;
nilG = nilaiPixel & 65280 / 256;
nilB = nilaiPixel & 16711680 / 256 / 256; }
void myImage() {
Bitmap mypic = newBitmap(PicAsli.Image); int imWid = PicAsli.Image.Width;
int imHei = PicAsli.Image.Height; int total = imWid * imHei;
//textBox1.AppendText("R G B " + Environment.NewLine);
//textBox1.AppendText("---" + Environment.NewLine);
for (int z = 0; z < imWid; z++) {
for (int i = 0; i < imHei; i++) {
Color pixelColor = mypic.GetPixel(z, i);
// textBox1.AppendText(" " + pixelColor.R + " " + pixelColor.G + " " + pixelColor.B + Environment.NewLine);
} }
// textBox1.AppendText("GRAYSCALE " + Environment.NewLine); // textBox1.AppendText("---" + Environment.NewLine);
for (int i = 0; i < imHei; i++) {
Color pixelColor = mypic.GetPixel(z, i);
// textBox1.AppendText(" " + (pixelColor.R + pixelColor.G + pixelColor.B)/3 + Environment.NewLine);
} }
// textBox1.AppendText("BINERISASI " + Environment.NewLine); // textBox1.AppendText("---" + Environment.NewLine);
for (int z = 0; z < imWid; z++) {
for (int i = 0; i < imHei; i++) {
Color pixelColor = mypic.GetPixel(z, i); Single Gray;
int Biner;
Gray = (pixelColor.R + pixelColor.G + pixelColor.B) / 3; if (Gray > 128)
{
Biner=1; }
else
{
Biner=0; }
// lbBiner.Items.Add (" " + Biner + Environment.NewLine);
} }
// ----> Lihat Program ExtBitmap.cs Line 400
//TextWriter tw = new StreamWriter("PerhitunganOk.text"); //tw.WriteLine(textBox1.Text);
//tw.Close();
cmbEdgeDetection.SelectedItem = 1; cmbEdgeDetection.SelectedIndex = 1; }
privatevoid frmEdges_Load(object sender, EventArgs e) {
btnSaveNewImage.Enabled = false; btnSaveKombi.Enabled = false; btnSavePrewitt.Enabled = false; btnOpenOriginal.Enabled = true;
}
privatevoid btnSavePrewitt_Click(object sender, EventArgs e) {
btnSavePrewitt.Enabled = true; ApplyFilter(false);
if (resultBitmap != null) {
SaveFileDialog sfd = newSaveFileDialog();
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
string fileExtension = Path.GetExtension(sfd.FileName).ToUpper(); ImageFormat imgFormat = ImageFormat.Png;
if (fileExtension == "BMP") {
imgFormat = ImageFormat.Bmp; }
elseif (fileExtension == "JPG") {
imgFormat = ImageFormat.Jpeg; }
StreamWriter streamWriter = newStreamWriter(sfd.FileName, false);
resultBitmap.Save(streamWriter.BaseStream, imgFormat); lblNmFile2.Text = sfd.FileName;
FileInfo fi = newFileInfo(sfd.FileName); long size = fi.Length / 1024;
lblSize2.Text = size.ToString(); streamWriter.Flush();
streamWriter.Close(); resultBitmap = null;
btnSavePrewitt.Enabled = false; }
} }
privatevoid btnSaveKombi_Click(object sender, EventArgs e) {
btnSaveKombi.Enabled = true; ApplyFilter(false);
if (resultBitmap != null) {
SaveFileDialog sfd = newSaveFileDialog();
sfd.Title = "Masukkan Nama File Citra hasil"; sfd.Filter = "Jpeg Images(*.jpg)|*.jpg"; sfd.Filter += "|Bitmap Images(*.bmp)|*.bmp"; if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
string fileExtension = Path.GetExtension(sfd.FileName).ToUpper(); ImageFormat imgFormat = ImageFormat.Png;
if (fileExtension == "BMP") {
imgFormat = ImageFormat.Bmp; }
elseif (fileExtension == "JPG") {
imgFormat = ImageFormat.Jpeg; }
StreamWriter streamWriter = newStreamWriter(sfd.FileName, false);
resultBitmap.Save(streamWriter.BaseStream, imgFormat); lblNmFile3.Text = sfd.FileName;
FileInfo fi = newFileInfo(sfd.FileName); long size = fi.Length / 1024;
streamWriter.Flush(); streamWriter.Close();
resultBitmap = null;
btnSaveKombi.Enabled = false; }
} } } }
FORM PERKALIAN---
staticvoid Main1(string[] args) {
Console.WriteLine("Perkalian matriks A dan Matriks B"); int barisA, kolomA, barisB, kolomB;
string input;
Random randomInt = newRandom();
Console.Write("Baris");
barisA = int.Parse(Console.ReadLine()); Console.Write("Kolomn Baris B =");
kolomA = barisB = int.Parse(Console.ReadLine()); Console.Write("Kolom");
kolomB = int.Parse(Console.ReadLine()); int[,] matA = newint[barisA, kolomA];
int[,] matB = newint[barisB, kolomB]; int[,] matC = newint[barisA, kolomB]; for (int i = 0; i < barisA; i++) for (int j = 0; j < kolomA; j++)
matA[i, j] = randomInt.Next(1, 3);
for (int i = 0; i < barisB; i++) for (int j = 0; j < kolomB; j++)
matB[i, j] = randomInt.Next(1, 3);
Console.Write("Matriks\n\n"); {
for (int i = 0; i < barisA; i++) {
for (int j = 0; j < kolomA; j++) Console.Write(matA[i, j] + "\t"); Console.WriteLine();
} }
Console.Write("\niks B :\n"); {
for (int i = 0; i < barisB; i++) {
for (int j = 0; j < kolomB; j++) Console.Write(matB[i, j] + "\t"); Console.WriteLine();
} }
Console.WriteLine("\n Hasil perkalian matriks 2D A dan B :"); {
for (int a = 0; a < barisA; a++) {
matC[a, b] = 0; for (int c = 0; c < kolomA; c++)
{
matC[a, b] += matA[a, c] * matB[c, b]; }
Console.Write(matC[a, b] + "\t"); }
Console.WriteLine(); }
Console.WriteLine("by teguh"); Console.ReadLine();
} } } }
FORM BRIGHTNES---
publicpartialclassfrmBrighness : Form {
OpenFileDialog oDlg; SaveFileDialog sDlg; int ID;
// private MenuItem cZoom;
ImageHandler imageHandler = newImageHandler();
string koneksi = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/TA-UNPRI-2016/JUAN/ProgramSobel/ImageEdgeDetection/bin/Debug/dbEdge.mdb";
public frmBrighness() {
InitializeComponent();
oDlg = newOpenFileDialog(); // Open Dialog Initialization oDlg.RestoreDirectory = true;
oDlg.InitialDirectory = "C:\\"; oDlg.FilterIndex = 1;
oDlg.Filter = "jpg Files (*.jpg)|*.jpg|bmp Files (*.bmp)|*.bmp"; /*************************/
sDlg = newSaveFileDialog(); // Save Dialog Initialization sDlg.RestoreDirectory = true;
sDlg.InitialDirectory = "C:\\"; sDlg.FilterIndex = 1;
sDlg.Filter = "jpg Files (*.jpg)|*.jpg|bmp Files (*.bmp)|*.bmp"; }
privateImage Img;
privateSize OriginalImageSize; privateSize ModifiedImageSize;
int cropX; int cropY; int cropWidth;
int cropHeight; int oCropX; int oCropY;
publicPen cropPen;
publicDashStyle cropDashStyle = DashStyle.DashDot; publicbool Makeselection = false;
privatevoid OpenToolStripMenuItem_Click(object sender, EventArgs e) {
// OpenFileDialog Dlg = new OpenFileDialog(); // Dlg.Filter = "";
// Dlg.Title = "Select image";
// if (Dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) // {
// Img = Image.FromFile(Dlg.FileName);
// //Image.FromFile(String) method creates an image from the specifed file, here dlg.Filename contains the name of the file from which to create the image
// LoadImage(); // }
if (DialogResult.OK == oDlg.ShowDialog()) {
imageHandler.CurrentBitmap = (Bitmap)Bitmap.FromFile(oDlg.FileName); imageHandler.BitmapPath = oDlg.FileName;
// this.AutoScroll = true;
// this.AutoScrollMinSize = new Size(Convert.ToInt32(imageHandler.CurrentBitmap.Width * zoomFactor), Convert.ToInt32(imageHandler.CurrentBitmap.Height * zoomFactor));
// this.Invalidate();
Img = Image.FromFile(oDlg.FileName); LoadImage();
//menuItemImageInfo.Enabled = true;
//ImageInfo imgInfo = new ImageInfo(imageHandler); //imgInfo.Show();
}
}
privatevoid LoadImage() {
//we set the picturebox size according to image, we can get image width and height with the help of Image.Width and Image.height properties.
int imgWidth = Img.Width; int imghieght = Img.Height;
PictureBox1.Width = imgWidth; PictureBox1.Height = imghieght; PictureBox1.Image = Img;
PictureBoxLocation();
OriginalImageSize = newSize(imgWidth, imghieght);
OleDbDataReader rdr = null;
string sql = string.Format("select * from Hasil where NmFile='" + oDlg.FileName +
"'");
OleDbConnection conn = newOleDbConnection(koneksi); conn.Open();
OleDbCommand cmd = newOleDbCommand(sql, conn);
rdr = cmd.ExecuteReader(); if (rdr.Read())
{
MessageBox.Show("Citra sudah ada disimpan", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
// ID = Convert.ToInt32(rdr["ID"]) + 1;
txtArti.Text = Convert.ToString(rdr["Arti"]); btnSave.Enabled = false;
{
btnSave.Enabled = true;
//MessageBox.Show("Citra tidak ada", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
privatevoid PictureBoxLocation() {
int _x = 0; int _y = 0;
if (SplitContainer1.Panel1.Width > PictureBox1.Width) {
_x = (SplitContainer1.Panel1.Width - PictureBox1.Width) / 2; }
if (SplitContainer1.Panel1.Height > PictureBox1.Height) {
_y = (SplitContainer1.Panel1.Height - PictureBox1.Height) / 2; }
PictureBox1.Location = newPoint(_x, _y); }
privatevoid SplitContainer1_Panel1_Resize(object sender, EventArgs e) {
PictureBoxLocation(); }
privatevoid btnOk_Click(object sender, EventArgs e) {
Bitmap bm_source = newBitmap(PictureBox1.Image); // Make a bitmap for the result.
Bitmap bm_dest = newBitmap(Convert.ToInt32(ModifiedImageSize.Width), Convert.ToInt32(ModifiedImageSize.Height));
// Make a Graphics object for the result Bitmap. Graphics gr_dest = Graphics.FromImage(bm_dest); // Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1); // Display the result.
PictureBox1.Image = bm_dest; PictureBox1.Width = bm_dest.Width; PictureBox1.Height = bm_dest.Height; PictureBoxLocation();
}
privatevoid Form1_Load(object sender, EventArgs e) {
string sql = "SELECT * FROM Hasil";
OleDbConnection con = newOleDbConnection(koneksi); con.Open();
}
# region "---Crop Image---"
privatevoid PictureBox1_MouseDown(object sender, MouseEventArgs e) {
if (TabControl1.SelectedIndex == 4) {
if (CreateText) {
Cursor = Cursors.IBeam; }
} else
{
Cursor = Cursors.Default; if (Makeselection)
{
try
{
if (e.Button == System.Windows.Forms.MouseButtons.Left) {
Cursor = Cursors.Cross; cropX = e.X;
cropY = e.Y;
cropPen = newPen(Color.Black, 1);
cropPen.DashStyle = DashStyle.DashDotDot;
}
PictureBox1.Refresh(); }
catch (Exception ex) { } } }
}
privatevoid PictureBox1_MouseUp(object sender, MouseEventArgs e) {
if (Makeselection) {
Cursor = Cursors.Default; }
}
privatevoid PictureBox1_MouseMove(object sender, MouseEventArgs e) {
if (TabControl1.SelectedIndex == 4) {
Point TextStartLocation = e.Location; if (CreateText)
{
Cursor = Cursors.IBeam; }
} else
{
Cursor = Cursors.Default; if (Makeselection)
{
try
if (PictureBox1.Image == null) return;
if (e.Button == System.Windows.Forms.MouseButtons.Left) {
PictureBox1.Refresh(); cropWidth = e.X - cropX; cropHeight = e.Y - cropY;
PictureBox1.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
}
} catch (Exception ex) { //if (ex.Number == 5) // return;
} } }
}
# endregion
privatevoid TrackBarBrightness_Scroll(object sender, EventArgs e) {
DomainUpDownBrightness.Text = TrackBarBrightness.Value.ToString();
float value = TrackBarBrightness.Value * 0.01f; float[][] colorMatrixElements = {
newfloat[] { 1, 0, 0, 0, 0 },
newfloat[] { 0, 1, 0, 0, 0 },
newfloat[] { 0, 0, 1, 0, 0 },
newfloat[] { value, value, value, 0, 1 }
};
ColorMatrix colorMatrix = newColorMatrix(colorMatrixElements); ImageAttributes imageAttributes = newImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Image _img = Img; //PictureBox1.Image
Graphics _g = default(Graphics);
Bitmap bm_dest = newBitmap(Convert.ToInt32(_img.Width), Convert.ToInt32(_img.Height)); _g = Graphics.FromImage(bm_dest);
_g.DrawImage(_img, newRectangle(0, 0, bm_dest.Width + 1, bm_dest.Height + 1), 0, 0, bm_dest.Width + 1, bm_dest.Height + 1, GraphicsUnit.Pixel, imageAttributes); PictureBox1.Image = bm_dest;
}
privatevoid btnRotateLeft_Click(object sender, EventArgs e) {
PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone); PictureBox1.Refresh();
}
privatevoid btnRotateRight_Click(object sender, EventArgs e) {
PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone); PictureBox1.Refresh();
}
privatevoid btnRotateHorizantal_Click(object sender, EventArgs e) {
PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX); PictureBox1.Refresh();
}
privatevoid btnRotatevertical_Click(object sender, EventArgs e) {
PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipY); PictureBox1.Refresh();
}
privatevoid TabPage1_Click(object sender, EventArgs e) {
}
privatevoid SaveToolStripMenuItem_Click(object sender, EventArgs e) {
if (DialogResult.OK == sDlg.ShowDialog()) {
imageHandler.SaveBitmap(sDlg.FileName); }
}
Dispose(); }
privatevoid btnSave_Click(object sender, EventArgs e) {
OleDbDataReader rdr = null;
string sql = string.Format("select * from Hasil order by ID desc"); OleDbConnection conn = newOleDbConnection(koneksi);
conn.Open();
OleDbCommand cmd = newOleDbCommand(sql, conn);
rdr = cmd.ExecuteReader(); if (rdr.Read())
{
ID = Convert.ToInt32(rdr["ID"]) + 1; }
else
{
ID = 1; }
sql = string.Format("insert into Hasil (ID,NmFile,Arti) VALUES ('{0}','{1}','{2}')", ID, oDlg.FileName,txtArti.Text);
OleDbCommand cmd1 = newOleDbCommand(sql, conn); cmd1.ExecuteNonQuery();
conn.Close();
btnSave.Enabled = false;
MessageBox.Show("Citra sudah disimpan", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtArti.Text = ""; }
privatevoid MenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
}
privatevoid btnKeluar_Click(object sender, EventArgs e) {
Dispose(); }
privatevoid btnClear_Click(object sender, EventArgs e) {
txtArti.Text = "";
PictureBox1.Image = null;
}
privatevoid PictureBox1_Click(object sender, EventArgs e) {
} } }