Graphics/Bitmap Limits? - c#

Im having some weird problems with Graphics and Bitmap.
I have a Graphics Object that is displayed on a PictureBox and im capturing the MouseMove and MouseClick Events that give X and Y Position of the Mouse on the Image but if the Y Position goes Bigger then 32775 it then goes into Negatives which means everything breaks. And if the Image is Bigger then 65535 it then stops displaying the Image.
Any Ideas how these problems can be fixed?
Thanks
Example Code:
http://pastebin.com/YEX0XD1q
Just Click Make 10,000 Bigger about 4 times then scroll down and on the right it will show the mouse X and Y position and as you move down through the image and hover over the Red Area if you go down enough it will go into Negative Y.

By Researching i've managed to solve my problem
Dont Use PictureBox inside Panel, Instead use ScrollableControl. This will fix the Problem where i couldn't make something bigger then 65K Height.
Use GetVirtualMouseLocation to get Virtual Mouse Positions
I Just used what was posted here
The Above Works Perfectly.

Related

How can i calculate the right coordinates?

I need help. I have a Picturebox and would now like to calculate given coordinates on the picture and then play them in the label. How can I do that best?
As seen in the picture.
If you then click on the image on it, then the data is entered in a list box.
Thank you for your help.
My Picture here: https://prnt.sc/puxyu6
In WPF this particular might for once be the hardest. WPF/UWP is designed for the MVVM, and I do not know anyone but beginners that programm outside of MVVM. And I can not think of way to do this with MVVM.
PictureBox is also the WinForms Element. The WPF equivalent is called Image.
Navigation aids like this are not a trivial thing. One reason there are so few of it. But it comes down to few step process:
Get the x and y pixel coordinates that was clicked, also in relation to that the overall display size of the Image. Usually the MouseClick Event would be the tool for that, but I can not find it. MouseDown or LeftMouseDown are the closest events.
If the entire image was shown with no zooming or cropping, it is now just simple math. If it was 20% of the X axis and 21% of the Y axis, it is pretty easy to figure out where 20% of X and 21 of Y is on the SourceImage.
If there was any zoom or crop, this has to be taken into account, otherwise it is 2.
Equate the Image Pixel position with the coordinates you know for it.
Part 1 would look like this and needs to be registered with the MouseDown or LeftMouseDown Event of the Image:
private void ContentControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//Unpack the sender
Image source = (Image)Sender;
//Get the click point
Point clickPoint = e.GetPosition(source);
//There is more then 1 height Property in WPF.
//The Actuall one is what you are looking for
//Unfortunately you get doubles for this and int for the other. Consider general advise regarding double math
double ElementHeight = source.ActualHeight;
double ElementWidth = source.ActualWidth;
//Do what you need to find the relative position
//Part 2
}
Hopefully someone else can give you a better answer.

Crop image from X and Y?

var ssbytearray = ((ITakesScreenshot)excelSession).GetScreenshot().AsByteArray;
var screenshot = new Bitmap(new MemoryStream(ssbytearray));
var croppedScreenshot = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
screenshot = screenshot.Clone(croppedScreenshot, screenshot.PixelFormat);
SaveScreenShot(saveLocation, screenshot);
In my code, I'm currently taking a screenshot of a Microsoft Office application that I called, "element" and it saves the image as its meant to. Unfortunately if I want to crop the image from the top or left side, it isn't as easy as replacing the X and Y coordinates with a number when creating the Rectangle. If I do try to change the X or Y coordinate with a value I get a SystemOutOfMemory exception. However if I want to crop it from the width and height and I try to change the value I'm able to.
I'd like to be able to crop the screenshot taken from the top and left side. Currently I'm only able to crop the screenshot from the bottom or right side using the width & height.
Any help's appreciated.
I solved this issue by changing the x, y, height & width values when creating the Rectangle. It will produce a System.OutOfMemory exception if the values you create the Rectangle with are bigger than the element you're trying to capture.
However this causes problems when changing resolutions. Currently trying to solve that issue now.
EDIT: To fix the second issue I came across I am changing the resolution of the system before & after the test runs so it can run on a resolution that's common on most systems, then change it back to its original.

How to move a rectangle that's used as a Meter bar in unity

I want to make a vertical meter for values between 2 ranges.
so what i tried so far, was to make a frame, and under it put a mask, and under that a rectangle.
with the idea, of lowering/increasing the rectangle Y position to make it seems to move.
rect.position = new Vector3(rect.position.x, (rect.position.y - rect.sizeDelta.y));
so i did that, to first lower by it's height, so the bar will seem empty.
but it doesn't work right, the position depends on the screen size, and the rect Y end up way lower then exactly his height.
so, tl;dr, i want a bar that i can give a value between 0-100 and it will fill it according to that.
and i might have gone wrong about it.
so, any suggestions would be appreciated.
edit:
Add picture to show what i want the bar to look like:
Well i feel kinda dumb
I didn't realize how customizable the Slider control is.
I removed it's handle, unchecked interactble and removed it's background, and now i can give it a value between 0-100 and it works exactly as i wanted.
thanks Mathias Siig Nørregaard for referencing me to the video link that opened my eyes.

16 bit mouse click limit on picturebox

This is a C# question.
I have a form. On the form I have a panel. On the panel I have a picture box with a very large scrollable bitmap (Y >> 32768). The picture box click event does not fire when the picture box Y is greater than 32767. I understand that mouse clicks are limited to 16 bits.
Is there way to get a mouse click of the underlying panel so I can compute Y from the scroll bar positon and the y coordinate of the panel?
Did you try another approach, such as "downscale" the bitmap to 32k size (or less) before to display it rather to display it in full size ? You could get the real X / Y position by multiply its value by the downscaling factor.
It don't know if there is an easy way to cross the 32k barrier but even if you could, I guess that the UI will be very slow according the size of your picture...

Snake in C# - player can scale window - dividing manual-scaled window into identical squares

As part of my self-education of programming I decided to make a snake in C#. The problem I have is about the client size of the game form.
I want player to be able to scale the window of the game, which is divided into 25 x 25 grid (every coordinate is like "one pixel") - it means, that at any moment, the window is divided into 25 x 25 identical squares.
The problem I get is near this code:
int SquareSide = (ClientSize.Width / 25);
When I set the ClientSize strictly to for example 600 x 600, which is a multiple of 25, everything goes OK. But when I don't (for example by manual resizing, which can change the size to, for example, 711 x 711), it creates at the right side a 'strip', which seems to be the rest of pixels, which can't be used because we are dividing to integer number.
My question is - is there any not-extremely-hard way to achieve dividing ANY client size of form into 25 x 25 grid without this problem? I tried using double, but FillRectangle method doesn't accept it.
I hope my question is understandable and thank you for replies.
To tell you the truth, there is
g.FillRectangle(Brush b, RectangleF rect)
RectangleF is a rectangle whose coordinates are float.
So you can use:
float SquareSide = (ClientSize.Width * 1f / 25);
I would try to simply handle OnResize event of the window, and at the moment user finishes rezise it, force the size that program need for perfect fit of the grid.
So for example at the moment user releases mouse and you figure out that one dimensions of the window 711x711, bring it to nearest correct fit 700x700.
In this way you guarantee good user experience on different monitor resolutions and for you guarantee a correct fit of the grid you draw.
Hope this helps.
Switch to float coordinates for everything. Make sure your game looks OK when lines do not have whole pixel coordinates. Make sure you "find next cell" code also works with floats, including mouse position detection if needed.
The other approach is to be happy with integer coordinates (and cells of the same size) and make page layout flexible to accomodate some unused space for odd 711x711 layouts (i.e. just center the field and keep some variable width border).

Categories