Smrf.NodeXL.Visualization.Wpf.NodeXLControl.CopyGraphToBitmap C# (CSharp) Method

CopyGraphToBitmap() public method

public CopyGraphToBitmap ( Int32 bitmapWidthPx, Int32 bitmapHeightPx ) : System.Drawing.Bitmap
bitmapWidthPx System.Int32
bitmapHeightPx System.Int32
return System.Drawing.Bitmap
    CopyGraphToBitmap
    (
        Int32 bitmapWidthPx,
        Int32 bitmapHeightPx
    )
    {
        const String MethodName = "CopyGraphToBitmap";

        this.ArgumentChecker.CheckArgumentPositive(MethodName, "bitmapWidthPx",
            bitmapWidthPx);

        this.ArgumentChecker.CheckArgumentPositive(MethodName, "bitmapHeightPx",
            bitmapHeightPx);

        CheckIfLayingOutGraph(MethodName);

        // Save the current vertex locations.

        LayoutSaver oLayoutSaver = new LayoutSaver(this.Graph);

        // Adjust the control's graph scale so that the graph's vertices and
        // edges will be the same relative size in the image that they are in
        // the control.

        GraphImageScaler oGraphImageScaler = new GraphImageScaler(this);

        oGraphImageScaler.SetGraphScale(bitmapWidthPx, bitmapHeightPx,
            WpfGraphicsUtil.GetScreenDpi(this).Width);

        // Adjust the control's transforms so that the image will be centered
        // on the same point on the graph that the control is centered on.

        GraphImageCenterer oGraphImageCenterer = new GraphImageCenterer(this);

        oGraphImageCenterer.CenterGraphImage(
            new Size(bitmapWidthPx, bitmapHeightPx) );

        // Transform the graph's layout to the specified size.

        Double dOriginalActualWidth = this.ActualWidth;
        Double dOriginalActualHeight = this.ActualHeight;

        Rect oBitmapRectangle = new Rect(0, 0,
            (Double)bitmapWidthPx, (Double)bitmapHeightPx);

        TransformLayout(oBitmapRectangle);

        Debug.Assert(m_eLayoutState == LayoutState.Stable);

        DrawGraph(oBitmapRectangle);

        System.Drawing.Bitmap oBitmap = WpfGraphicsUtil.VisualToBitmap(this,
            bitmapWidthPx, bitmapHeightPx);

        // Restore the original layout.
        //
        // NOTE:
        //
        // Don't try calling TransformLayout() again using the original
        // rectangle.  The first call to TransformLayout() lost "resolution" if
        // the layout was transformed to a smaller rectangle, and attempting to
        // reverse the transform will yield poor results.

        oLayoutSaver.RestoreLayout();

        oGraphImageScaler.RestoreGraphScale();

        oBitmapRectangle =
            new Rect(0, 0, dOriginalActualWidth, dOriginalActualHeight);

        Debug.Assert(m_eLayoutState == LayoutState.Stable);

        DrawGraph(oBitmapRectangle);

        oGraphImageCenterer.RestoreCenter();

        return (oBitmap);
    }

Usage Example

    GetImages
    (
        NodeXLControl oNodeXLControl,
        Boolean bUseFixedAspectRatio,
        out Byte [] abtFullSizeImage,
        out Byte [] abtThumbnail
    )
    {
        Debug.Assert(oNodeXLControl != null);
        Debug.Assert(oNodeXLControl.ActualWidth >= MinimumNodeXLControlWidth);
        Debug.Assert(oNodeXLControl.ActualHeight >= MinimumNodeXLControlHeight);

        Size oFullSizeImageSizePx = GetFullSizeImageSizePx(oNodeXLControl,
            bUseFixedAspectRatio);

        Image oFullSizeImage = oNodeXLControl.CopyGraphToBitmap(
            oFullSizeImageSizePx.Width, oFullSizeImageSizePx.Height);

        Size oThumbnailImageSizePx = GetThumbnailImageSizePx(
            oFullSizeImageSizePx);

        Image oThumbnail = oFullSizeImage.GetThumbnailImage(
            oThumbnailImageSizePx.Width, oThumbnailImageSizePx.Height,
            () => {return false;}, IntPtr.Zero);

        abtFullSizeImage = ImageToBytes(oFullSizeImage);
        abtThumbnail = ImageToBytes(oThumbnail);

        oFullSizeImage.Dispose();
        oThumbnail.Dispose();
    }
NodeXLControl