コード例 #1
0
        /// <summary>
        /// Renders the <see cref="PresentationImage"/> to a provided offscreen <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="bmp">The offscreen bitmap to render to.</param>
        /// <returns></returns>
        /// <remarks>
        /// This method can be used anywhere an offscreen bitmap is required, such as
        /// paper/DICOM printing, thumbnail generation, creation of new DICOM images, etc.
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="bmp"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="bmp"/> has invalid dimensions.</exception>
        /// <exception cref="RenderingException">Thrown if any <see cref="Exception"/> is encountered while rendering the image.</exception>
        public virtual void DrawToBitmap(Bitmap bmp)
        {
            Platform.CheckForNullReference(bmp, "bmp");

            Platform.CheckPositive(bmp.Width, "bmp.Width");
            Platform.CheckPositive(bmp.Height, "bmp.Height");

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

            try
            {
                using (var surface = 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);
                    DrawNoEvents(drawArgs);
                    drawArgs = new DrawArgs(surface, null, DrawMode.Refresh);
                    DrawNoEvents(drawArgs);
                }
            }
            finally
            {
                graphics.ReleaseHdc(contextId);
                graphics.Dispose();
            }
        }