コード例 #1
0
        private void RefreshImage(System.Drawing.Graphics graphics)
        {
            if (!Visible)
            {
                return;
            }

            _renderingSurface.WindowID        = Handle;
            _renderingSurface.ContextID       = graphics.GetHdc();
            _renderingSurface.ClientRectangle = ClientRectangle;
            _renderingSurface.ClipRectangle   = ClientRectangle;

            try
            {
                // if there was an exception the last time we rendered the buffer, don't refresh from the buffer and instead redraw the error message
                if (string.IsNullOrEmpty(_lastRenderExceptionMessage))
                {
                    WinFormsScreenProxy screen = new WinFormsScreenProxy(Screen.FromControl(this));
                    DrawArgs            args   = new DrawArgs(_renderingSurface, screen, DrawMode.Refresh);
                    _magnificationImage.Draw(args);
                }
                else
                {
                    // we cannot simply pass the existing Graphics because we haven't released its hDC yet
                    // if we do, we'll get a "Object is currently in use elsewhere" exception
                    DrawErrorMessage(_lastRenderExceptionMessage, _renderingSurface.ContextID, ClientRectangle);
                }
            }
            catch (Exception ex)
            {
                Platform.Log(LogLevel.Error, ex, "An error has occured while refreshing the magnified contents of the tile.");

                var exceptionMessage = ex is RenderingException ? ((RenderingException)ex).UserMessage : ex.Message;

                // we cannot simply pass the Graphics because we haven't released its hDC yet
                // if we do, we'll get a "Object is currently in use elsewhere" exception
                DrawErrorMessage(exceptionMessage, _renderingSurface.ContextID, ClientRectangle);
            }
            finally
            {
                graphics.ReleaseHdc(_renderingSurface.ContextID);
            }
        }
コード例 #2
0
        private void RenderImage()
        {
            if (!Visible)
            {
                return;
            }

            if (_firstRender)
            {
                // the first time we try to render a freshly cloned image, we need to draw it twice
                // this is to make sure the client rectangle is updated when we try to compute the correct point of interest
                _firstRender = false;
                RenderImage();
            }

            using (System.Drawing.Graphics graphics = base.CreateGraphics())
            {
                _renderingSurface.WindowID        = Handle;
                _renderingSurface.ContextID       = graphics.GetHdc();
                _renderingSurface.ClientRectangle = ClientRectangle;
                _renderingSurface.ClipRectangle   = ClientRectangle;

                try
                {
                    ImageSpatialTransform sourceTransform = (ImageSpatialTransform)((ISpatialTransformProvider)_sourceImage).SpatialTransform;
                    ImageSpatialTransform transform       = (ImageSpatialTransform)((ISpatialTransformProvider)_magnificationImage).SpatialTransform;

                    float scale = sourceTransform.Scale * _magnificationFactor;
                    transform.ScaleToFit   = false;
                    transform.Scale        = scale;
                    transform.TranslationX = 0;
                    transform.TranslationY = 0;

                    // compute translation required to move the point of interest on the magnified image to the centre of the client area
                    var translation = transform.ConvertToSource(new PointF(ClientSize.Width / 2f, ClientSize.Height / 2f)) - new SizeF(_sourcePointOfInterest);
                    transform.TranslationX = translation.X;
                    transform.TranslationY = translation.Y;

                    WinFormsScreenProxy screen = new WinFormsScreenProxy(Screen.FromControl(this));
                    DrawArgs            args   = new DrawArgs(_renderingSurface, screen, ClearCanvas.ImageViewer.Rendering.DrawMode.Render);
                    _magnificationImage.Draw(args);

                    // clear the rendering exception message
                    _lastRenderExceptionMessage = null;
                }
                catch (Exception ex)
                {
                    Platform.Log(LogLevel.Error, ex, "An error has occured while rendering the magnified contents of the tile.");

                    // a rendering exception was encountered, so set the message field
                    _lastRenderExceptionMessage = ex is RenderingException ? ((RenderingException)ex).UserMessage : ex.Message;

                    // we cannot simply pass the existing Graphics because we haven't released its hDC yet
                    // if we do, we'll get a "Object is currently in use elsewhere" exception
                    DrawErrorMessage(_lastRenderExceptionMessage, _renderingSurface.ContextID, ClientRectangle);
                }
                finally
                {
                    graphics.ReleaseHdc(_renderingSurface.ContextID);
                }
            }

            Refresh();
        }