private void RenderImage(DrawArgs args) { if (args.DrawMode == DrawMode.Refresh) { RefreshImage(args); 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(args); } try { var sourceTransform = (ImageSpatialTransform)((ISpatialTransformProvider)SelectedPresentationImage).SpatialTransform; var transform = (ImageSpatialTransform)((ISpatialTransformProvider)_magnificationImage).SpatialTransform; float scale = sourceTransform.Scale * ToolSettings.DefaultInstance.MagnificationFactor; transform.ScaleToFit = false; transform.Scale = scale; transform.TranslationX = 0; transform.TranslationY = 0; var midPoint = new PointF(args.RenderingSurface.ClientRectangle.Width / 2f, args.RenderingSurface.ClientRectangle.Height / 2f); var sourcePointOfInterest = sourceTransform.ConvertToSource(_tileLocation); // compute translation required to move the point of interest on the magnified image to the centre of the client area var translation = transform.ConvertToSource(midPoint) - new SizeF(sourcePointOfInterest); transform.TranslationX = translation.X; transform.TranslationY = translation.Y; _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, args.RenderingSurface.ContextID, args.RenderingSurface.ClientRectangle); } }
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); } }