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.Drawing.Drawing2D;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Kokile.Forms
- {
- public partial class ImageForm : Form
- {
- BarcodeFinder barcodeFinder;
- string barcodeNumber;
- private UserSettings userSettings;
- private ConfigManager configManager;
- private SQLiteManager sqliteManager;
- Image img;
- Point mouseDown;
- int startx = 0; // offset of image when mouse was pressed
- int starty = 0;
- int imgx = 0; // current offset of image
- int imgy = 0;
- bool mousepressed = false; // true as long as left mousebutton is pressed
- float zoom = 1;
- public ImageForm(ConfigManager configManager)
- {
- InitializeComponent();
- this.configManager = configManager;
- barcodeFinder = new BarcodeFinder(barcodePictureBox);
- LoadUserSettings();
- sqliteManager = new SQLiteManager(userSettings.databasePath);
- barcodeLabel.Text = "5000";
- //imagePictureBox.Image = Image.FromFile("image.jpg");
- //this.imagePictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.imageBox_MouseDown);
- //this.imagePictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
- //this.imagePictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.imageBox_MouseUp);
- }
- private void LoadUserSettings()
- {
- this.userSettings = new UserSettings();
- this.userSettings = configManager.LoadSettings();
- }
- private void barcodePictureBox_MouseDown(object sender, MouseEventArgs e)
- {
- barcodeNumber = barcodeFinder.CalculateClickedRectangleIndex(e.X);
- if (!File.Exists(this.userSettings.databasePath))
- {
- barcodeLabel.Text = "Nie połączono z bazą danych";
- barcodeLabel.ForeColor = Color.Red;
- return;
- }
- barcodeLabel.ForeColor = Color.Black;
- int barcodeId = sqliteManager.SearchForBarcodeId(barcodeNumber);
- if (barcodeId == -1)
- {
- barcodeLabel.Text = "brak";
- }
- else
- {
- barcodeLabel.Text = barcodeId.ToString();
- }
- }
- public void UpdateConfigFromConfigManager(ConfigManager configManager)
- {
- this.configManager = configManager;
- this.userSettings = this.configManager.LoadSettings();
- sqliteManager.changeDatabasePath(userSettings.databasePath);
- }
- private void imagePictureBox_MouseMove(object sender, MouseEventArgs e)
- {
- MouseEventArgs mouse = e as MouseEventArgs;
- if (mouse.Button == MouseButtons.Left)
- {
- Point mousePosNow = mouse.Location;
- int deltaX = mousePosNow.X - mouseDown.X; // the distance the mouse has been moved since mouse was pressed
- int deltaY = mousePosNow.Y - mouseDown.Y;
- imgx = (int)(startx + (deltaX / zoom)); // calculate new offset of image based on the current zoom factor
- imgy = (int)(starty + (deltaY / zoom));
- imagePictureBox.Refresh();
- }
- }
- private void imagePictureBox_MouseDown(object sender, MouseEventArgs e)
- {
- MouseEventArgs mouse = e as MouseEventArgs;
- if (mouse.Button == MouseButtons.Left)
- {
- if (!mousepressed)
- {
- mousepressed = true;
- mouseDown = mouse.Location;
- startx = imgx;
- starty = imgy;
- }
- }
- }
- private void imagePictureBox_MouseUp(object sender, MouseEventArgs e)
- {
- mousepressed = false;
- }
- protected override void OnMouseWheel(MouseEventArgs e)
- {
- float oldzoom = zoom;
- if (e.Delta > 0)
- {
- zoom += 0.1F;
- }
- else if (e.Delta < 0)
- {
- zoom = Math.Max(zoom - 0.1F, 0.01F);
- }
- MouseEventArgs mouse = e as MouseEventArgs;
- Point mousePosNow = mouse.Location;
- int x = mousePosNow.X - imagePictureBox.Location.X; // Where location of the mouse in the pictureframe
- int y = mousePosNow.Y - imagePictureBox.Location.Y;
- int oldimagex = (int)(x / oldzoom); // Where in the IMAGE is it now
- int oldimagey = (int)(y / oldzoom);
- int newimagex = (int)(x / zoom); // Where in the IMAGE will it be when the new zoom i made
- int newimagey = (int)(y / zoom);
- imgx = newimagex - oldimagex + imgx; // Where to move image to keep focus on one point
- imgy = newimagey - oldimagey + imgy;
- imagePictureBox.Refresh(); // calls imageBox_Paint
- }
- private void imagePictureBox_Paint(object sender, PaintEventArgs e)
- {
- e.Graphics.Clear(Color.Red);
- e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
- //e.Graphics.ScaleTransform(zoom, zoom);
- e.Graphics.ScaleTransform(zoom, zoom);
- e.Graphics.DrawImage(img, imgx, imgy);
- }
- protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
- {
- const int WM_KEYDOWN = 0x100;
- const int WM_SYSKEYDOWN = 0x104;
- if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
- {
- switch (keyData)
- {
- case Keys.Right:
- imgx -= (int)(imagePictureBox.Width * 0.1F / zoom);
- imagePictureBox.Refresh();
- break;
- case Keys.Left:
- imgx += (int)(imagePictureBox.Width * 0.1F / zoom);
- imagePictureBox.Refresh();
- break;
- case Keys.Down:
- imgy -= (int)(imagePictureBox.Height * 0.1F / zoom);
- imagePictureBox.Refresh();
- break;
- case Keys.Up:
- imgy += (int)(imagePictureBox.Height * 0.1F / zoom);
- imagePictureBox.Refresh();
- break;
- case Keys.PageDown:
- imgy -= (int)(imagePictureBox.Height * 0.90F / zoom);
- imagePictureBox.Refresh();
- break;
- case Keys.PageUp:
- imgy += (int)(imagePictureBox.Height * 0.90F / zoom);
- imagePictureBox.Refresh();
- break;
- }
- }
- return base.ProcessCmdKey(ref msg, keyData);
- }
- private void loadImageBtn_Click(object sender, EventArgs e)
- {
- string selectedImagePath = "";
- using (OpenFileDialog openFileDialog = new OpenFileDialog())
- {
- openFileDialog.Filter = "Image Files (*.jpg; *.jpeg; *.png)|*.jpg;*.jpeg;*.png|All Files (*.*)|*.*";
- openFileDialog.FilterIndex = 1;
- openFileDialog.RestoreDirectory = true;
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- try
- {
- // Load the selected image file
- selectedImagePath = openFileDialog.FileName;
- //Image selectedImage = Image.FromFile(selectedImagePath);
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error loading image: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- }
- }
- img = Image.FromFile(selectedImagePath);
- using (Graphics graphics = Graphics.FromImage(img))
- {
- Pen pen = new Pen(Color.Red, 3);
- graphics.DrawRectangle(pen, new Rectangle(50, 50, 100, 100));
- }
- Graphics g = this.CreateGraphics();
- //// Fit whole image
- //zoom = Math.Min(
- // ((float)imagePictureBox.Height / (float)img.Height) * (img.VerticalResolution / g.DpiY),
- // ((float)imagePictureBox.Width / (float)img.Width) * (img.HorizontalResolution / g.DpiX)
- //);
- //Fit width
- zoom = ((float)imagePictureBox.Width / (float)img.Width) * (img.HorizontalResolution / g.DpiX);
- imagePictureBox.Paint += new PaintEventHandler(imagePictureBox_Paint);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment