I have a block of code which is executed on a C# Windows Form upon Mouse_Up. It basically zooms in on a rendered image and keeps zooming in each time the rubber band is drawn. I want to reverse this by adding a checkbox (which I have already done) and if the check box is ticked, do zooming out instead from the current view as per drawing of the rubber band. I thought it would be as simple as using the opposite operands i.e "+" instead of a "-" but it won't work. I have posted the event code below. There is a conditional IF on there for changing of the checkbox state. The first lot work fine with zooming in. The section after the ELSE IF doesn't let me zoom out - hence I have just left repeated code there for someone to look at. Thanks
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (checkBox1.Checked == false)
{
int z, w;
this.Cursor = Cursors.Arrow;
// Set internal flag to know we no longer "have the mouse".
weHaveMouse = false;
// If we have drawn previously, draw again in that spot
// to remove the lines.
if (ptLast.X != -1)
{
toolStripStatusLabel1.Text = "Please click and drag to zoom into the Fractal";
Point ptCurrent = new Point(e.X, e.Y);
MyDrawReversibleRectangle(ptOriginal, ptLast);
}
// Set flags to know that there is no "previous" line to reverse.
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
//e.consume();
if (action)
{
xe = e.X;
ye = e.Y;
if (xs > xe)
{
z = xs;
xs = xe;
xe = z;
}
if (ys > ye)
{
z = ys;
ys = ye;
ye = z;
}
w = (xe - xs);
z = (ye - ys);
if ((w < 2) && (z < 2)) initvalues();
else
{
if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy);
else xe = (int)((float)xs + (float)z * xy);
xende = xstart + xzoom * (double)xe;
yende = ystart + yzoom * (double)ye;
xstart += xzoom * (double)xs;
ystart += yzoom * (double)ys;
}
xzoom = (xende - xstart) /(double)x1;
yzoom = (yende - ystart) / (double)y1;
mandelbrot();
rectangle = false;
//Refresh();
}
}
else if (checkBox1.Checked == true)
{
int z, w;
this.Cursor = Cursors.Arrow;
// Set internal flag to know we no longer "have the mouse".
weHaveMouse = false;
// If we have drawn previously, draw again in that spot
// to remove the lines.
if (ptLast.X != -1)
{
toolStripStatusLabel1.Text = "Please click and drag to zoom into the Fractal";
Point ptCurrent = new Point(e.X, e.Y);
MyDrawReversibleRectangle(ptOriginal, ptLast);
}
// Set flags to know that there is no "previous" line to reverse.
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
//e.consume();
if (action)
{
xe = e.X;
ye = e.Y;
if (xs > xe)
{
z = xs;
xs = xe;
xe = z;
}
if (ys > ye)
{
z = ys;
ys = ye;
ye = z;
}
w = (xe - xs);
z = (ye - ys);
if ((w < 2) && (z < 2)) initvalues();
else
{
if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy);
else xe = (int)((float)xs + (float)z * xy);
xende = xstart + xzoom * (double)xe;
yende = ystart + yzoom * (double)ye;
xstart += xzoom * (double)xs;
ystart += yzoom * (double)ys;
}
xzoom = (xende - xstart) / (double)x1;
yzoom = (yende - ystart) / (double)y1;
mandelbrot();
rectangle = false;
//Refresh();
}
}
}
First of all, in my humble opinion, there is no need to draw rubber band when you are zooming out. Just a click where the zoomed out image will be centered. Nevertheless, I cannot see the difference in the code you provided between the code for zooming in and zooming out. In general, the simplest thing to do is to zoom out the image in a way that current center of the image remains at its place and handle the image drawing in a way that X and Y coordinates are divided by 2 and Width and Height are multiplied by 2.
In case of your code, calculating something like this at the end of code could maybe do the trick:
xzoom = 1/xzoom;
yzoom = 1/yzoom;
Related
I'm moving control(label or image) success in PictureBox. When I move, it will save control position(x, y).
Like this:
But problem is: the image result is:
Before gif image, I was drag and drop a label control in center screen. But result image doesn't save label in center screen.
I was set PictureBox attribute to StretchImage.
My code to get position and DrawText in PictureBox like:
public PositionControl CtrlPos = new PositionControl();
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Control control = (Control)sender;
Point nextPosition = new Point();
nextPosition = picPreview.PointToClient(MousePosition);
nextPosition.Offset(mouseX, mouseY);
control.Location = nextPosition;
CtrlPos.x = nextPosition.X;
CtrlPos.y = nextPosition.Y;
Invalidate();
}
}
My code is parent control(PictureBox) contain all control.
In my class, I'm using this:
private void picPreview_MouseMove(object sender, MouseEventArgs e)
{
if (SelectedControl != null && e.Button == MouseButtons.Left)
{
timer1.Stop();
Invalidate();
if (SelectedControl.Height < 20)
{
SelectedControl.Height = 20;
direction = Direction.None;
Cursor = Cursors.Default;
return;
}
else if (SelectedControl.Width < 20)
{
SelectedControl.Width = 20;
direction = Direction.None;
Cursor = Cursors.Default;
return;
}
//get the current mouse position relative the the app
Point pos = picPreview.PointToClient(MousePosition);
#region resize the control in 8 directions
if (direction == Direction.NW)
{
//north west, location and width, height change
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(SelectedControl.Size.Width - (newLocation.X - SelectedControl.Location.X),
SelectedControl.Size.Height - (newLocation.Y - SelectedControl.Location.Y));
SelectedControl.Location = newLocation;
CtrlPos.x = newLocation.X;
CtrlPos.y = newLocation.Y;
SelectedControl.Size = newSize;
}
else if (direction == Direction.SE)
{
//south east, width and height change
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(SelectedControl.Size.Width + (newLocation.X - SelectedControl.Size.Width - SelectedControl.Location.X),
SelectedControl.Height + (newLocation.Y - SelectedControl.Height - SelectedControl.Location.Y));
SelectedControl.Size = newSize;
}
else if (direction == Direction.N)
{
//north, location and height change
newLocation = new Point(SelectedControl.Location.X, pos.Y);
newSize = new Size(SelectedControl.Width,
SelectedControl.Height - (pos.Y - SelectedControl.Location.Y));
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
else if (direction == Direction.S)
{
//south, only the height changes
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(SelectedControl.Width,
pos.Y - SelectedControl.Location.Y);
SelectedControl.Size = newSize;
}
else if (direction == Direction.W)
{
//west, location and width will change
newLocation = new Point(pos.X, SelectedControl.Location.Y);
newSize = new Size(SelectedControl.Width - (pos.X - SelectedControl.Location.X),
SelectedControl.Height);
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
else if (direction == Direction.E)
{
//east, only width changes
newLocation = new Point(pos.X, pos.Y);
newSize = new Size(pos.X - SelectedControl.Location.X,
SelectedControl.Height);
SelectedControl.Size = newSize;
}
else if (direction == Direction.SW)
{
//south west, location, width and height change
newLocation = new Point(pos.X, SelectedControl.Location.Y);
newSize = new Size(SelectedControl.Width - (pos.X - SelectedControl.Location.X),
pos.Y - SelectedControl.Location.Y);
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
else if (direction == Direction.NE)
{
//north east, location, width and height change
newLocation = new Point(SelectedControl.Location.X, pos.Y);
newSize = new Size(pos.X - SelectedControl.Location.X,
SelectedControl.Height - (pos.Y - SelectedControl.Location.Y));
SelectedControl.Location = newLocation;
SelectedControl.Size = newSize;
}
#endregion
}
}
Draw image using CtrlPost(x, y):
g.DrawImage(
DrawText(image, new Font(cbxFont.Text, fontSize), colorInput,
Color.Transparent),
new Point(CtrlPos.x, CtrlPos.y));
// g is Graphics object.
Updated:
I was using code to add label1 to pictureBox1 like this:
pictureBox1.Controls.Add(label1);
If your PictureBox is in Sizemodes StretchImage or Zoom then the pixels are scaled; the Label's Location however is not. So you would have the calculate the position where to draw:
PointF stretched(Point p0, PictureBox pb)
{
if (pb.Image == null) return PointF.Empty;
float scaleX = 1f * pb.Image.Width / pb.ClientSize.Width;
float scaleY = 1f * pb.Image.Height / pb.ClientSize.Height;
return new PointF(p0.X * scaleX, p0.Y * scaleY);
}
You would call it as PointF p1 = stretched(p0, pictureBox1);
You would draw maybe like this:
g.DrawImage( DrawText(image, new Font(cbxFont.Text, fontSize),
colorInput, Color.Transparent),
Point.Round(stretched( CtrlPos.Location, picPreview));
If you also want to correct the size you can use a similar function..
If the SizeMode is CenterImage the pixels are not scaled but most likely transposed and a correction is necessary as well.
For the other direction simply switch denominator and numerator in the fractions!
Please see this picture:
I need to move by mouse one of the two points along an arc of 90°
Please have in mind that I can rotate the Arrow and the whole thing will rotate as well.
What I did:
public override bool HandleMouseMove(MouseEventArgs e)
{
double angleRad = 0;
double new_cx = 0;
double new_cY = 0;
double currentAngle = Arrow.Rotation.Angle;
Point pt = TMEMControl.ScreenToWorld(new Point(e.X, e.Y));
Point center = TMEMControl.ScreenToWorld(new Point(Arrow.Translation.X, Arrow.Translation.Y));
Point c = new Point(center.X + 25, center.Y);
angleRad = (-currentAngle) * (Math.PI / 180);
new_cx = center.X + Math.Cos(angleRad) * 25;
new_cY = center.Y + Math.Sin(angleRad) * 25;
c = new Point(new_cx, new_cY);
Vector CenterPt = new Vector((pt.X-center.X),(pt.Y-center.Y));
Vector CenterC = new Vector((c.X - center.X),(c.Y - center.Y));
fieldOfView = Vector.AngleBetween(CenterPt, CenterC);
if (fieldOfView >= 90) fieldOfView = 90;
if (fieldOfView <= 0) fieldOfView = 0;
TMEMControl.Settings.VideoOverlayHelper.FieldOfView = (float)SmartFrame.Basic.BaseAngle.Deg2Rad(fieldOfView*2);
updateFieldofView = (float)fieldOfView;
if (updateFieldofView > 87) updateFieldofView = 87;
if (updateFieldofView < 0) updateFieldofView = 0;
return true;
}
public void Reset(int id, double x, double y, double angle)
{
double angleInRadians = 0;
Point center = new Point(x + 15, y + 20);
Point refP = new Point(x + 15, (y - 80));
distance = Point.Subtract(refP, center).Length;
double new_x = 0;
double new_y = 0;
ControlPoint cpi = CameraCtrl.getCP(id);
if (null != cpi)
{
switch (id)
{
case 0:
cpi.point = new Point(center.X, center.Y);
cpi.state = ControlPoint.States.Selectable;
break;
case 1:
cpi.point = new Point(x + 15, (y + 80));
angleInRadians = (float)SmartFrame.Basic.BaseAngle.Deg2Rad((angle + (87 - updateFieldofView)));
new_x = center.X + Math.Cos(-angleInRadians) * distance;
new_y = center.Y + Math.Sin(-angleInRadians) * distance;
cpi.point = new Point(new_x, new_y);
cpi.state = ControlPoint.States.Fixed;
break;
case 2:
cpi.point = new Point(x + 15, (y - 80));
angleInRadians = (float)SmartFrame.Basic.BaseAngle.Deg2Rad((angle + (180 - (87 - updateFieldofView))));
new_x = center.X + Math.Cos(-angleInRadians) * distance;
new_y = center.Y + Math.Sin(-angleInRadians) * distance;
cpi.point = new Point(new_x, new_y);
cpi.state = ControlPoint.States.Selectable;
break;
}
}
}
It's working 90% the problem is that once I snap/click on one of the red dots and move the mouse, the red dot is always few degrees ahead from the the mouse cursor. and I need to move it with the cursor Position.
And just for the info why I set always the angle to max 87°, is because I want to keep a little distance between the Arrow and my two lines.
i am trying to zoom in and zoom out on a content page using xamarin.forms.
I am able zoom in and zoom out but the problem is scrolling is not working.
i want zoom an image. with this code zooming is working perfectly. But while zooming i am not able to see full image. i must scroll to view the rest of the image. for that i need to scroll. but scrolling is not working.
XAML
xmlns:helper="clr-namespace:KPGTC.Deals.Mobile.Helpers"
<helper:PinchToZoomContainer>
<helper:PinchToZoomContainer.Content>
<Image x:Name="img_Popup"/>
</helper:PinchToZoomContainer.Content>
</helper:PinchToZoomContainer>
Code:
public class PinchToZoomContainer : ContentView
{
double MIN_SCALE = 1;
double MAX_SCALE = 4;
double startScale = 1;
double currentScale = 1;
double xOffset = 0;
double yOffset = 0;
bool _isActive = false;
public PinchToZoomContainer()
{
DependencyService.Get<IHelpers>().ShowAlert("Double-tap to zoom");
//var _pinchGesture = new PinchGestureRecognizer();
//_pinchGesture.PinchUpdated += OnPinchUpdated;
//GestureRecognizers.Add(_pinchGesture);
var _tapGesture = new TapGestureRecognizer { NumberOfTapsRequired = 2 };
_tapGesture.Tapped += On_Tapped;
GestureRecognizers.Add(_tapGesture);
var _panGesture = new PanGestureRecognizer();
_panGesture.PanUpdated += OnPanUpdated;
GestureRecognizers.Add(_panGesture);
TranslationX = 0;
TranslationY = 0;
_isActive = false;
}
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (_isActive)
{
if (e.TotalX > 0)
{
if (e.TotalX > 2)
{
TranslationX += 15;
}
}
else
{
if (e.TotalX < -2)
{
TranslationX -= 15;
}
}
}
}
private void On_Tapped(object sender, EventArgs e)
{
if (Scale > MIN_SCALE)
{
_isActive = false;
this.ScaleTo(MIN_SCALE, 250, Easing.CubicInOut);
this.TranslateTo(0, 0, 250, Easing.CubicInOut);
}
else
{
_isActive = true;
AnchorX = AnchorY = 0.5;
this.ScaleTo(MAX_SCALE, 250, Easing.CubicInOut);
}
}
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (e.Status == GestureStatus.Started)
{
startScale = Content.Scale;
Content.AnchorX = 0;
Content.AnchorY = 0;
}
if (e.Status == GestureStatus.Running)
{
currentScale += (e.Scale - 1) * startScale;
currentScale = Math.Max(1, currentScale);
double renderedX = Content.X + xOffset;
double deltaX = renderedX / Width;
double deltaWidth = Width / (Content.Width * startScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
double renderedY = Content.Y + yOffset;
double deltaY = renderedY / Height;
double deltaHeight = Height / (Content.Height * startScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);
Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0);
Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0);
Content.Scale = currentScale;
}
if (e.Status == GestureStatus.Completed)
{
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
}
}
}
Alright, this was quite a tough one and admittedly I don't understand fully how I made it, but I made it.
Some thoughts:
You mixed the translation of the container and the content, which is quite tricky to handle - if this is possible at all
When panning, you added 15 every time the pan event was raised, but there is a better way: Just store the initial offset of the content and then add the TotalX and TotalY respectively to the TranslationX and the TranslationY of the content (this was the easy part)
Panning while zooming was quite hard to get right and I had to find it out by trial and error
Basically you have to store the origin of the pinch gesture when the gesture starts and calculate the diff between the original origin and the current origin
Then you have to add the diff (multiplied by the with and height respectively of the control) to the target translation
Here is the code for the panning:
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (e.StatusType == GestureStatus.Started)
{
this.xOffset = this.Content.TranslationX;
this.yOffset = this.Content.TranslationY;
}
if (e.StatusType != GestureStatus.Completed
&& e.StatusType != GestureStatus.Canceled)
{
this.Content.TranslationX = this.xOffset + e.TotalX;
this.Content.TranslationY = this.yOffset + e.TotalY;
}
if (e.StatusType == GestureStatus.Completed)
{
this.xOffset = this.Content.TranslationX;
this.yOffset = this.Content.TranslationY;
}
}
And here for the pinching
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (e.Status == GestureStatus.Started)
{
this.startScale = this.Content.Scale;
this.Content.AnchorX = 0;
this.Content.AnchorY = 0;
this.startScaleOrigin = e.ScaleOrigin;
}
if (e.Status == GestureStatus.Running)
{
var originDiff = PinchToZoomContainer.CalculateDiff(e.ScaleOrigin, this.startScaleOrigin);
this.currentScale += (e.Scale - 1) * this.startScale;
this.currentScale = Math.Max(1, this.currentScale);
double renderedX = this.Content.X + this.xOffset;
double deltaX = renderedX / this.Width;
double deltaWidth = this.Width / (this.Content.Width * this.startScale);
double originX = (this.startScaleOrigin.X - deltaX) * deltaWidth;
double renderedY = this.Content.Y + this.yOffset;
double deltaY = renderedY / this.Height;
double deltaHeight = this.Height / (this.Content.Height * this.startScale);
double originY = (startScaleOrigin.Y - deltaY) * deltaHeight;
double targetX = this.xOffset - ((originX) * this.Content.Width) * (this.currentScale - this.startScale) - originDiff.X * this.Content.Width;
double targetY = this.yOffset - ((originY) * this.Content.Height) * (this.currentScale - this.startScale) - originDiff.Y * this.Content.Height;
this.Content.TranslationX = targetX.Clamp(-this.Content.Width * (this.currentScale - 1), 0);
this.Content.TranslationY = targetY.Clamp(-this.Content.Height * (this.currentScale - 1), 0);
this.Content.Scale = this.currentScale;
}
if (e.Status == GestureStatus.Completed)
{
this.xOffset = this.Content.TranslationX;
this.yOffset = this.Content.TranslationY;
}
}
(Of course you have to add Point startScaleOrigin to your class).
Finally you need the method to calculate the distance between two points
private static Point CalculateDiff(Point first, Point second)
{
return second.Offset(-first.X, -first.Y);
}
Unfortunately I did not manage to get the tapping right, but I think you should be able to figure it out from here.
private void picZoom_MouseMove(object sender, MouseEventArgs e)
{
if (PicImageClicked == false)
{
if (checkBox1.Checked)
{
int x = e.X + picZoom.Left - picImage.Left;
int y = e.Y + picZoom.Top - picImage.Top;
if (x <= picImage.Width && y <= picImage.Height &&
x <= picImage.Top && y <= picImage.Left)
{
MouseEventArgs e2 = new MouseEventArgs(MouseButtons.None, 0, x, y, 0);
picImageReposition(null, e2);
}
}
else
{
UpdateZoomedImage(e);
}
}
}
picImage is a bigger pictureBox and picZoom is a smaller pictureBox the picZoom i'm moving aorund inside the picImage are with the mouse.
This condition:
if (x <= picImage.Width && y <= picImage.Height
Is working if i'm moving the picZoom to the left border or the bottom border it stop on it and not continue.
But the second condition:
x <= picImage.Top && y <= picImage.Left
Is not working good it make everything slow and it's not stopping on the left or top borders.
I want to make a condition/s so the picZoom will stay in the picImage area borders.
What i tried now is:
private void picZoom_MouseMove(object sender, MouseEventArgs e)
{
Point pnt;
int x, y;
if (MouseButtons.Left == e.Button)
{
pnt = picZoom.PointToScreen(e.Location);
pnt = this.PointToClient(pnt);
x = pnt.X - mouseDown.X;
y = pnt.Y - mouseDown.Y;
if (x < picImage.Left)
{
x = picImage.Left;
}
else if (x + picZoom.Width > picImage.Left + picImage.Width)
{
x = picImage.Left + picImage.Width - picZoom.Width;
}
else
{ }
if (y < picImage.Top)
{
y = picImage.Top;
}
else if (y + picZoom.Height > picImage.Top + picImage.Height)
{
y = picImage.Top + picImage.Height - picZoom.Height;
}
else
{ }
picZoom.Location = new Point(x, y);
if (PicImageClicked == false)
{
if (checkBox1.Checked)
{
MouseEventArgs e2 = new MouseEventArgs(MouseButtons.None, 0, x, y, 0);
picImageReposition(null, e2);
}
else
{
UpdateZoomedImage(e);
}
}
}
}
picImageReposition is:
private void picImageReposition(object sender, MouseEventArgs e)
{
// If no picture is loaded, return
if (picImage.Image == null)
return;
if (PicImageClicked == false)
{
picZoom.BringToFront();
picZoom.Left = e.X + picImage.Left - picZoom.Width/2;
picZoom.Top = e.Y + picImage.Top - picZoom.Height/2;
UpdateZoomedImage(e);
}
}
And UpdateZoomedImage is:
private void UpdateZoomedImage(MouseEventArgs e)
{
// Calculate the width and height of the portion of the image we want
// to show in the picZoom picturebox. This value changes when the zoom
// factor is changed.
int zoomWidth = picZoom.Width / _ZoomFactor;
int zoomHeight = picZoom.Height / _ZoomFactor;
// Calculate the horizontal and vertical midpoints for the crosshair
// cursor and correct centering of the new image
int halfWidth = zoomWidth / 2;
int halfHeight = zoomHeight / 2;
// Create a new temporary bitmap to fit inside the picZoom picturebox
tempBitmap = new Bitmap(zoomWidth, zoomHeight, PixelFormat.Format24bppRgb);
// Create a temporary Graphics object to work on the bitmap
Graphics bmGraphics = Graphics.FromImage(tempBitmap);
// Clear the bitmap with the selected backcolor
bmGraphics.Clear(_BackColor);
// Set the interpolation mode
bmGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the portion of the main image onto the bitmap
// The target rectangle is already known now.
// Here the mouse position of the cursor on the main image is used to
// cut out a portion of the main image.
bmGraphics.DrawImage(picImage.Image,
new Rectangle(0, 0, zoomWidth, zoomHeight),
new Rectangle(e.X - halfWidth, e.Y - halfHeight, zoomWidth, zoomHeight),
GraphicsUnit.Pixel);
// Draw the bitmap on the picZoom picturebox
picZoom.Image = tempBitmap;
// Draw a crosshair on the bitmap to simulate the cursor position
bmGraphics.DrawLine(Pens.Black, halfWidth + 1, halfHeight - 4, halfWidth + 1, halfHeight - 1);
bmGraphics.DrawLine(Pens.Black, halfWidth + 1, halfHeight + 6, halfWidth + 1, halfHeight + 3);
bmGraphics.DrawLine(Pens.Black, halfWidth - 4, halfHeight + 1, halfWidth - 1, halfHeight + 1);
bmGraphics.DrawLine(Pens.Black, halfWidth + 6, halfHeight + 1, halfWidth + 3, halfHeight + 1);
// Dispose of the Graphics object
bmGraphics.Dispose();
// Refresh the picZoom picturebox to reflect the changes
picZoom.Refresh();
}
If the formula of moving the picZoom control is what you want eg
int x = e.X + picZoom.Left - picImage.Left;
int y = e.Y + picZoom.Top - picImage.Top;
then the comparison is(x, y is the position in picImage meaning that 0,0 is the left top position):
if(x >= 0 && y >= 0 && x + picZoom.Width <= picImage.Width && y + picZoom.Height <= picImage.Height)
{
...
...
}
EDIT
When you click in picZoom and drag, the control moves with the mouse:
private Point mouseDown;
private void picZoom_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = e.Location;
}
private void picZoom_MouseMove(object sender, MouseEventArgs e)
{
Point pnt;
int x, y;
if (MouseButtons.Left == e.Button)
{
pnt = picZoom.PointToScreen(e.Location);
pnt = this.PointToClient(pnt);
x = pnt.X - mouseDown.X;
y = pnt.Y - mouseDown.Y;
if (x < picImage.Left)
{
x = picImage.Left;
}
else if (x + picZoom.Width > picImage.Left + picImage.Width)
{
x = picImage.Left + picImage.Width - picZoom.Width;
}
else
{ }
if (y < picImage.Top)
{
y = picImage.Top;
}
else if (y + picZoom.Height > picImage.Top + picImage.Height)
{
y = picImage.Top + picImage.Height - picZoom.Height;
}
else
{ }
picZoom.Location = new Point(x, y);
}
}
valter
This is not an answer to your question as much as it is a suggestion. Could you not create a Rect() directly over your picture and simply check if the mouse position is within it's bounds.
Something along the lines of:
Rectangle rect = obj.GetBounds(e.Graphics);
if (!rect.Intersects(e.ClipRectangle))
continue;
(stolen from another post)
So I looked over your code again, and it struck me.
You are comparing an x value to the top bound of your picture and a y value to the left bound...
x <= picImage.Top && y <= picImage.Left
should be
x <= picImage.Left && y <= picImage.Top
That is, if I understand what you are trying to do.
Good luck!
I have a WPF application, wherein I draw rectangles in a canvas.I need to add a functionality in which when i draw a rectangle if there is a rectangle next to it (for eg: suppose first rectangle x coordinate is 236 and second rectangle coordinate is 235) i need to snap the second rectangle x coordinate to 236 as shown in the image.
The snap would be done only if the distance difference is 10.
I have written the following code to do this.
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
startPos = e.GetPosition(Canvas);
System.Windows.Point curPosition = e.GetPosition(SectionCanvas);
rect = new System.Windows.Shapes.Rectangle
{
Stroke = brushColor,
StrokeDashArray = new DoubleCollection { 2, 2 },
Tag = "rectangle"
};
Canvas.SetLeft(rect, startPos.X);
Canvas.SetTop(rect, startPos.X);
SectionCanvas.Children.Add(rect);
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
currentPos = e.GetPosition(SectionCanvas);
var x = Math.Min(currentPos.X, startPos.X);
var y = Math.Min(currentPos.Y, startPos.Y);
var w = Math.Max(currentPos.X, startPos.X) - x;
var h = Math.Max(currentPos.Y, startPos.Y) - y;
rect.Width = w;
rect.Height = h;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
}
private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
if(rect == null)
{
MessageBox.Show("Could not capture section, Please try again");
return;
}
endPos = e.GetPosition(SectionCanvas);
IEnumerable<Rect> coordinates = rectCollection.Select(r => new Rect(Canvas.GetLeft(r), Canvas.GetTop(r), r.Width, r.Height));
Rect newCordinates = new Rect(Canvas.GetLeft(rect), Canvas.GetTop(rect), rect.Width, rect.Height);
if (coordinates.Any(c => c.IntersectsWith(newCordinates)))))
{
MessageBox.Show("New Rectangle intersects with existing rectangle");
Canvas.Children.Remove(rect);
return;
}
rectCollection.Add(rect);
rect = null;
foreach(Point p in tempCollection)
{
if((startPos.X <= (p.X + 10) && startPos.X >= (p.X -10)))
{
startPos.X = p.X;
}
if(endPos.X <= (p.X + 10) && endPos.X >= (p.X - 10))
{
var x1 = Math.Max(endPos.X,p.X) - Math.Min(endPos.X, p.X);
var w1 = startPos.X - x1;
endPos.X = p.X;
startPos.X = w1;
}
if ((startPos.Y <= (p.Y + 10) && startPos.Y >= (p.Y - 10)))
{
startPos.Y = p.Y;
}
if (endPos.Y <= (p.Y + 10) && endPos.Y >= (p.Y - 10))
{
var x1 = Math.Max(endPos.Y, p.Y) - Math.Min(endPos.Y, p.Y);
var w1 = startPos.Y - x1;
endPos.Y = p.Y;
}
}
var x = Math.Min(currentPos.X, startPos.X);
var y = Math.Min(currentPos.Y, startPos.Y);
var w = Math.Max(currentPos.X, startPos.X) - x;
var h = Math.Max(currentPos.Y, startPos.Y) - y;
rect.Width = w;
rect.Height = h;
rect.Stroke = Brushes.Coral;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
rect = null;
tempCollection.Add(startPos);
tempCollection.Add(endPos);
}
The above code doesnt work when I am changing the endpoints values. While debugging I can see that the end point value changes but the rectangle drawn doesn't change. I am not able to find out what I am doing wrong.
I figured out the answer which was a dumb mistake from my end.
While calculating the width and height after snapping, I am using currentPos, instead I should use endPos.
var x = Math.Min(endPos.X, startPos.X);
var y = Math.Min(endPos.Y, startPos.Y);
var w = Math.Max(endPos.X, startPos.X) - x;
var h = Math.Max(endPos.Y, startPos.Y) - y;