Пример #1
0
 public ImageFormControl(String imageFileToOpen)
 {
     FileName = imageFileToOpen;
     thread = null;
     fileManager = new FileManager();
     ImageState = new ImageState();
 }
Пример #2
0
        public ImageState CalculateZoomedAndRotatedImageState(ImageState imageState)
        {
            ImageState zoomedImageState = new ImageState();

            zoomedImageState.X = (int)(imageState.X * imageState.Zoom);
            zoomedImageState.Y = (int)(imageState.Y * imageState.Zoom);
            zoomedImageState.ScreenHeight = imageState.ScreenHeight;
            zoomedImageState.ScreenWidth = imageState.ScreenWidth;

            float angle = imageState.Angle;
            while (angle > 360) angle = angle - 360;
            while (angle < 0) angle = angle + 360;

            if (angle == 90 || angle == 270)
            {
                zoomedImageState.Height = (int)(imageState.Width * imageState.Zoom);
                zoomedImageState.Width = (int)(imageState.Height * imageState.Zoom);
            }
            else
            {
                zoomedImageState.Height = (int)(imageState.Height * imageState.Zoom);
                zoomedImageState.Width = (int)(imageState.Width * imageState.Zoom);
            }

            //zoomedImageState.Top = (int)(imageState.Top * imageState.Zoom);
            //zoomedImageState.Right = (int)(imageState.Right * imageState.Zoom);
            //zoomedImageState.Left = (int)(imageState.Left * imageState.Zoom);
            //zoomedImageState.Bottom = (int)(imageState.Right * imageState.Zoom);

            zoomedImageState.Zoom = 1;

            return zoomedImageState;
        }
Пример #3
0
        public ImageState AdjustPositionAndScale(int left, int top, int right, int bottom, float otherImageScaleH, float otherImageScaleW, int angle=0)
        {
            ImageState imageState = new ImageState();
            imageState = adjustAngle(angle, imageState);

            left = (int)(left / otherImageScaleW);
            right = (int)(right / otherImageScaleW);
            top = (int)(top / otherImageScaleH);
            bottom = (int)(bottom / otherImageScaleH);

            int width = right - left;
            int height = bottom - top;

            float scaleh = (float)BoxSize.Height / height;
            float scalew = (float)BoxSize.Width / width;

            float minScale = Math.Min(scaleh, scalew);

            Point center = Center;

            imageState.Zoom = minScale;
            imageState.X = -left;
            imageState.Y = -top;

            int scaledWidth = (int)(width * minScale);
            int scaledHeight = (int)(height * minScale);

            int imageWidth = (int)(imageState.Zoom * ImageSize.Width);
            imageState = adjustCenter(minScale, scaledWidth, scaledHeight, imageState);

            return imageState;
        }
Пример #4
0
 public void sendImageStateUpdateForListeners(ImageState imageState)
 {
     if (this.UpdateImage != null)
     {
         this.UpdateImage.BeginInvoke(imageState, null, null);
     }
 }
Пример #5
0
 public ImageState(ImageState otherState)
 {
     this.Active = otherState.Active;
     this.Angle = otherState.Angle;
     this.Bottom = otherState.Bottom;
     this.Height = otherState.Height;
     this.Left = otherState.Left;
     this.Right = otherState.Right;
     this.ScreenHeight = otherState.ScreenHeight;
     this.ScreenWidth = otherState.ScreenWidth;
     this.Top = otherState.Top;
     this.Width = otherState.Width;
     this.X = otherState.X;
     this.Y = otherState.Y;
     this.Zoom = otherState.Zoom;
 }
Пример #6
0
        public void UpdateImageState(ImageState imageState)
        {
            ImageState.Angle = imageState.Angle;
            ImageState.Bottom = imageState.Bottom;
            ImageState.ScreenHeight = imageState.ScreenHeight;
            ImageState.Left = imageState.Left;
            ImageState.Right = imageState.Right;
            ImageState.Top = imageState.Top;
            ImageState.ScreenWidth = imageState.ScreenWidth;
            ImageState.X = imageState.X;
            ImageState.Y = imageState.Y;
            ImageState.Zoom = imageState.Zoom;
            ImageState.Active = imageState.Active;

            if (ImageUpdate != null)
            {
                ImageUpdate.BeginInvoke(ImageState, null, null);
            }
        }
Пример #7
0
        private ImageState adjustCenter(float minScale, int scaledWidth, int scaledHeight, ImageState imageState)
        {
            int pictureWidth = BoxSize.Width;
            int pictureHeight = BoxSize.Height;

            float extrawidth = 0;
            float extraheight = 0;
            if (pictureWidth > scaledWidth)
            {
                extrawidth = (pictureWidth - scaledWidth) / 2;
                extrawidth = (extrawidth > 0) ? extrawidth / minScale : 0;
                if (extrawidth > 0) imageState.X += (int)extrawidth;
            }
            if (pictureHeight > scaledHeight)
            {
                extraheight = (pictureHeight - scaledHeight) / 2;
                extraheight = (extraheight > 0) ? extraheight / minScale : 0;
                if (extraheight > 0) imageState.Y += (int)extraheight;
            }

            return imageState;
        }
Пример #8
0
        public ImageZoomMainForm(String imagePath)
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;

            imageState = new ImageState();
            imageState.Zoom = 1f;
            imageState.X = 0;
            imageState.Y = 0;
            imageState.Angle = 0;
            
            string imagefilename = imagePath;
            loadImage(imagefilename);
            imageState.Width = img.Width;
            imageState.Height = img.Height;
                        
            pictureBox.Paint += new PaintEventHandler(imageBox_Paint);

            this.Show();
            this.WindowState = FormWindowState.Maximized;
            this.Activate();
            
            imageUtils = new ImageUtils();
            imageUtils.Center = getCenterPoint();
            imageUtils.BoxSize = pictureBox.Size;
            imageUtils.ImageSize = img.Size;

            Graphics g = this.CreateGraphics();

            setViewMinimumBounds(0,0,img.Width, img.Height, img.Height, img.Width, 0);

            interceptor = new InterceptKeyboard();
            InterceptKeyboard.SetHook(interceptor.hook);
            interceptor.KeyEvent += keyDown;

            zoomLabel.Text = "zoom is: " + imageState.Zoom;
        }
Пример #9
0
        public void imageShouldBeShownAllIfFitsAndNoShift()
        {
            ImageUtils target = new ImageUtils();
            int left = 0; 
            int top = 0;
            int right = 100; 
            int bottom = 100; 
            float otherImageScaleH = 1; 
            float otherImageScaleW = 1; 
            ImageState actual;
            ImageState expected = new ImageState();
            expected.X = 0;
            expected.Y = 0;
            expected.Zoom = 1f;

            target.ImageSize = new Size(100,100);
            target.BoxSize = new Size(100, 100);            

            actual = target.AdjustPositionAndScale(left, top, right, bottom, otherImageScaleH, otherImageScaleW);
            Assert.AreEqual(expected.X, actual.X);
            Assert.AreEqual(expected.Y, actual.Y);
            Assert.AreEqual(expected.Zoom, actual.Zoom);
            Assert.AreEqual(expected.Angle, actual.Angle);
        }
Пример #10
0
        public void imageShouldBeZommedIfDesiredPartCanBeShownBigger()
        {
            ImageUtils target = new ImageUtils();
            int left = 50;
            int top = 50;
            int right = 75;
            int bottom = 75;
            float otherImageScaleH = 1;
            float otherImageScaleW = 1;
            ImageState actual;
            ImageState expected = new ImageState();
            expected.X = -left;
            expected.Y = -top;
            expected.Zoom = 4f;

            target.ImageSize = new Size(100, 100);
            target.BoxSize = new Size(100, 100);

            actual = target.AdjustPositionAndScale(left, top, right, bottom, otherImageScaleH, otherImageScaleW);
            Assert.AreEqual(expected.X, actual.X);
            Assert.AreEqual(expected.Y, actual.Y);
            Assert.AreEqual(expected.Zoom, actual.Zoom);
            Assert.AreEqual(expected.Angle, actual.Angle);
        }
Пример #11
0
        public void setViewMinimumBounds(int left, int top, int right, int bottom, int otherImageHeight, int otherImageWidth, int rotation)
        {
            Output.Debug("SetView", left + ":" + top + ":" + right + ":" + bottom);

            Size otherImageSize = new Size(otherImageWidth, otherImageHeight);

            imageState = imageUtils.adjustAngle(rotation, imageState);

            float[] dimensionMultiplier = imageUtils.MultipliersToSameSize(GetImageSize(), otherImageSize, imageState.Angle);
            imageState = imageUtils.AdjustPositionAndScale(left, top, right, bottom, dimensionMultiplier[0], dimensionMultiplier[1], rotation);

            pictureBox.Refresh();
        }
Пример #12
0
        public ImageState GetImageState()
        {
            Size originalImageSize = GetImageSize();

            Size imageSize = originalImageSize;

            ImageState imageState = new ImageState(this.imageState);

            //if image is rotated its dimensions are inverted
            if (imageState.Angle == 90 || imageState.Angle == 270)
            {
                imageSize.Height = originalImageSize.Width;
                imageSize.Width = originalImageSize.Height;
            }

            RectangleF displayArea = pictureBox.CreateGraphics().VisibleClipBounds;

            int zoomedHeight = (int)(imageSize.Height * imageState.Zoom);
            int zoomedWidth = (int)(imageSize.Width * imageState.Zoom);

            int zoomedX = (int)(imageState.X * imageState.Zoom);
            int zoomedY = (int)(imageState.Y * imageState.Zoom);

            imageState.Left = (zoomedX >= 0) ? 0 : (int)-imageState.X;
            imageState.Right = (displayArea.Width >= zoomedWidth + zoomedX) ? imageSize.Width : (int)((displayArea.Width - zoomedX)/imageState.Zoom);

            imageState.Top = (zoomedY >= 0) ? 0 : (int)-imageState.Y;
            imageState.Bottom = (displayArea.Height >= zoomedHeight + zoomedY) ? imageSize.Height : (int)((displayArea.Height - zoomedY)/imageState.Zoom);

            imageState.ScreenHeight = (int)displayArea.Height;
            imageState.ScreenWidth = (int)displayArea.Width;

            this.imageState = new ImageState(imageState);
            this.imageState.Width = originalImageSize.Width;
            this.imageState.Height = originalImageSize.Height;
            
            return imageState;
        }
Пример #13
0
        public void ProcessCommand(String command, float value)
        {
            Size imageSize = GetImageSize();
            imageState.Width = imageSize.Width;
            imageState.Height = imageSize.Height;
            imageState.ScreenHeight = pictureBox.Height;
            imageState.ScreenWidth = pictureBox.Width;

            switch (command)
            {
                case ServiceCommands.IMAGE_MOVE_X:
                    addToX((int)(pictureBox.Width * value / imageState.Zoom));
                    break;

                case ServiceCommands.IMAGE_MOVE_Y:
                    addToY((int)(pictureBox.Height * value / imageState.Zoom));
                    break;

                case ServiceCommands.IMAGE_ZOOM:
                    zoomPicture(value, new Point(pictureBox.Width/2, pictureBox.Height/2));
                    break;

                case ServiceCommands.IMAGE_ROTATE:
                    imageState.Angle += (int)value;
                    break;

                case ServiceCommands.CLOSE_IMAGE:
                    ImageState closedState = new ImageState();
                    closedState.Active = false;
                    sendImageStateUpdateForListeners(closedState);
                    return;
            }

            ImageState zoomedImageState = calcHelper.CalculateZoomedAndRotatedImageState(imageState);
            keepInsideBounds(zoomedImageState);
            
            sendImageStateUpdateForListeners(GetImageState());

            Invoke((MethodInvoker)delegate
            {
                pictureBox.Refresh();
            });         
        }
Пример #14
0
        public void keepInsideBounds(ImageState zoomedImageState)
        {
            Graphics graphics = pictureBox.CreateGraphics();
            if (zoomedImageState.Height > pictureBox.Height)
            {
                if (zoomedImageState.Height + zoomedImageState.Y < graphics.VisibleClipBounds.Height)
                {
                    imageState.Y = (int)((graphics.VisibleClipBounds.Height - zoomedImageState.Height) / imageState.Zoom);
                }
                if (imageState.Y > 0)
                {
                    imageState.Y = 0;
                }
            }
            else
            {
                //If height is less than screen width, keep centralized
                int excess = (int)((imageState.ScreenHeight - zoomedImageState.Height) / imageState.Zoom);
                imageState.Y = (int)(excess / 2);
            }

            if (zoomedImageState.Width > pictureBox.Width)
            {
                if (zoomedImageState.Width + zoomedImageState.X < graphics.VisibleClipBounds.Width)
                {
                    imageState.X = (int)((graphics.VisibleClipBounds.Width - zoomedImageState.Width) / imageState.Zoom);
                }
                if (imageState.X > 0)
                {
                    imageState.X = 0;
                }
            }
            else
            {
                //If width is less than screen width, keep centralized
                int excess = (int)((imageState.ScreenWidth - zoomedImageState.Width) / imageState.Zoom);
                imageState.X = (int)(excess / 2);
            }
        }
Пример #15
0
 private void ImageUpdated(ImageState imageState)
 {
     if (!imageState.Active)
     {
         this.CloseCurrentImage();
     }
     WarnImagePresentationListeners();
 }
Пример #16
0
        public void imageShouldBeShiftedToBottomAsRequestedAndCantFitAllWhenRotated90()
        {
            ImageUtils target = new ImageUtils();
            int left = 0;
            int top = 100;
            int right = 100;
            int bottom = 200;
            float otherImageScaleH = 1;
            float otherImageScaleW = 1;
            ImageState actual;
            ImageState expected = new ImageState();
            expected.X = 0;
            expected.Y = -top;
            expected.Zoom = 1f;
            expected.Angle = 90;

            target.ImageSize = new Size(100, 1000);
            target.BoxSize = new Size(100, 100);

            actual = target.AdjustPositionAndScale(left, top, right, bottom, otherImageScaleH, otherImageScaleW, 90);
            Assert.AreEqual(expected.X, actual.X);
            Assert.AreEqual(expected.Y, actual.Y);
            Assert.AreEqual(expected.Zoom, actual.Zoom);
            Assert.AreEqual(expected.Angle, actual.Angle);
        }
Пример #17
0
        public void imageShouldBeShiftedToTwiceBottomWhenScaledRightIsDoubled()
        {
            ImageUtils target = new ImageUtils();
            int left = 0;
            int top = 100;
            int right = 200;
            int bottom = 300;
            float otherImageScaleH = 2f;
            float otherImageScaleW = 2f;
            ImageState actual;
            ImageState expected = new ImageState();
            expected.X = 0;
            expected.Y = -50;
            expected.Zoom = 1f;
            expected.Angle = 0;

            target.ImageSize = new Size(100, 200);
            target.BoxSize = new Size(100, 100);

            actual = target.AdjustPositionAndScale(left, top, right, bottom, otherImageScaleH, otherImageScaleW);
            Assert.AreEqual(expected.X, actual.X);
            Assert.AreEqual(expected.Y, actual.Y);
            Assert.AreEqual(expected.Zoom, actual.Zoom);
            Assert.AreEqual(expected.Angle, actual.Angle);
        }
Пример #18
0
        public void imageShouldBeShiftedRightSameWayWhenIsRotated180()
        {
            ImageUtils target = new ImageUtils();
            int left = 100;
            int top = 0;
            int right = 200;
            int bottom = 100;
            float otherImageScaleH = 1;
            float otherImageScaleW = 1;
            ImageState actual;
            ImageState expected = new ImageState();
            expected.X = -left;
            expected.Y = 0;
            expected.Zoom = 1f;
            expected.Angle = 180;

            target.ImageSize = new Size(1000, 100);
            target.BoxSize = new Size(100, 100);

            actual = target.AdjustPositionAndScale(left, top, right, bottom, otherImageScaleH, otherImageScaleW, 180);
            Assert.AreEqual(expected.X, actual.X);
            Assert.AreEqual(expected.Y, actual.Y);
            Assert.AreEqual(expected.Zoom, actual.Zoom);
            Assert.AreEqual(expected.Angle, actual.Angle);
        }
Пример #19
0
        public void imageShouldBeZommedWithSamePositioningWhenRotated90AndIsNotSquared()
        {
            ImageUtils target = new ImageUtils();
            int left = 50;
            int top = 50;
            int right = 75;
            int bottom = 75;
            float otherImageScaleH = 1;
            float otherImageScaleW = 1;
            ImageState actual;
            ImageState expected = new ImageState();
            expected.X = -left;
            expected.Y = -top;
            expected.Zoom = 4f;
            expected.Angle = 90;

            target.ImageSize = new Size(200, 100);
            target.BoxSize = new Size(100, 100);

            actual = target.AdjustPositionAndScale(left, top, right, bottom, otherImageScaleH, otherImageScaleW, 90);
            Assert.AreEqual(expected.X, actual.X);
            Assert.AreEqual(expected.Y, actual.Y);
            Assert.AreEqual(expected.Zoom, actual.Zoom);
            Assert.AreEqual(expected.Angle, actual.Angle);
        }
Пример #20
0
        public ImageState adjustAngle(int rotation, ImageState imageState)
        {
            while (rotation < 0) rotation += 360;
            while (rotation > 360) rotation -= 360;

            if (rotation == 0)
            {
                imageState.Angle = 0;
            }
            else if (Math.Abs(rotation) == 180)
            {
                imageState.Angle = 180;
            }
            else if (Math.Abs(rotation) == 90 || Math.Abs(rotation) == 270)
            {
                imageState.Angle = rotation;
            }
            return imageState;
        }