private void UpdateSizeAndPositionFromPreview() { if (_updateIsInProgress) { return; } _updateIsInProgress = true; try { ImageRubberband rubberband = (ImageRubberband)BitmapViewer1.Rubberband; int realWidth; int realHeight; int realX; int realY; realWidth = ConvertToInchH(Convert.ToSingle(rubberband.Rectangle.Width) * _previewScaleX, _printerResolutionX); realHeight = ConvertToInchH(Convert.ToSingle(rubberband.Rectangle.Height) * _previewScaleY, _printerResolutionY); realX = ConvertToInchH(Convert.ToSingle(rubberband.Rectangle.X) * _previewScaleX, _printerResolutionX); realY = ConvertToInchH(Convert.ToSingle(rubberband.Rectangle.Y) * _previewScaleY, _printerResolutionY); _lastX = TextBoxPositionX.Text = realX.ToString(); _lastY = TextBoxPositionY.Text = realY.ToString(); _lastWidth = TextBoxWidth.Text = realWidth.ToString(); _lastHeight = TextBoxHeight.Text = realHeight.ToString(); } finally { _updateIsInProgress = false; } }
private void UpdatePreview() { //Paper bitmap initializing int width; int height; width = 0; height = 0; CalculatePaperImageDimensions(ref width, ref height); System.Drawing.Size fittedSize = FitDimensions(width, height, BitmapViewer1.Width - 20, BitmapViewer1.Height - 20); if (BitmapViewer1.Bitmap != null) { BitmapViewer1.Bitmap.Dispose(); } BitmapViewer1.Bitmap = new Aurigma.GraphicsMill.Bitmap(fittedSize.Width, fittedSize.Height, PixelFormat.Format24bppRgb, RgbColor.White); //Scale coefficients _previewScaleX = Convert.ToSingle(width) / Convert.ToSingle(fittedSize.Width); _previewScaleY = Convert.ToSingle(height) / Convert.ToSingle(fittedSize.Height); //Image rubberband initializing Aurigma.GraphicsMill.Bitmap previewImage = new Aurigma.GraphicsMill.Bitmap(); Resize resizeTransform = new Resize(); resizeTransform.Width = fittedSize.Width; resizeTransform.Height = fittedSize.Height; resizeTransform.ResizeMode = Aurigma.GraphicsMill.Transforms.ResizeMode.Fit; previewImage = resizeTransform.Apply(_originalImage); ImageRubberband rubberband = new ImageRubberband(previewImage); BitmapViewer1.Rubberband = rubberband; rubberband.RectangleChanged += new Aurigma.GraphicsMill.WinControls.RectangleEventHandler(this.RubberbandRectangleChanged); //Setting image size int imageActualWidth; int imageActualHeight; imageActualWidth = Convert.ToInt32(Convert.ToSingle(_originalImage.Width) / Convert.ToSingle(_originalImage.DpiX) * Convert.ToSingle(_printerResolutionX)); imageActualHeight = Convert.ToInt32(Convert.ToSingle(_originalImage.Height) / Convert.ToSingle(_originalImage.DpiY) * Convert.ToSingle(_printerResolutionY)); int previewImageWidth; int previewImageHeight; previewImageWidth = Convert.ToInt32(Convert.ToSingle(imageActualWidth) / Convert.ToSingle(_previewScaleX)); previewImageHeight = Convert.ToInt32(Convert.ToSingle(imageActualHeight) / Convert.ToSingle(_previewScaleY)); System.Drawing.Size tmp = FitDimensions(previewImageWidth, previewImageHeight, fittedSize.Width, fittedSize.Height); previewImageWidth = tmp.Width; previewImageHeight = tmp.Height; rubberband.Rectangle = new System.Drawing.Rectangle((fittedSize.Width - previewImageWidth) / 2, (fittedSize.Height - previewImageHeight) / 2, previewImageWidth, previewImageHeight); //Initializing coordinates & size editboxes UpdateSizeAndPositionFromPreview(); }
private void CenterRubberband() { ImageRubberband rubberband = (ImageRubberband)BitmapViewer1.Rubberband; System.Drawing.Rectangle prevRect = rubberband.Rectangle; int x; int y; x = (BitmapViewer1.Bitmap.Width - prevRect.Width) / 2; y = (BitmapViewer1.Bitmap.Height - prevRect.Height) / 2; rubberband.Rectangle = new System.Drawing.Rectangle(x, y, prevRect.Width, prevRect.Height); }
private void RubberbandRectangleChanged(object sender, Aurigma.GraphicsMill.WinControls.RectangleEventArgs e) { if (_proportionCorrecting) { return; } _proportionCorrecting = true; try { _proportionCorrecting = true; ImageRubberband rubberband = (ImageRubberband)BitmapViewer1.Rubberband; Rectangle rect = rubberband.Rectangle; rect.Height = (rect.Width * _originalImage.Height) / _originalImage.Width; if (rect.Height < 5) { rect.Height = 5; rect.Width = (rect.Height * _originalImage.Width) / _originalImage.Height; } int spade = rect.Y + rect.Height - BitmapViewer1.Bitmap.Height; if (spade > 0) { rect.Y -= spade; } spade = rect.X + rect.Width - BitmapViewer1.Bitmap.Width; if (spade > 0) { rect.X -= spade; } rubberband.Rectangle = rect; UpdateSizeAndPositionFromPreview(); } finally { _proportionCorrecting = false; } }
private void UpdatePreviewFromSizeAndPosition(bool dimensionsChanged, bool widthChanged) { ImageRubberband rubberband = (ImageRubberband)BitmapViewer1.Rubberband; if ((rubberband == null) || _updateIsInProgress) { return; } _updateIsInProgress = true; try { System.Drawing.Rectangle prevRect = rubberband.Rectangle; int newWidth = 0; int newHeight = 0; int newX = 0; int newY = 0; try { if (dimensionsChanged) { newWidth = int.Parse(TextBoxWidth.Text); newHeight = int.Parse(TextBoxHeight.Text); } else { newX = int.Parse(TextBoxPositionX.Text); newY = int.Parse(TextBoxPositionY.Text); } } catch { newX = 0; newY = 0; newWidth = 10; newHeight = 10; } if (dimensionsChanged) { newWidth = Convert.ToInt32(Convert.ToSingle(ConvertToPixels(newWidth, _printerResolutionX)) / _previewScaleX); newHeight = Convert.ToInt32(Convert.ToSingle(ConvertToPixels(newHeight, _printerResolutionY)) / _previewScaleY); if (widthChanged) { newHeight = Convert.ToInt32(Convert.ToSingle(newWidth) * Convert.ToSingle(prevRect.Height) / Convert.ToSingle(prevRect.Width)); } else { newWidth = Convert.ToInt32(Convert.ToSingle(newHeight) * Convert.ToSingle(prevRect.Width) / Convert.ToSingle(prevRect.Height)); } System.Drawing.Size fittedSize = FitDimensions(newWidth, newHeight, BitmapViewer1.Bitmap.Width - prevRect.X, BitmapViewer1.Bitmap.Height - prevRect.Y); rubberband.Rectangle = new System.Drawing.Rectangle(prevRect.X, prevRect.Y, fittedSize.Width, fittedSize.Height); TextBoxWidth.Text = ConvertToInchH(Convert.ToSingle(fittedSize.Width) * _previewScaleX, _printerResolutionX).ToString(); TextBoxHeight.Text = ConvertToInchH(Convert.ToSingle(fittedSize.Height * _previewScaleY), _printerResolutionY).ToString(); } else { newX = Convert.ToInt32(Convert.ToSingle(ConvertToPixels(newX, _printerResolutionX)) / _previewScaleX); newY = Convert.ToInt32(Convert.ToSingle(ConvertToPixels(newY, _printerResolutionY)) / _previewScaleY); newX = Math.Min(newX, BitmapViewer1.Bitmap.Width - prevRect.Width); newY = Math.Min(newY, BitmapViewer1.Bitmap.Height - prevRect.Height); rubberband.Rectangle = new System.Drawing.Rectangle(newX, newY, prevRect.Width, prevRect.Height); TextBoxPositionX.Text = ConvertToInchH(Convert.ToSingle(newX) * _previewScaleX, _printerResolutionX).ToString(); TextBoxPositionY.Text = ConvertToInchH(Convert.ToSingle(newY) * _previewScaleY, _printerResolutionY).ToString(); } } finally { _updateIsInProgress = false; } }
private void TextBox_TextChanged(object sender, System.EventArgs e) { if (TextBoxPositionX.Text == _lastX && TextBoxPositionY.Text == _lastY && TextBoxWidth.Text == _lastWidth && TextBoxHeight.Text == _lastHeight) { return; } try { ImageRubberband rubberband = (ImageRubberband)BitmapViewer1.Rubberband; int width; int height; int x; int y; x = ConvertToPixels(System.Convert.ToSingle(TextBoxPositionX.Text) / _previewScaleX, _printerResolutionX); y = ConvertToPixels(System.Convert.ToSingle(TextBoxPositionY.Text) / _previewScaleY, _printerResolutionY); width = ConvertToPixels(System.Convert.ToSingle(TextBoxWidth.Text) / _previewScaleX, _printerResolutionX); height = ConvertToPixels(System.Convert.ToSingle(TextBoxHeight.Text) / _previewScaleY, _printerResolutionY); if ((TextBox)sender == TextBoxWidth) { height = (width * _originalImage.Height) / _originalImage.Width; } if ((TextBox)sender == TextBoxHeight) { width = (height * _originalImage.Width) / _originalImage.Height; } if (x > BitmapViewer1.Bitmap.Width) { x = BitmapViewer1.Bitmap.Width - width; } if (x < 0) { x = 0; } if (y > BitmapViewer1.Bitmap.Height) { y = BitmapViewer1.Bitmap.Height - height; } if (y < 0) { y = 0; } if (x + width > BitmapViewer1.Bitmap.Width) { width = BitmapViewer1.Bitmap.Width - x; } if (y + height > BitmapViewer1.Bitmap.Height) { height = BitmapViewer1.Bitmap.Height - y; } rubberband.Rectangle = new Rectangle(x, y, width, height); } catch { TextBoxPositionX.Text = _lastX; TextBoxPositionY.Text = _lastY; TextBoxWidth.Text = _lastWidth; TextBoxHeight.Text = _lastHeight; } }