kapko

Untitled

Aug 7th, 2023
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.24 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.Drawing.Drawing2D;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace Kokile.Forms
  14. {
  15.     public partial class ImageForm : Form
  16.     {
  17.         BarcodeFinder barcodeFinder;
  18.         string barcodeNumber;
  19.         private UserSettings userSettings;
  20.         private ConfigManager configManager;
  21.         private SQLiteManager sqliteManager;
  22.  
  23.         Image img;
  24.         Point mouseDown;
  25.         int startx = 0;             // offset of image when mouse was pressed
  26.         int starty = 0;
  27.         int imgx = 0;               // current offset of image
  28.         int imgy = 0;
  29.  
  30.         bool mousepressed = false;  // true as long as left mousebutton is pressed
  31.         float zoom = 1;
  32.  
  33.         public ImageForm(ConfigManager configManager)
  34.         {
  35.             InitializeComponent();
  36.             this.configManager = configManager;
  37.             barcodeFinder = new BarcodeFinder(barcodePictureBox);
  38.             LoadUserSettings();
  39.             sqliteManager = new SQLiteManager(userSettings.databasePath);
  40.             barcodeLabel.Text = "5000";
  41.             //imagePictureBox.Image = Image.FromFile("image.jpg");
  42.  
  43.  
  44.  
  45.            
  46.  
  47.             //this.imagePictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.imageBox_MouseDown);
  48.             //this.imagePictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
  49.             //this.imagePictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.imageBox_MouseUp);
  50.         }
  51.  
  52.         private void LoadUserSettings()
  53.         {
  54.             this.userSettings = new UserSettings();
  55.             this.userSettings = configManager.LoadSettings();
  56.         }
  57.  
  58.         private void barcodePictureBox_MouseDown(object sender, MouseEventArgs e)
  59.         {
  60.             barcodeNumber = barcodeFinder.CalculateClickedRectangleIndex(e.X);
  61.             if (!File.Exists(this.userSettings.databasePath))
  62.             {
  63.                 barcodeLabel.Text = "Nie połączono z bazą danych";
  64.                 barcodeLabel.ForeColor = Color.Red;
  65.                 return;
  66.             }
  67.  
  68.             barcodeLabel.ForeColor = Color.Black;
  69.             int barcodeId = sqliteManager.SearchForBarcodeId(barcodeNumber);
  70.  
  71.             if (barcodeId == -1)
  72.             {
  73.                 barcodeLabel.Text = "brak";
  74.             }
  75.             else
  76.             {
  77.                 barcodeLabel.Text = barcodeId.ToString();
  78.             }
  79.         }
  80.  
  81.  
  82.         public void UpdateConfigFromConfigManager(ConfigManager configManager)
  83.         {
  84.             this.configManager = configManager;
  85.             this.userSettings = this.configManager.LoadSettings();
  86.             sqliteManager.changeDatabasePath(userSettings.databasePath);
  87.         }
  88.  
  89.         private void imagePictureBox_MouseMove(object sender, MouseEventArgs e)
  90.         {
  91.             MouseEventArgs mouse = e as MouseEventArgs;
  92.  
  93.             if (mouse.Button == MouseButtons.Left)
  94.             {
  95.                 Point mousePosNow = mouse.Location;
  96.  
  97.                 int deltaX = mousePosNow.X - mouseDown.X; // the distance the mouse has been moved since mouse was pressed
  98.                 int deltaY = mousePosNow.Y - mouseDown.Y;
  99.  
  100.                 imgx = (int)(startx + (deltaX / zoom));  // calculate new offset of image based on the current zoom factor
  101.                 imgy = (int)(starty + (deltaY / zoom));
  102.  
  103.                 imagePictureBox.Refresh();
  104.             }
  105.         }
  106.  
  107.         private void imagePictureBox_MouseDown(object sender, MouseEventArgs e)
  108.         {
  109.             MouseEventArgs mouse = e as MouseEventArgs;
  110.  
  111.             if (mouse.Button == MouseButtons.Left)
  112.             {
  113.                 if (!mousepressed)
  114.                 {
  115.                     mousepressed = true;
  116.                     mouseDown = mouse.Location;
  117.                     startx = imgx;
  118.                     starty = imgy;
  119.                 }
  120.             }
  121.         }
  122.  
  123.         private void imagePictureBox_MouseUp(object sender, MouseEventArgs e)
  124.         {
  125.             mousepressed = false;
  126.         }
  127.  
  128.         protected override void OnMouseWheel(MouseEventArgs e)
  129.         {
  130.             float oldzoom = zoom;
  131.  
  132.             if (e.Delta > 0)
  133.             {
  134.                 zoom += 0.1F;
  135.             }
  136.  
  137.             else if (e.Delta < 0)
  138.             {
  139.                 zoom = Math.Max(zoom - 0.1F, 0.01F);
  140.             }
  141.  
  142.             MouseEventArgs mouse = e as MouseEventArgs;
  143.             Point mousePosNow = mouse.Location;
  144.  
  145.             int x = mousePosNow.X - imagePictureBox.Location.X;    // Where location of the mouse in the pictureframe
  146.             int y = mousePosNow.Y - imagePictureBox.Location.Y;
  147.  
  148.             int oldimagex = (int)(x / oldzoom);  // Where in the IMAGE is it now
  149.             int oldimagey = (int)(y / oldzoom);
  150.  
  151.             int newimagex = (int)(x / zoom);     // Where in the IMAGE will it be when the new zoom i made
  152.             int newimagey = (int)(y / zoom);
  153.  
  154.             imgx = newimagex - oldimagex + imgx;  // Where to move image to keep focus on one point
  155.             imgy = newimagey - oldimagey + imgy;
  156.  
  157.             imagePictureBox.Refresh();  // calls imageBox_Paint
  158.         }
  159.  
  160.  
  161.         private void imagePictureBox_Paint(object sender, PaintEventArgs e)
  162.         {
  163.             e.Graphics.Clear(Color.Red);
  164.             e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  165.             //e.Graphics.ScaleTransform(zoom, zoom);
  166.             e.Graphics.ScaleTransform(zoom, zoom);
  167.             e.Graphics.DrawImage(img, imgx, imgy);
  168.         }
  169.  
  170.  
  171.         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  172.         {
  173.             const int WM_KEYDOWN = 0x100;
  174.             const int WM_SYSKEYDOWN = 0x104;
  175.  
  176.             if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
  177.             {
  178.                 switch (keyData)
  179.                 {
  180.                     case Keys.Right:
  181.                         imgx -= (int)(imagePictureBox.Width * 0.1F / zoom);
  182.                         imagePictureBox.Refresh();
  183.                         break;
  184.  
  185.                     case Keys.Left:
  186.                         imgx += (int)(imagePictureBox.Width * 0.1F / zoom);
  187.                         imagePictureBox.Refresh();
  188.                         break;
  189.  
  190.                     case Keys.Down:
  191.                         imgy -= (int)(imagePictureBox.Height * 0.1F / zoom);
  192.                         imagePictureBox.Refresh();
  193.                         break;
  194.  
  195.                     case Keys.Up:
  196.                         imgy += (int)(imagePictureBox.Height * 0.1F / zoom);
  197.                         imagePictureBox.Refresh();
  198.                         break;
  199.  
  200.                     case Keys.PageDown:
  201.                         imgy -= (int)(imagePictureBox.Height * 0.90F / zoom);
  202.                         imagePictureBox.Refresh();
  203.                         break;
  204.  
  205.                     case Keys.PageUp:
  206.                         imgy += (int)(imagePictureBox.Height * 0.90F / zoom);
  207.                         imagePictureBox.Refresh();
  208.                         break;
  209.                 }
  210.             }
  211.  
  212.             return base.ProcessCmdKey(ref msg, keyData);
  213.         }
  214.  
  215.         private void loadImageBtn_Click(object sender, EventArgs e)
  216.         {
  217.             string selectedImagePath = "";
  218.             using (OpenFileDialog openFileDialog = new OpenFileDialog())
  219.             {
  220.                 openFileDialog.Filter = "Image Files (*.jpg; *.jpeg; *.png)|*.jpg;*.jpeg;*.png|All Files (*.*)|*.*";
  221.                 openFileDialog.FilterIndex = 1;
  222.                 openFileDialog.RestoreDirectory = true;
  223.  
  224.                 if (openFileDialog.ShowDialog() == DialogResult.OK)
  225.                 {
  226.                     try
  227.                     {
  228.                         // Load the selected image file
  229.                         selectedImagePath = openFileDialog.FileName;
  230.                         //Image selectedImage = Image.FromFile(selectedImagePath);
  231.                     }
  232.                     catch (Exception ex)
  233.                     {
  234.                         MessageBox.Show("Error loading image: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  235.                         return;
  236.                     }
  237.                 }
  238.             }
  239.  
  240.             img = Image.FromFile(selectedImagePath);
  241.             using (Graphics graphics = Graphics.FromImage(img))
  242.             {
  243.                 Pen pen = new Pen(Color.Red, 3);
  244.                 graphics.DrawRectangle(pen, new Rectangle(50, 50, 100, 100));
  245.             }
  246.  
  247.             Graphics g = this.CreateGraphics();
  248.  
  249.             //// Fit whole image
  250.             //zoom = Math.Min(
  251.             //  ((float)imagePictureBox.Height / (float)img.Height) * (img.VerticalResolution / g.DpiY),
  252.             //  ((float)imagePictureBox.Width / (float)img.Width) * (img.HorizontalResolution / g.DpiX)
  253.             //);
  254.  
  255.             //Fit width
  256.             zoom = ((float)imagePictureBox.Width / (float)img.Width) * (img.HorizontalResolution / g.DpiX);
  257.            
  258.             imagePictureBox.Paint += new PaintEventHandler(imagePictureBox_Paint);
  259.         }
  260.     }
  261. }
  262.  
Advertisement
Add Comment
Please, Sign In to add comment