Advertisement
2bicz

Untitled

May 17th, 2022
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace AnalizaObrazow
  13. {
  14.     public partial class MainForm : Form
  15.     {
  16.         private Bitmap img = null;
  17.         private bool isGrayScale = false;
  18.  
  19.         public MainForm()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void panel1_Paint(object sender, PaintEventArgs e)
  25.         {
  26.  
  27.         }
  28.  
  29.         private void mainPanel_Paint(object sender, PaintEventArgs e)
  30.         {
  31.  
  32.         }
  33.  
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.  
  37.         }
  38.  
  39.         private void btFileOpen_Click(object sender, EventArgs e)
  40.         {
  41.             // otwieranie okna dialogowego z plikami
  42.             OpenFileDialog open = new OpenFileDialog();
  43.  
  44.             // filtry obrazu
  45.             open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
  46.             if(open.ShowDialog() == DialogResult.OK)
  47.             {
  48.                 // wyล›wietlanie obrazka w picture box
  49.                 img = new Bitmap(open.FileName);
  50.                 bitmap.Image = img;
  51.                 isGrayScale = false;
  52.             }
  53.         }
  54.  
  55.         private void btFileSave_Click(object sender, EventArgs e)
  56.         {
  57.             // otwieranie okna dialogowego zapisu
  58.             SaveFileDialog save = new SaveFileDialog();
  59.  
  60.             // filtry obrazu
  61.             save.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
  62.  
  63.             if (save.ShowDialog() == DialogResult.OK)
  64.             {
  65.                 bitmap.Image.Save(save.FileName);
  66.             }
  67.         }
  68.  
  69.         private void btFileNew_Click(object sender, EventArgs e)
  70.         {
  71.             bitmap.Image = null;
  72.             isGrayScale = false;
  73.         }
  74.  
  75.         // przeciฤ…ganie formularza po ekranie
  76.         [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
  77.         private extern static void ReleaseCapture();
  78.  
  79.         [DllImport("user32.DLL", EntryPoint = "SendMessage")]
  80.         private extern static void SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
  81.  
  82.         private void panelHeader_MouseDown(object sender, MouseEventArgs e)
  83.         {
  84.             if (btFullscreen.IconChar == FontAwesome.Sharp.IconChar.WindowRestore)
  85.             {
  86.                 btFullscreen.IconChar = FontAwesome.Sharp.IconChar.WindowMaximize;
  87.             }
  88.  
  89.             ReleaseCapture();
  90.             SendMessage(this.Handle, 0x112, 0xf012, 0);        
  91.         }
  92.  
  93.         private void btClose_Click(object sender, EventArgs e)
  94.         {
  95.             this.Close();
  96.             Application.Exit();
  97.         }
  98.  
  99.         private void btFullscreen_Click(object sender, EventArgs e)
  100.         {
  101.             if (this.WindowState == FormWindowState.Normal)
  102.             {
  103.                 this.WindowState = FormWindowState.Maximized;
  104.                 btFullscreen.IconChar = FontAwesome.Sharp.IconChar.WindowRestore;
  105.  
  106.             }
  107.             else if (this.WindowState == FormWindowState.Maximized)
  108.             {
  109.                 this.WindowState = FormWindowState.Normal;
  110.                 btFullscreen.IconChar = FontAwesome.Sharp.IconChar.WindowMaximize;
  111.             }
  112.         }
  113.  
  114.         private void btMinimize_Click(object sender, EventArgs e)
  115.         {
  116.             this.WindowState = FormWindowState.Minimized;
  117.         }
  118.  
  119.         private void btEdit_Click(object sender, EventArgs e)
  120.         {
  121.             // zmiana wygladu przyciskow
  122.             btEdit.BackColor = Color.FromArgb(43, 43, 43);
  123.             btEdit.ForeColor = Color.FromArgb(255, 255, 255);
  124.             btFile.BackColor = Color.FromArgb(210, 19, 19);
  125.         }
  126.  
  127.         private void btGrayscale_Click(object sender, EventArgs e)
  128.         {
  129.             if(img != null)
  130.             {
  131.                 for (int x = 0; x < img.Width; x++)
  132.                 {
  133.                     for (int y = 0; y < img.Height; y++)
  134.                     {
  135.                         Color color = img.GetPixel(x, y);
  136.                         byte red = (byte)(color.R * 0.299);
  137.                         byte green = (byte)(color.G * 0.587);
  138.                         byte blue = (byte)(color.B * 0.114);
  139.                         int grayScale = red + green + blue;
  140.                         color = Color.FromArgb(grayScale, grayScale, grayScale);
  141.                         img.SetPixel(x, y, color);
  142.                     }
  143.                 }
  144.                 isGrayScale = true;
  145.                 bitmap.Image = img;
  146.             }
  147.         }
  148.  
  149.         private void btHistogram_Click(object sender, EventArgs e)
  150.         {
  151.             if (isGrayScale)
  152.             {
  153.                 int[] pixelsGrayScale = new int[256];
  154.                 foreach (int i in pixelsGrayScale)
  155.                 {
  156.                     pixelsGrayScale[i] = 0;
  157.                 }
  158.  
  159.                 for(int x = 0; x < img.Width; x++)
  160.                 {
  161.                     for(int y = 0; y < img.Height; y++)
  162.                     {
  163.                         Color color = img.GetPixel(x, y);
  164.                         byte grayLevel = color.R;
  165.                         Console.WriteLine(grayLevel);
  166.                         pixelsGrayScale[grayLevel] += 1;
  167.                     }
  168.                 }
  169.  
  170.                 GrayscaleHistogramForm histogramForm = new GrayscaleHistogramForm(pixelsGrayScale);
  171.                 histogramForm.Show();
  172.             }
  173.         }
  174.     }
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement