Beispiel #1
0
        public Bitmap CreateStackIcon(IImageSet imageSet)
        {
            Platform.CheckForNullReference(imageSet, "imageSet");
            IPresentationImage image = GetMiddlePresentationImage(imageSet);

            return(GetStackIcon(delegate(int width, int height) { return image.DrawToBitmap(width, height); }));
        }
Beispiel #2
0
        private static Bitmap DrawToIcon(IPresentationImage image, int width, int height)
        {
            //We just hide the text overlay and application graphics because it creates ugly icons.
            var textOverlayHider         = new TextOverlayVisibilityHelper(image);
            var applicationGraphicsHider = GraphicsVisibilityHelper.CreateForApplicationGraphics(image);

            textOverlayHider.Hide();
            applicationGraphicsHider.HideAll();

            try
            {
                return(image.DrawToBitmap(width, height));
            }
            catch (Exception ex)
            {
                // rendering the error text to a 100x100 icon is useless, so we'll just paint a placeholder error icon and log the icon error
                Platform.Log(LogLevel.Warn, ex, "Failed to render icon with dimensions {0}x{1}", width, height);
                var bitmap = new Bitmap(width, height);
                using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
                {
                    graphics.FillRectangle(Brushes.Black, 0, 0, width, height);
                    graphics.DrawLine(Pens.WhiteSmoke, 0, 0, width, height);
                    graphics.DrawLine(Pens.WhiteSmoke, 0, height, width, 0);
                }
                return(bitmap);
            }
            finally
            {
                textOverlayHider.Restore();
                applicationGraphicsHider.RestoreAll();
            }
        }
        private static Bitmap DrawToFilmSizeBitmap(Size destSize, IPresentationImage image, double tileRatio, bool withAnnotation)
        {
            HideAnnotation(image);
            Bitmap bitmap = image.DrawToBitmap(destSize.Width, destSize.Height);
            Size   size   = CalculateSize(destSize, tileRatio);

            if ((size.Width & 1) == 1)
            {
                size.Width++;
            }
            Rectangle destination = new Rectangle(0, 0, size.Width, size.Height);
            Bitmap    bitmap2     = new Bitmap(size.Width, size.Height);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap2);
            if (size.Height > destSize.Height)
            {
                g.DrawImage(bitmap, 0, (size.Height - destSize.Height) / 2);
            }
            else
            {
                g.DrawImage(bitmap, (size.Width - destSize.Width) / 2, 0);
            }
            ShowAnnotation(image);
            if (withAnnotation)
            {
                DrawTextOverlay(g, destination, image);
            }
            g.Dispose();
            return(bitmap2);
        }
Beispiel #4
0
        public Bitmap GetBitmap(int n)
        {
            IPresentationImage presentationImage =
                PresentationImageFactory.Create(ImageSop.Frames[n]);

            int width  = imageSop.Frames[n].Columns;
            int height = imageSop.Frames[n].Rows;

            return(presentationImage.DrawToBitmap(width, height));
        }
        public static Bitmap CreatePresentationImageIcon(IPresentationImage image, double tileRatio)
        {
            ISpatialTransformProvider provider = image as ISpatialTransformProvider;

            if (provider == null)
            {
                throw new Exception(SR.ConvertTransformProviderFailed);
            }
            ImageSpatialTransform spatialTransform = provider.SpatialTransform as ImageSpatialTransform;
            object memento = spatialTransform.CreateMemento();
            Size   size    = CalculateSize(image.ClientRectangle.Size, tileRatio);
            Bitmap bitmap  = new Bitmap(image.DrawToBitmap(size.Width, size.Height), _iconWidth, Convert.ToInt32((float)(_iconWidth * Convert.ToSingle(tileRatio))));

            spatialTransform.SetMemento(memento);
            return(bitmap);
        }
Beispiel #6
0
        private static void IdentifyPresentationImageFrames(IPresentationImage image, out int imageFrameNumber, out int?overlayFrameNumber)
        {
            var overlayColor = Color.Red;
            var imageColor   = Color.White;

            // force the overlays to show in our chosen colour
            PresentationState.DicomDefault.Deserialize(image);
            var dps = DicomGraphicsPlane.GetDicomGraphicsPlane((IDicomPresentationImage)image, true);

            foreach (var overlay in dps.ImageOverlays)
            {
                overlay.Color = overlayColor;
            }

            var sopProvider = (IImageSopProvider)image;

            using (var dump = image.DrawToBitmap(sopProvider.Frame.Columns, sopProvider.Frame.Rows))
            {
                // identify the frame number encoded in the image
                imageFrameNumber  = 0;
                imageFrameNumber += AreEqual(Sample(dump, 95, 205, 8, 8), imageColor) ? 0x10 : 0;
                imageFrameNumber += AreEqual(Sample(dump, 113, 205, 8, 8), imageColor) ? 0x08 : 0;
                imageFrameNumber += AreEqual(Sample(dump, 130, 205, 8, 8), imageColor) ? 0x04 : 0;
                imageFrameNumber += AreEqual(Sample(dump, 148, 205, 8, 8), imageColor) ? 0x02 : 0;
                imageFrameNumber += AreEqual(Sample(dump, 166, 205, 8, 8), imageColor) ? 0x01 : 0;

                // check if overlay positioning blocks are in the right place
                if (!AreEqual(Sample(dump, 187, 73, 8, 8), overlayColor) || !AreEqual(Sample(dump, 74, 182, 8, 8), overlayColor))
                {
                    overlayFrameNumber = null;
                    return;
                }

                // identify the frame number encoded in the overlay
                overlayFrameNumber  = 0;
                overlayFrameNumber += AreEqual(Sample(dump, 95, 182, 8, 8), overlayColor) ? 0x10 : 0;
                overlayFrameNumber += AreEqual(Sample(dump, 113, 182, 8, 8), overlayColor) ? 0x08 : 0;
                overlayFrameNumber += AreEqual(Sample(dump, 130, 182, 8, 8), overlayColor) ? 0x04 : 0;
                overlayFrameNumber += AreEqual(Sample(dump, 148, 182, 8, 8), overlayColor) ? 0x02 : 0;
                overlayFrameNumber += AreEqual(Sample(dump, 166, 182, 8, 8), overlayColor) ? 0x01 : 0;
            }
        }
Beispiel #7
0
        protected void RunAngleTest(ImageKey key, float expectedAngle, string dumpFilename, params Angle[] shapes)
        {
            ProtractorAngleCalculator analyzer = new ProtractorAngleCalculator();

            Trace.WriteLine(string.Format("Using {0} for test case {1}", key, dumpFilename), "UNIT_TESTS");
            using (IPresentationImage image = GetImage(key))
            {
                IImageSopProvider sopProvider = (IImageSopProvider)image;
                DicomAttribute    attribute;
                if (sopProvider.Sop.DataSource.TryGetAttribute(DicomTags.PixelAspectRatio, out attribute))
                {
                    Trace.WriteLine("PixelAspectRatio (0028,0034) PRESENT", "UNIT_TESTS");
                }
                if (!sopProvider.Frame.PixelSpacing.IsNull)
                {
                    Trace.WriteLine("PixelSpacing (0028,0030) or equivalent PRESENT", "UNIT_TESTS");
                }

                try
                {
                    foreach (Angle shapeData in shapes)
                    {
                        Roi roi          = CreateRoiFromGraphic((IOverlayGraphicsProvider)image, shapeData);
                        var actualResult = analyzer.Analyze(roi, RoiAnalysisMode.Normal);

                        float actualAngle = float.Parse(RegexAngleMeasurement.Match(actualResult.SerializedAsString()).Groups[1].Value, CultureInfo.InvariantCulture);
                        Trace.WriteLine(String.Format("Actual: {0} degrees, Expected: {1} degrees", actualAngle, expectedAngle));
                        Assert.AreEqual(expectedAngle, actualAngle, 0.5f, "Testing Angle {0}", shapeData);                         // allow half degree tolerance
                    }
                }
                finally
                {
                    if (DumpMeasuredImages && !string.IsNullOrEmpty(dumpFilename))
                    {
                        using (Bitmap bmp = image.DrawToBitmap(256, 256))
                        {
                            bmp.Save(string.Format("{1}-{0}.bmp", key, dumpFilename), ImageFormat.Bmp);
                        }
                    }
                }
            }
        }
        private static Bitmap ImageDrawToBitmap(IPresentationImage presentationImage, int width, int height, float dpi)
        {
            if (!(presentationImage is PresentationImage))
            {
                return(presentationImage.DrawToBitmap(width, height));
            }

            var image = (PresentationImage)presentationImage;
            var bmp   = new Bitmap(width, height);

            var graphics  = System.Drawing.Graphics.FromImage(bmp);
            var contextId = graphics.GetHdc();

            try
            {
                using (var surface = image.ImageRenderer.GetRenderingSurface(IntPtr.Zero, bmp.Width, bmp.Height))
                {
                    surface.ContextID     = contextId;
                    surface.ClipRectangle = new Rectangle(0, 0, bmp.Width, bmp.Height);

                    var drawArgs = new DrawArgs(surface, null, DrawMode.Render)
                    {
                        Dpi = dpi, Tag = bmp
                    };
                    image.Draw(drawArgs);
                    drawArgs = new DrawArgs(surface, null, DrawMode.Refresh)
                    {
                        Dpi = dpi, Tag = bmp
                    };
                    image.Draw(drawArgs);
                }
            }
            finally
            {
                graphics.ReleaseHdc(contextId);
                graphics.Dispose();
            }
            return(bmp);
        }
        public DicomElement(string filePath)
        {
            this.FilePath = filePath;

            DicomFile = new DicomFile(filePath);
            DicomFile.Load();

            ImageSop = new ClearCanvas.ImageViewer.StudyManagement.LocalImageSop(filePath);

            PresentationImage =
                PresentationImageFactory.Create(ImageSop.Frames[1]);

            int width  = ImageSop.Frames[1].Columns;
            int height = ImageSop.Frames[1].Rows;

            this.Bitmap = PresentationImage.DrawToBitmap(width, height);
            if (DicomFile.DataSet[DicomTags.InstanceNumber] == null)
            {
                throw new Exception("Tag 'Instance Number' not found!");
            }
            this.InstanceNumber = DicomFile.DataSet[DicomTags.InstanceNumber].GetInt32(0, 1);
        }
Beispiel #10
0
    private static Bitmap DrawLutFrame(Frame f, double ww, double wc)
    {
        IPresentationImage pres     = PresentationImageFactory.Create(f);
        IVoiLutProvider    provider = ((IVoiLutProvider)pres);
        IVoiLutManager     manager  = provider.VoiLutManager;
        var linearLut = manager.VoiLut as IVoiLutLinear;

        if (linearLut != null)
        {
            var standardLut = linearLut as IBasicVoiLutLinear;

            if (standardLut == null)
            {
                var installLut = new BasicVoiLutLinear(ww, wc);

                manager.InstallVoiLut(installLut);
            }
            else
            {
                standardLut.WindowWidth  = ww;
                standardLut.WindowCenter = wc;
            }
            provider.Draw();
        }
        try
        {
            var bmp = pres.DrawToBitmap(f.Columns, f.Rows);
            if (f.Columns == f.Rows)
            {
                if (f.Columns < 512)
                {
                    bmp = ResizeBitmap(bmp, 512, 512);
                }
            }
            return(bmp);
        }
        catch { return(null); }
    }
        protected static Bitmap DrawToBitmap(IPresentationImage presentationImage)
        {
            var imageGraphicProvider     = (IImageGraphicProvider)presentationImage;
            var annotationLayoutProvider = presentationImage as IAnnotationLayoutProvider;
            var annotationLayoutVisible  = true;

            if (annotationLayoutProvider != null)
            {
                annotationLayoutVisible = annotationLayoutProvider.AnnotationLayout.Visible;
                annotationLayoutProvider.AnnotationLayout.Visible = false;
            }

            try
            {
                return(presentationImage.DrawToBitmap(imageGraphicProvider.ImageGraphic.Columns, imageGraphicProvider.ImageGraphic.Rows));
            }
            finally
            {
                if (annotationLayoutProvider != null)
                {
                    annotationLayoutProvider.AnnotationLayout.Visible = annotationLayoutVisible;
                }
            }
        }
Beispiel #12
0
		private static Bitmap ImageDrawToBitmap(IPresentationImage presentationImage, int width, int height, float dpi)
		{
			if (!(presentationImage is PresentationImage))
				return presentationImage.DrawToBitmap(width, height);

			var image = (PresentationImage) presentationImage;
			var bmp = new Bitmap(width, height);

			var graphics = System.Drawing.Graphics.FromImage(bmp);
			var contextId = graphics.GetHdc();
			try
			{
				using (var surface = image.ImageRenderer.CreateRenderingSurface(IntPtr.Zero, bmp.Width, bmp.Height, RenderingSurfaceType.Offscreen))
				{
					surface.ContextID = contextId;
					surface.ClipRectangle = new Rectangle(0, 0, bmp.Width, bmp.Height);

					var drawArgs = new DrawArgs(surface, null, DrawMode.Render) {Dpi = dpi, Tag = bmp};
					image.Draw(drawArgs);
                    drawArgs = new DrawArgs(surface, null, DrawMode.Refresh) { Dpi = dpi, Tag = bmp};
					image.Draw(drawArgs);
				}
			}
			finally
			{
				graphics.ReleaseHdc(contextId);
				graphics.Dispose();
			}
			return bmp;
		}
        public static Bitmap CreatePresentationImagePrintData(IPresentationImage image, double tileRatio, bool withAnnotation)
        {
            Size size;
            ISpatialTransformProvider provider = image as ISpatialTransformProvider;

            if (provider == null)
            {
                Platform.Log(LogLevel.Error, " 转换失败");
                throw new Exception("转换失败");
            }
            ImageSpatialTransform spatialTransform = provider.SpatialTransform as ImageSpatialTransform;
            object memento = spatialTransform.CreateMemento();

            if (!spatialTransform.ScaleToFit)
            {
                //Platform.Log(LogLevel.Error, "!spatialTransform.ScaleToFit");
                float num    = 1f / spatialTransform.Scale;
                int   width  = Convert.ToInt32((float)(image.ClientRectangle.Width * num));
                int   height = Convert.ToInt32((float)(image.ClientRectangle.Height * num));
                //if ((width > spatialTransform.SourceWidth) || (height > spatialTransform.SourceHeight))
                //{
                //    float num4 = ((float)image.ClientRectangle.Width) / ((float)image.ClientRectangle.Height);
                //    float num5 = ((float)spatialTransform.SourceWidth) / ((float)spatialTransform.SourceHeight);
                //    if (num4 > num5)
                //    {
                //        size = new Size(spatialTransform.SourceWidth, Convert.ToInt32((float)(((float)spatialTransform.SourceWidth) / num4)));
                //        spatialTransform.Scale = (spatialTransform.Scale * spatialTransform.SourceWidth) / ((float)image.ClientRectangle.Width);
                //    }
                //    else
                //    {
                //        size = new Size(Convert.ToInt32((float)(spatialTransform.SourceHeight * num4)), spatialTransform.SourceHeight);
                //        spatialTransform.Scale = (spatialTransform.Scale * spatialTransform.SourceHeight) / ((float)image.ClientRectangle.Height);
                //    }
                //}
                //else
                {
                    if (width >= 3000 || height >= 3000)
                    {
                        width  = (int)(width * 0.3);
                        height = (int)(height * 0.3);
                    }

                    size = new Size(width, height);
                    spatialTransform.Scale = (spatialTransform.Scale * width) / ((float)image.ClientRectangle.Width);
                }
            }
            else
            {
                if (spatialTransform.SourceWidth > 3000 || spatialTransform.SourceHeight > 3000)
                {
                    size = new Size((int)(spatialTransform.SourceWidth * 0.3), (int)(spatialTransform.SourceHeight * 0.3));
                }
                else
                {
                    size = new Size(spatialTransform.SourceWidth, spatialTransform.SourceHeight);
                }
            }


            Size size2 = CalculateSize(size, tileRatio);

            if (!withAnnotation)
            {
                HideAnnotation(image);
            }
            Bitmap bitmap = image.DrawToBitmap(size2.Width, size2.Height);

            if (!withAnnotation)
            {
                ShowAnnotation(image);
            }
            spatialTransform.SetMemento(memento);
            return(bitmap);
        }
Beispiel #14
0
		public Bitmap CreateImageIcon(IPresentationImage image)
		{
			Platform.CheckForNullReference(image, "image");
			return GetImageIcon(delegate(int width, int height) { return image.DrawToBitmap(width, height); });
		}
Beispiel #15
0
 public Bitmap CreateImageIcon(IPresentationImage image)
 {
     Platform.CheckForNullReference(image, "image");
     return(GetImageIcon(delegate(int width, int height) { return image.DrawToBitmap(width, height); }));
 }
		private static void IdentifyPresentationImageFrames(IPresentationImage image, out int imageFrameNumber, out int? overlayFrameNumber)
		{
			var overlayColor = Color.Red;
			var imageColor = Color.White;

			// force the overlays to show in our chosen colour
			PresentationState.DicomDefault.Deserialize(image);
			var dps = DicomGraphicsPlane.GetDicomGraphicsPlane((IDicomPresentationImage) image, true);
			foreach (var overlay in dps.ImageOverlays)
				overlay.Color = overlayColor;

			var sopProvider = (IImageSopProvider) image;
			using (var dump = image.DrawToBitmap(sopProvider.Frame.Columns, sopProvider.Frame.Rows))
			{
				// identify the frame number encoded in the image
				imageFrameNumber = 0;
				imageFrameNumber += AreEqual(Sample(dump, 95, 205, 8, 8), imageColor) ? 0x10 : 0;
				imageFrameNumber += AreEqual(Sample(dump, 113, 205, 8, 8), imageColor) ? 0x08 : 0;
				imageFrameNumber += AreEqual(Sample(dump, 130, 205, 8, 8), imageColor) ? 0x04 : 0;
				imageFrameNumber += AreEqual(Sample(dump, 148, 205, 8, 8), imageColor) ? 0x02 : 0;
				imageFrameNumber += AreEqual(Sample(dump, 166, 205, 8, 8), imageColor) ? 0x01 : 0;

				// check if overlay positioning blocks are in the right place
				if (!AreEqual(Sample(dump, 187, 73, 8, 8), overlayColor) || !AreEqual(Sample(dump, 74, 182, 8, 8), overlayColor))
				{
					overlayFrameNumber = null;
					return;
				}

				// identify the frame number encoded in the overlay
				overlayFrameNumber = 0;
				overlayFrameNumber += AreEqual(Sample(dump, 95, 182, 8, 8), overlayColor) ? 0x10 : 0;
				overlayFrameNumber += AreEqual(Sample(dump, 113, 182, 8, 8), overlayColor) ? 0x08 : 0;
				overlayFrameNumber += AreEqual(Sample(dump, 130, 182, 8, 8), overlayColor) ? 0x04 : 0;
				overlayFrameNumber += AreEqual(Sample(dump, 148, 182, 8, 8), overlayColor) ? 0x02 : 0;
				overlayFrameNumber += AreEqual(Sample(dump, 166, 182, 8, 8), overlayColor) ? 0x01 : 0;
			}
		}
		protected static Bitmap DrawToBitmap(IPresentationImage presentationImage)
		{
			var imageGraphicProvider = (IImageGraphicProvider) presentationImage;
			var annotationLayoutProvider = presentationImage as IAnnotationLayoutProvider;
			var annotationLayoutVisible = true;

			if (annotationLayoutProvider != null)
			{
				annotationLayoutVisible = annotationLayoutProvider.AnnotationLayout.Visible;
				annotationLayoutProvider.AnnotationLayout.Visible = false;
			}

			try
			{
				return presentationImage.DrawToBitmap(imageGraphicProvider.ImageGraphic.Columns, imageGraphicProvider.ImageGraphic.Rows);
			}
			finally
			{
				if (annotationLayoutProvider != null)
				{
					annotationLayoutProvider.AnnotationLayout.Visible = annotationLayoutVisible;
				}
			}
		}
Beispiel #18
0
		private static Bitmap DrawToIcon(IPresentationImage image, int width, int height)
		{
			//We just hide the text overlay and application graphics because it creates ugly icons.
			var textOverlayHider = new TextOverlayVisibilityHelper(image);
			var applicationGraphicsHider = GraphicsVisibilityHelper.CreateForApplicationGraphics(image);
			textOverlayHider.Hide();
			applicationGraphicsHider.HideAll();

			try
			{
				return image.DrawToBitmap(width, height);
			}
			catch (Exception ex)
			{
				// rendering the error text to a 100x100 icon is useless, so we'll just paint a placeholder error icon and log the icon error
				Platform.Log(LogLevel.Warn, ex, "Failed to render icon with dimensions {0}x{1}", width, height);
				var bitmap = new Bitmap(width, height);
				using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
				{
					graphics.FillRectangle(Brushes.Black, 0, 0, width, height);
					graphics.DrawLine(Pens.WhiteSmoke, 0, 0, width, height);
					graphics.DrawLine(Pens.WhiteSmoke, 0, height, width, 0);
				}
				return bitmap;
			}
			finally
			{
				textOverlayHider.Restore();
				applicationGraphicsHider.RestoreAll();
			}
		}