How do I convert from screen space coordinates to image space coordinates in WinForms PictureBox?
// Recompute the image scaling the zoom mode uses to fit the image on screen imageScale ::= min(pictureBox.width / image.width, pictureBox.height / image.height) scaledWidth ::= image.width * imageScale scaledHeight ::= image.height * imageScale // Compute the offset of the image to center it in the picture box imageX ::= (pictureBox.width - scaledWidth) / 2 imageY ::= (pictureBox.height - scaledHeight) / 2 // Test the coordinate in the picture box against the image bounds if pos.x < imageX or imageX + scaledWidth < pos.x then return null if pos.y < imageY or imageY + scaledHeight < pos.y then return null // Compute the normalized (0..1) coordinates in image space u ::= (pos.x - imageX) / imageScale v ::= (pos.y - imageY) / imageScale return (u, v)
This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 3.0
- How do you convert decimal in C#?
- How do you calculate someone's age based on datetime?
- How can I calculate relative time using a C# language?
- If a query result set is filled in LINQ with data then fill the data set or the table with data. This example is just that.
- How do I get a distinct, ordered list of names from a data table using LINQ?
- Decoding T-SQL CAST in C#/VB.NET.
- Do you like compression of files? How do you decompress them?
- How can I print an HTML document from a web service?
- What is the catch all algorithm?
- How do you sort a dictionary by value?
- WinForms ComboBox data binding gotcha WinForms combobox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha WinForms ComboBox data binding gotcha gotcha
- What is the difference between Int and Integer in Java and C#?
- How to change version number on iOS?
- How can I create an object instance from a Type?
- How do I express the left join with aggregate SQL as LINQ query?