public void DestinationToSourceRoundtrip() { // be sure to covert back and forth CompositeImageGraphic graphic = new CompositeImageGraphic(3062, 3732); ImageSpatialTransform transform = (ImageSpatialTransform)graphic.SpatialTransform; transform.ClientRectangle = new Rectangle(6, 6, 493, 626); PointF dstPt1 = new Point(100, 200); PointF srcPt = transform.ConvertToSource(dstPt1); PointF dstPt2 = transform.ConvertToDestination(srcPt); Assert.IsTrue(FloatComparer.AreEqual(dstPt1, dstPt2)); }
private static float GetDisplayedWidth(IPresentationImage presentationImage, RectangleF referenceDisplayedRectangle) { ImageSpatialTransform transform = GetImageTransform(presentationImage); Frame frame = GetFrame(presentationImage); //Convert the displayed width to source dimensions SizeF sourceSize = transform.ConvertToSource(new SizeF(referenceDisplayedRectangle.Width, 0)); float x = Math.Abs(sourceSize.Width); float y = Math.Abs(sourceSize.Height); //The displayed width is the magnitude of the line in source coordinates, //but one of xLength or yLength will always be zero, so we can optimize. if (x > y) { return(x * (float)frame.NormalizedPixelSpacing.Column); } return(y * (float)frame.NormalizedPixelSpacing.Row); }
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(); }
public override string GetAnnotationText(IPresentationImage presentationImage) { if (presentationImage == null) { return(String.Empty); } IImageSopProvider imageSopProvider = presentationImage as IImageSopProvider; if (imageSopProvider == null) { return(String.Empty); } ISpatialTransformProvider spatialTransformProvider = presentationImage as ISpatialTransformProvider; if (spatialTransformProvider == null) { return(String.Empty); } ImageSpatialTransform transform = spatialTransformProvider.SpatialTransform as ImageSpatialTransform; if (transform == null) { return(String.Empty); } if (transform.RotationXY % 90 != 0) { return(SR.ValueNotApplicable); } Frame frame = imageSopProvider.Frame; PixelSpacing normalizedPixelSpacing = frame.NormalizedPixelSpacing; if (normalizedPixelSpacing.IsNull) { return(String.Empty); } RectangleF sourceRectangle = new RectangleF(0, 0, frame.Columns, frame.Rows); RectangleF destinationRectangle = transform.ConvertToDestination(sourceRectangle); destinationRectangle = RectangleUtilities.Intersect(destinationRectangle, presentationImage.ClientRectangle); //Convert the displayed width and height to source dimensions SizeF widthInSource = transform.ConvertToSource(new SizeF(destinationRectangle.Width, 0)); SizeF heightInSource = transform.ConvertToSource(new SizeF(0, destinationRectangle.Height)); //The displayed FOV is given by the magnitude of each line in source coordinates, but //for each of the 2 lines, one of x or y will be zero, so we can optimize. float x1 = Math.Abs(widthInSource.Width); float y1 = Math.Abs(widthInSource.Height); float x2 = Math.Abs(heightInSource.Width); float y2 = Math.Abs(heightInSource.Height); double displayedFieldOfViewX, displayedFieldOfViewY; if (x1 > y1) //the image is not rotated { displayedFieldOfViewX = x1 * normalizedPixelSpacing.Column / 10; displayedFieldOfViewY = y2 * normalizedPixelSpacing.Row / 10; } else //the image is rotated by 90 or 270 degrees { displayedFieldOfViewX = x2 * normalizedPixelSpacing.Column / 10; displayedFieldOfViewY = y1 * normalizedPixelSpacing.Row / 10; } return(String.Format(SR.FormatCentimeters, String.Format(SR.Format2Dimensions, displayedFieldOfViewX.ToString("F1"), displayedFieldOfViewY.ToString("F1")))); }