protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); Cursor = Cursors.Default; _edges = ResizeModes.None; Refresh(); }
private void _control_MouseLeave(object sender, EventArgs e) { Control c = (Control)sender; c.Cursor = Cursors.Default; _edges = ResizeModes.None; c.Refresh(); }
/// <summary> /// This method will resize the in-memory image using a fixed ratio /// </summary> private void ImageSizeChanged(ResizeModes resizeMode) { var image = ImagePreview.Source as BitmapSource; if (image == null) { return; } Dispatcher.InvokeAsync(() => { if (ImageHeight == (int)image.Height && ImageWidth == (int)image.Width) { return; } using (var bitmap = WindowUtilities.BitmapSourceToBitmap(ImagePreview.Source as BitmapSource)) { if (bitmap == null) { StatusBar.ShowStatusError("No image to resize."); return; } if (ImageWidth != bitmap.Width && ImageHeight != bitmap.Height) { return; } Bitmap bitmap2; using (bitmap2 = ImageResizer.ResizeImageByMode(bitmap, ImageWidth, ImageHeight, resizeMode)) { if (bitmap2 != null) { Debug.WriteLine($"ImageSizeChanged from: {ImageWidth} x {ImageHeight} to: {bitmap2.Width} x {bitmap2.Height}"); ImageWidth = bitmap2.Width; ImageHeight = bitmap2.Height; var bmpSource = WindowUtilities.BitmapToBitmapSource(bitmap2); WindowUtilities.DoEvents(); ImagePreview.Source = bmpSource; Dispatcher.InvokeAsync(() => ResizeImagePreviewControl(bmpSource), DispatcherPriority.ApplicationIdle); } } } }, DispatcherPriority.ApplicationIdle); }
public void Resize(short width, short height, ResizeModes mode, bool shrinkBuffer = true) { NotifyMapResizing(); int newBufferLength; switch (mode) { case ResizeModes.Buffer: newBufferLength = width * height; if (CurrentBufferSize != newBufferLength) { if (CurrentBufferSize < newBufferLength) { Tiles.AddRange(new byte[newBufferLength - CurrentBufferSize]); } else if (shrinkBuffer) { Tiles.RemoveRange(newBufferLength, CurrentBufferSize - newBufferLength); } } break; case ResizeModes.Logical: if (width != Width) { if (width < Width) { for (int row = 0; row < Height; row++) { Tiles.RemoveRange((row * width) + width, Width - width); } } else { var diff = new byte[width - Width]; for (int row = 0; row < Height; row++) { Tiles.InsertRange((row * width) + Width, diff); } } } if (height != Height) { newBufferLength = width * height; //any bytes after this point were not visible before this resize (or don't exist yet) var hiddenStart = width * Height; //any bytes after this point are still not visible (or don't exist yet) var hiddenEnd = Math.Min(newBufferLength, CurrentBufferSize); //if the buffer size needs to change... if (newBufferLength != CurrentBufferSize) { //add visible bytes if (CurrentBufferSize < newBufferLength) { Tiles.AddRange(new byte[newBufferLength - CurrentBufferSize]); } //remove non-visible bytes else if (shrinkBuffer) { Tiles.RemoveRange(newBufferLength, CurrentBufferSize - newBufferLength); } } //clear any previously hidden bytes for (int i = hiddenStart; i < hiddenEnd; i++) { Tiles[i] = 0x00; } } break; } Width = width; Height = height; NotifyMapResized(); }
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) { base.OnMouseMove(e); if (_mouseDown && _edges != ResizeModes.None && Focused) { int x = 0; int y = 0; int width = 0; int height = 0; SuspendLayout(); switch (_edges) { case ResizeModes.Move: x = Math.Max(Math.Min(Left + e.X - _mouseOrigin.X, Parent.Width - Width), 0); y = Math.Max(Math.Min(Top + e.Y - _mouseOrigin.Y, Parent.Height - Height), 0); SetBounds(x, y, Width, Height); break; case ResizeModes.Top: y = Math.Max(Math.Min(Top + e.Y, Parent.Height - Height), 0); height = Math.Max(Math.Min(Height - e.Y, _controlOrigin.Height + _controlOrigin.Y), 0); SetBounds(Left, y, Width, height); break; case ResizeModes.Right: width = Math.Max(Math.Min(Width - (Width - e.X), Parent.Width - _controlOrigin.X), 0); SetBounds(Left, Top, width, Height); break; case ResizeModes.Bottom: height = Math.Max(Math.Min(Height - (Height - e.Y), Parent.Height - _controlOrigin.Y), 0); SetBounds(Left, Top, Width, height); break; case ResizeModes.Left: x = Math.Max(Math.Min(Left + e.X, Parent.Width - Width), 0); width = Math.Max(Math.Min(Width - e.X, _controlOrigin.Width + _controlOrigin.X), 0); SetBounds(x, Top, width, Height); break; case ResizeModes.TopLeft: x = Math.Max(Math.Min(Left + e.X, Parent.Width - Width), 0); y = Math.Max(Math.Min(Top + e.Y, Parent.Height - Height), 0); width = Math.Max(Math.Min(Width - e.X, _controlOrigin.Width + _controlOrigin.X), 0); height = Math.Max(Math.Min(Height - e.Y, _controlOrigin.Height + _controlOrigin.Y), 0); SetBounds(x, y, width, height); break; case ResizeModes.TopRight: y = Math.Max(Math.Min(Top + e.Y, Parent.Height - Height), 0); width = Math.Max(Math.Min(Width - (Width - e.X), Parent.Width - _controlOrigin.X), 0); height = Math.Max(Math.Min(Height - e.Y, _controlOrigin.Height + _controlOrigin.Y), 0); SetBounds(Left, y, width, height); break; case ResizeModes.BottomRight: width = Math.Max(Math.Min(Width - (Width - e.X), Parent.Width - _controlOrigin.X), 0); height = Math.Max(Math.Min(Height - (Height - e.Y), Parent.Height - _controlOrigin.Y), 0); SetBounds(Left, Top, width, height); break; case ResizeModes.BottomLeft: x = Math.Max(Math.Min(Left + e.X, Parent.Width - Width), 0); width = Math.Max(Math.Min(Width - e.X, _controlOrigin.Width + _controlOrigin.X), 0); height = Math.Max(Math.Min(Height - (Height - e.Y), Parent.Height - _controlOrigin.Y), 0); SetBounds(x, Top, width, height); break; } Invalidate(); ResumeLayout(); if (MouseMoved != null) { MouseMoved(Left, Top, Width, Height, e.X, e.Y); } } else { if (Focused) { if (_allowedEdges == ResizeModes.Move && e.X >= 0 && e.Y <= Width && e.Y >= 0 && e.Y <= Height) { // Move Cursor = Cursors.SizeAll; _edges = ResizeModes.Move; } else if (e.X >= _gripWidth && e.X <= Width - _gripWidth && e.Y >= _gripWidth && e.Y <= Height - _gripWidth) { // Move Cursor = Cursors.SizeAll; _edges = ResizeModes.Move; } else if (e.X <= _gripWidth && e.Y <= _gripWidth) { // TopLeft Cursor = Cursors.SizeNWSE; _edges = ResizeModes.TopLeft; } else if (e.X >= Width - _gripWidth && e.Y <= _gripWidth) { // TopRight Cursor = Cursors.SizeNESW; _edges = ResizeModes.TopRight; } else if (e.X >= Width - _gripWidth && e.Y >= Height - _gripWidth) { // BottomRight Cursor = Cursors.SizeNWSE; _edges = ResizeModes.BottomRight; } else if (e.X <= _gripWidth && e.Y >= Height - _gripWidth) { // BottomLeft Cursor = Cursors.SizeNESW; _edges = ResizeModes.BottomLeft; } else if (e.Y <= _gripWidth) { // Top Cursor = Cursors.SizeNS; _edges = ResizeModes.Top; } else if (e.X >= Width - _gripWidth) { // Right Cursor = Cursors.SizeWE; _edges = ResizeModes.Right; } else if (e.Y >= Height - _gripWidth) { // Bottom Cursor = Cursors.SizeNS; _edges = ResizeModes.Bottom; } else if (e.X <= _gripWidth) { // Left Cursor = Cursors.SizeWE; _edges = ResizeModes.Left; } else { Cursor = Cursors.Default; _edges = ResizeModes.None; } } _edges = _edges & _allowedEdges; if (_edges == ResizeModes.None) { Cursor = Cursors.Default; } } }
public ResizeablePictureBox(ResizeModes edges, int gripWidth = 4) : base() { _gripWidth = gripWidth; _allowedEdges = edges; }
public Anchor(Control control, ResizeModes edges) { _control = control; _allowedEdges = edges; }
private void _control_MouseMove(object sender, MouseEventArgs e) { Control control = (Control)sender; if (_mouseDown && _edges != ResizeModes.None && control.Focused) { control.SuspendLayout(); switch (_edges) { case ResizeModes.Move: int x = Math.Max(Math.Min(control.Left + e.X - _mouseOrigin.X, control.Parent.Width - control.Width), 0); int y = Math.Max(Math.Min(control.Top + e.Y - _mouseOrigin.Y, control.Parent.Height - control.Height), 0); control.SetBounds(x, y, control.Width, control.Height); break; case ResizeModes.Top: // TODO: Limit all resizes by parent bounds control.SetBounds(control.Left, control.Top + e.Y, control.Width, control.Height - e.Y); break; case ResizeModes.Right: control.SetBounds(control.Left, control.Top, control.Width - (control.Width - e.X), control.Height); break; case ResizeModes.Bottom: control.SetBounds(control.Left, control.Top, control.Width, control.Height - (control.Height - e.Y)); break; case ResizeModes.Left: control.SetBounds(control.Left + e.X, control.Top, control.Width - e.X, control.Height); break; case ResizeModes.TopLeft: control.SetBounds(control.Left + e.X, control.Top + e.Y, control.Width - e.X, control.Height - e.Y); break; case ResizeModes.TopRight: control.SetBounds(control.Left, control.Top + e.Y, control.Width - (control.Width - e.X), control.Height - e.Y); break; case ResizeModes.BottomRight: control.SetBounds(control.Left, control.Top, control.Width - (control.Width - e.X), control.Height - (control.Height - e.Y)); break; case ResizeModes.BottomLeft: control.SetBounds(control.Left + e.X, control.Top, control.Width - e.X, control.Height - (control.Height - e.Y)); break; } control.ResumeLayout(); } else { if (control.Focused) { if (e.X >= _gripWidth && e.X <= control.Width - _gripWidth && e.Y >= _gripWidth && e.Y <= control.Height - _gripWidth) { // Move control.Cursor = Cursors.SizeAll; _edges = ResizeModes.Move; } else if (e.X <= _gripWidth && e.Y <= _gripWidth) { // TopLeft control.Cursor = Cursors.SizeNWSE; _edges = ResizeModes.TopLeft; } else if (e.X >= control.Width - _gripWidth && e.Y <= _gripWidth) { // TopRight control.Cursor = Cursors.SizeNESW; _edges = ResizeModes.TopRight; } else if (e.X >= control.Width - _gripWidth && e.Y >= control.Height - _gripWidth) { // BottomRight control.Cursor = Cursors.SizeNWSE; _edges = ResizeModes.BottomRight; } else if (e.X <= _gripWidth && e.Y >= control.Height - _gripWidth) { // BottomLeft control.Cursor = Cursors.SizeNESW; _edges = ResizeModes.BottomLeft; } else if (e.Y <= _gripWidth) { // Top control.Cursor = Cursors.SizeNS; _edges = ResizeModes.Top; } else if (e.X >= control.Width - _gripWidth) { // Right control.Cursor = Cursors.SizeWE; _edges = ResizeModes.Right; } else if (e.Y >= control.Height - _gripWidth) { // Bottom control.Cursor = Cursors.SizeNS; _edges = ResizeModes.Bottom; } else if (e.X <= _gripWidth) { // Left control.Cursor = Cursors.SizeWE; _edges = ResizeModes.Left; } else { control.Cursor = Cursors.Default; _edges = ResizeModes.None; } } _edges = _edges & _allowedEdges; if (_edges == ResizeModes.None) { control.Cursor = Cursors.Default; } } }
/// <summary> /// Resizes an image from a bitmap either based on the width or height depending on mode. /// Note image will resize to the larger of the two sides /// </summary> /// <param name="bmp">Bitmap to resize</param> /// <param name="width">new width</param> /// <param name="height">new height</param> /// <returns>resized or original bitmap. Be sure to Dispose this bitmap</returns> public static Bitmap ResizeImageByMode(Bitmap bmp, int width, int height, ResizeModes resizeMode, InterpolationMode mode = InterpolationMode.HighQualityBicubic) { #if NETFULL if (resizeMode == ResizeModes.Auto) { return(ImageUtils.ResizeImage(bmp, width, height, mode)); } #endif Bitmap bmpOut = null; try { decimal ratio; int newWidth = 0; int newHeight = 0; // If the image is smaller than a thumbnail just return original size if (resizeMode == ResizeModes.DontKeepAspectRatio) { newWidth = width; newHeight = height; } else if (bmp.Width < width && bmp.Height < height) { newWidth = bmp.Width; newHeight = bmp.Height; } else { if (resizeMode == ResizeModes.Auto) { if (width > height) { resizeMode = ResizeModes.ByWidth; } else { resizeMode = ResizeModes.ByHeight; } } if (resizeMode == ResizeModes.ByWidth) { ratio = (decimal)width / bmp.Width; newWidth = width; decimal lnTemp = bmp.Height * ratio; newHeight = (int)lnTemp; } else { ratio = (decimal)height / bmp.Height; newHeight = height; decimal lnTemp = bmp.Width * ratio; newWidth = (int)lnTemp; } } bmpOut = new Bitmap(newWidth, newHeight); bmpOut.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution); using (Graphics g = Graphics.FromImage(bmpOut)) { g.InterpolationMode = mode; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight); g.DrawImage(bmp, 0, 0, newWidth, newHeight); } } catch { return(null); } return(bmpOut); }