Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace AnalizaObrazow
- {
- public partial class MainForm : Form
- {
- private Bitmap img = null;
- private bool isGrayScale = false;
- public MainForm()
- {
- InitializeComponent();
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- }
- private void mainPanel_Paint(object sender, PaintEventArgs e)
- {
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void btFileOpen_Click(object sender, EventArgs e)
- {
- // otwieranie okna dialogowego z plikami
- OpenFileDialog open = new OpenFileDialog();
- // filtry obrazu
- open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
- if(open.ShowDialog() == DialogResult.OK)
- {
- // wyświetlanie obrazka w picture box
- img = new Bitmap(open.FileName);
- bitmap.Image = img;
- isGrayScale = false;
- }
- }
- private void btFileSave_Click(object sender, EventArgs e)
- {
- // otwieranie okna dialogowego zapisu
- SaveFileDialog save = new SaveFileDialog();
- // filtry obrazu
- save.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
- if (save.ShowDialog() == DialogResult.OK)
- {
- bitmap.Image.Save(save.FileName);
- }
- }
- private void btFileNew_Click(object sender, EventArgs e)
- {
- bitmap.Image = null;
- isGrayScale = false;
- }
- // przeciąganie formularza po ekranie
- [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
- private extern static void ReleaseCapture();
- [DllImport("user32.DLL", EntryPoint = "SendMessage")]
- private extern static void SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
- private void panelHeader_MouseDown(object sender, MouseEventArgs e)
- {
- if (btFullscreen.IconChar == FontAwesome.Sharp.IconChar.WindowRestore)
- {
- btFullscreen.IconChar = FontAwesome.Sharp.IconChar.WindowMaximize;
- }
- ReleaseCapture();
- SendMessage(this.Handle, 0x112, 0xf012, 0);
- }
- private void btClose_Click(object sender, EventArgs e)
- {
- this.Close();
- Application.Exit();
- }
- private void btFullscreen_Click(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Normal)
- {
- this.WindowState = FormWindowState.Maximized;
- btFullscreen.IconChar = FontAwesome.Sharp.IconChar.WindowRestore;
- }
- else if (this.WindowState == FormWindowState.Maximized)
- {
- this.WindowState = FormWindowState.Normal;
- btFullscreen.IconChar = FontAwesome.Sharp.IconChar.WindowMaximize;
- }
- }
- private void btMinimize_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- }
- private void btEdit_Click(object sender, EventArgs e)
- {
- // zmiana wygladu przyciskow
- btEdit.BackColor = Color.FromArgb(43, 43, 43);
- btEdit.ForeColor = Color.FromArgb(255, 255, 255);
- btFile.BackColor = Color.FromArgb(210, 19, 19);
- }
- private void btGrayscale_Click(object sender, EventArgs e)
- {
- if(img != null)
- {
- for (int x = 0; x < img.Width; x++)
- {
- for (int y = 0; y < img.Height; y++)
- {
- Color color = img.GetPixel(x, y);
- byte red = (byte)(color.R * 0.299);
- byte green = (byte)(color.G * 0.587);
- byte blue = (byte)(color.B * 0.114);
- int grayScale = red + green + blue;
- color = Color.FromArgb(grayScale, grayScale, grayScale);
- img.SetPixel(x, y, color);
- }
- }
- isGrayScale = true;
- bitmap.Image = img;
- }
- }
- private void btHistogram_Click(object sender, EventArgs e)
- {
- if (isGrayScale)
- {
- int[] pixelsGrayScale = new int[256];
- foreach (int i in pixelsGrayScale)
- {
- pixelsGrayScale[i] = 0;
- }
- for(int x = 0; x < img.Width; x++)
- {
- for(int y = 0; y < img.Height; y++)
- {
- Color color = img.GetPixel(x, y);
- byte grayLevel = color.R;
- Console.WriteLine(grayLevel);
- pixelsGrayScale[grayLevel] += 1;
- }
- }
- GrayscaleHistogramForm histogramForm = new GrayscaleHistogramForm(pixelsGrayScale);
- histogramForm.Show();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement