Ejemplo n.º 1
0
 /// <summary>
 /// Call <see cref="GraphPane.AxisChange()"/> for all <see cref="GraphPane"/> objects in the
 /// <see cref="PaneList"/> list.
 /// </summary>
 /// <remarks>
 /// This overload of AxisChange just uses the default Graphics instance for the screen.
 /// If you have a Graphics instance available from your Windows Form, you should use
 /// the <see cref="AxisChange(IGraphics)" /> overload instead.
 /// </remarks>
 public void AxisChange()
 {
     using (IGraphics g = new GdiGraphics(Graphics.FromHwnd(IntPtr.Zero)))
     {
         AxisChange(g);
     }
 }
        /// <summary>
        /// Handle a MouseWheel event in the <see cref="ZedGraphControl" />
        /// </summary>
        /// <param name="sender">A reference to the <see cref="ZedGraphControl" /></param>
        /// <param name="e">A <see cref="MouseEventArgs" /> instance</param>
        protected void ZedGraphControl_MouseWheel( object sender, MouseEventArgs e )
        {
            if ( ( _isEnableVZoom || _isEnableHZoom ) && _isEnableWheelZoom && _masterPane != null )
            {
                GraphPane pane = this.MasterPane.FindChartRect( new PointF( e.X, e.Y ) );
                if ( pane != null && e.Delta != 0 )
                {
                    ZoomState oldState = ZoomStateSave( pane, ZoomState.StateType.WheelZoom );
                    //ZoomState oldState = pane.ZoomStack.Push( pane, ZoomState.StateType.Zoom );

                    PointF centerPoint = new PointF( e.X, e.Y );
                    double zoomFraction = ( 1 + ( e.Delta < 0 ? 1.0 : -1.0 ) * ZoomStepFraction );

                    ZoomPane( pane, zoomFraction, centerPoint, _isZoomOnMouseCenter, false );

                    ApplyToAllPanes( pane );

                    using (IGraphics g = new GdiGraphics(this.CreateGraphics()))
                    {
                        // always AxisChange() the dragPane
                        pane.AxisChange( g );

                        foreach ( GraphPane tempPane in _masterPane._paneList )
                        {
                            if ( tempPane != pane && ( _isSynchronizeXAxes || _isSynchronizeYAxes ) )
                                tempPane.AxisChange( g );
                        }
                    }

                    ZoomStatePush( pane );

                    // Provide Callback to notify the user of zoom events
                    if ( this.ZoomEvent != null )
                        this.ZoomEvent( this, oldState, new ZoomState( pane, ZoomState.StateType.WheelZoom ) );

                    this.Refresh();

                }
            }
        }
        /// <summary>
        /// Zoom a specified pane in or out according to the specified zoom fraction.
        /// </summary>
        /// <remarks>
        /// The zoom will occur on the <see cref="XAxis" />, <see cref="YAxis" />, and
        /// <see cref="Y2Axis" /> only if the corresponding flag, <see cref="IsEnableHZoom" /> or
        /// <see cref="IsEnableVZoom" />, is true.  Note that if there are multiple Y or Y2 axes, all of
        /// them will be zoomed.
        /// </remarks>
        /// <param name="pane">The <see cref="GraphPane" /> instance to be zoomed.</param>
        /// <param name="zoomFraction">The fraction by which to zoom, less than 1 to zoom in, greater than
        /// 1 to zoom out.  For example, 0.9 will zoom in such that the scale is 90% of what it was
        /// originally.</param>
        /// <param name="centerPt">The screen position about which the zoom will be centered.  This
        /// value is only used if <see paramref="isZoomOnCenter" /> is true.
        /// </param>
        /// <param name="isZoomOnCenter">true to cause the zoom to be centered on the point
        /// <see paramref="centerPt" />, false to center on the <see cref="Chart.Rect" />.
        /// </param>
        /// <param name="isRefresh">true to force a refresh of the control, false to leave it unrefreshed</param>
        protected void ZoomPane( GraphPane pane, double zoomFraction, PointF centerPt,
					bool isZoomOnCenter, bool isRefresh )
        {
            double x;
            double x2;
            double[] y;
            double[] y2;

            pane.ReverseTransform( centerPt, out x, out x2, out y, out y2 );

            if ( _isEnableHZoom )
            {
                ZoomScale( pane.XAxis, zoomFraction, x, isZoomOnCenter );
                ZoomScale( pane.X2Axis, zoomFraction, x2, isZoomOnCenter );
            }
            if ( _isEnableVZoom )
            {
                for ( int i = 0; i < pane.YAxisList.Count; i++ )
                    ZoomScale( pane.YAxisList[i], zoomFraction, y[i], isZoomOnCenter );
                for ( int i = 0; i < pane.Y2AxisList.Count; i++ )
                    ZoomScale( pane.Y2AxisList[i], zoomFraction, y2[i], isZoomOnCenter );
            }

            using (IGraphics g = new GdiGraphics(CreateGraphics()))
            {
                pane.AxisChange( g );
                //g.Dispose();
            }

            this.SetScroll( this.hScrollBar1, pane.XAxis, _xScrollRange.Min, _xScrollRange.Max );
            this.SetScroll( this.vScrollBar1, pane.YAxis, _yScrollRangeList[0].Min,
                _yScrollRangeList[0].Max );

            if ( isRefresh )
                Refresh();
        }
        private void HandleZoomFinish( object sender, MouseEventArgs e )
        {
            PointF mousePtF = BoundPointToRect( new Point( e.X, e.Y ), _dragPane.Chart._rect );

            // Only accept a drag if it covers at least 5 pixels in each direction
            //Point curPt = ( (Control)sender ).PointToScreen( Point.Round( mousePt ) );
            if ( ( Math.Abs( mousePtF.X - _dragStartPt.X ) > 4 || !_isEnableHZoom ) &&
                    ( Math.Abs( mousePtF.Y - _dragStartPt.Y ) > 4 || !_isEnableVZoom ) )
            {
                // Draw the rectangle to be evaluated. Set a dashed frame style
                // using the FrameStyle enumeration.
                //ControlPaint.DrawReversibleFrame( this.dragRect,
                //	this.BackColor, FrameStyle.Dashed );

                double x1, x2, xx1, xx2;
                double[] y1, y2, yy1, yy2;
                //PointF startPoint = ( (Control)sender ).PointToClient( this.dragRect.Location );

                _dragPane.ReverseTransform( _dragStartPt, out x1, out xx1, out y1, out yy1 );
                _dragPane.ReverseTransform( mousePtF, out x2, out xx2, out y2, out yy2 );

                bool zoomLimitExceeded = false;

                if ( _isEnableHZoom )
                {
                    double min1 = Math.Min( x1, x2 );
                    double max1 = Math.Max( x1, x2 );
                    double min2 = Math.Min( xx1, xx2 );
                    double max2 = Math.Max( xx1, xx2 );

                    if ( Math.Abs( x1 - x2 ) < ZoomResolution || Math.Abs( xx1 - xx2 ) < ZoomResolution )
                        zoomLimitExceeded = true;
                }

                if ( _isEnableVZoom && !zoomLimitExceeded )
                {
                    for ( int i = 0; i < y1.Length; i++ )
                    {
                        if ( Math.Abs( y1[i] - y2[i] ) < ZoomResolution )
                        {
                            zoomLimitExceeded = true;
                            break;
                        }
                    }
                    for ( int i = 0; i < yy1.Length; i++ )
                    {
                        if ( Math.Abs( yy1[i] - yy2[i] ) < ZoomResolution )
                        {
                            zoomLimitExceeded = true;
                            break;
                        }
                    }
                }

                if ( !zoomLimitExceeded )
                {

                    ZoomStatePush( _dragPane );
                    //ZoomState oldState = _dragPane.ZoomStack.Push( _dragPane,
                    //			ZoomState.StateType.Zoom );

                    if ( _isEnableHZoom )
                    {
                        _dragPane.XAxis._scale._min = Math.Min( x1, x2 );
                        _dragPane.XAxis._scale._minAuto = false;
                        _dragPane.XAxis._scale._max = Math.Max( x1, x2 );
                        _dragPane.XAxis._scale._maxAuto = false;

                        _dragPane.X2Axis._scale._min = Math.Min( xx1, xx2 );
                        _dragPane.X2Axis._scale._minAuto = false;
                        _dragPane.X2Axis._scale._max = Math.Max( xx1, xx2 );
                        _dragPane.X2Axis._scale._maxAuto = false;
                    }

                    if ( _isEnableVZoom )
                    {
                        for ( int i = 0; i < y1.Length; i++ )
                        {
                            _dragPane.YAxisList[i]._scale._min = Math.Min( y1[i], y2[i] );
                            _dragPane.YAxisList[i]._scale._max = Math.Max( y1[i], y2[i] );
                            _dragPane.YAxisList[i]._scale._minAuto = false;
                            _dragPane.YAxisList[i]._scale._maxAuto = false;
                        }
                        for ( int i = 0; i < yy1.Length; i++ )
                        {
                            _dragPane.Y2AxisList[i]._scale._min = Math.Min( yy1[i], yy2[i] );
                            _dragPane.Y2AxisList[i]._scale._max = Math.Max( yy1[i], yy2[i] );
                            _dragPane.Y2AxisList[i]._scale._minAuto = false;
                            _dragPane.Y2AxisList[i]._scale._maxAuto = false;
                        }
                    }

                    this.SetScroll( this.hScrollBar1, _dragPane.XAxis, _xScrollRange.Min, _xScrollRange.Max );
                    this.SetScroll( this.vScrollBar1, _dragPane.YAxis, _yScrollRangeList[0].Min,
                        _yScrollRangeList[0].Max );

                    ApplyToAllPanes( _dragPane );

                    // Provide Callback to notify the user of zoom events
                    if ( this.ZoomEvent != null )
                        this.ZoomEvent( this, _zoomState, //oldState,
                            new ZoomState( _dragPane, ZoomState.StateType.Zoom ) );

                    using (IGraphics g = new GdiGraphics(CreateGraphics()))
                    {
                        // always AxisChange() the dragPane
                        _dragPane.AxisChange( g );

                        foreach ( GraphPane pane in _masterPane._paneList )
                        {
                            if ( pane != _dragPane && ( _isSynchronizeXAxes || _isSynchronizeYAxes ) )
                                pane.AxisChange( g );
                        }
                    }
                }

                Refresh();
            }
        }
        /// <summary>
        /// Handle a MouseDown event in the <see cref="ZedGraphControl" />
        /// </summary>
        /// <param name="sender">A reference to the <see cref="ZedGraphControl" /></param>
        /// <param name="e">A <see cref="MouseEventArgs" /> instance</param>
        protected void ZedGraphControl_MouseDown( object sender, MouseEventArgs e )
        {
            _isPanning = false;
            _isZooming = false;
            _isEditing = false;
            _isSelecting = false;
            _dragPane = null;

            Point mousePt = new Point( e.X, e.Y );

            // Callback for doubleclick events
            if ( _masterPane != null && e.Clicks > 1 && this.DoubleClickEvent != null )
            {
                if ( this.DoubleClickEvent( this, e ) )
                    return;
            }

            // Provide Callback for MouseDown events
            if ( _masterPane != null && this.MouseDownEvent != null )
            {
                if ( this.MouseDownEvent( this, e ) )
                    return;
            }

            if ( e.Clicks > 1 || _masterPane == null )
                return;

            // First, see if the click is within a Linkable object within any GraphPane
            GraphPane pane = this.MasterPane.FindPane( mousePt );
            if ( pane != null &&
                    e.Button == _linkButtons && Control.ModifierKeys == _linkModifierKeys )
            {
                object source;
                Link link;
                int index;
                using (IGraphics g = new GdiGraphics(CreateGraphics()))
                {
                    float scaleFactor = pane.CalcScaleFactor();
                    if ( pane.FindLinkableObject( mousePt, g, scaleFactor, out source, out link, out index ) )
                    {
                        if ( LinkEvent != null && LinkEvent( this, pane, source, link, index ) )
                            return;

                        string url;
                        CurveItem curve = source as CurveItem;

                        if ( curve != null )
                            url = link.MakeCurveItemUrl( pane, curve, index );
                        else
                            url = link._url;

                        if ( url != string.Empty )
                        {
                            System.Diagnostics.Process.Start( url );
                            // linkable objects override any other actions with mouse
                            return;
                        }
                    }
                    //g.Dispose();
                }
            }

            // Second, Check to see if it's within a Chart Rect
            pane = this.MasterPane.FindChartRect( mousePt );
            //Rectangle rect = new Rectangle( mousePt, new Size( 1, 1 ) );

            if ( pane != null &&
                ( _isEnableHPan || _isEnableVPan ) &&
                ( ( e.Button == _panButtons && Control.ModifierKeys == _panModifierKeys ) ||
                ( e.Button == _panButtons2 && Control.ModifierKeys == _panModifierKeys2 ) ) )
            {
                _isPanning = true;
                _dragStartPt = mousePt;
                _dragPane = pane;
                //_zoomState = new ZoomState( _dragPane, ZoomState.StateType.Pan );
                ZoomStateSave( _dragPane, ZoomState.StateType.Pan );
            }
            else if ( pane != null && ( _isEnableHZoom || _isEnableVZoom ) &&
                ( ( e.Button == _zoomButtons && Control.ModifierKeys == _zoomModifierKeys ) ||
                ( e.Button == _zoomButtons2 && Control.ModifierKeys == _zoomModifierKeys2 ) ) )
            {
                _isZooming = true;
                _dragStartPt = mousePt;
                _dragEndPt = mousePt;
                _dragEndPt.Offset( 1, 1 );
                _dragPane = pane;
                ZoomStateSave( _dragPane, ZoomState.StateType.Zoom );
            }
            //Revision: JCarpenter 10/06
            else if ( pane != null && _isEnableSelection && e.Button == _selectButtons &&
                ( Control.ModifierKeys == _selectModifierKeys ||
                    Control.ModifierKeys == _selectAppendModifierKeys ) )
            {
                _isSelecting = true;
                _dragStartPt = mousePt;
                _dragEndPt = mousePt;
                _dragEndPt.Offset( 1, 1 );
                _dragPane = pane;
            }
            else if ( pane != null && ( _isEnableHEdit || _isEnableVEdit ) &&
                 ( e.Button == EditButtons && Control.ModifierKeys == EditModifierKeys ) )
            {

                // find the point that was clicked, and make sure the point list is editable
                // and that it's a primary Y axis (the first Y or Y2 axis)
                if ( pane.FindNearestPoint( mousePt, out _dragCurve, out _dragIndex ) &&
                            _dragCurve.Points is IPointListEdit )
                {
                    _isEditing = true;
                    _dragPane = pane;
                    _dragStartPt = mousePt;
                    _dragStartPair = _dragCurve[_dragIndex];
                }
            }
        }
        //Revision: JCarpenter 10/06
        /// <summary>
        /// Find the object currently under the mouse cursor, and return its state.
        /// </summary>
        private ContextMenuObjectState GetObjectState()
        {
            ContextMenuObjectState objState = ContextMenuObjectState.Background;

            // Determine object state
            Point mousePt = this.PointToClient( Control.MousePosition );
            int iPt;
            GraphPane pane;
            object nearestObj;

            IGraphics g = new GdiGraphics(this.CreateGraphics());
            {
                if ( this.MasterPane.FindNearestPaneObject( mousePt, g, out pane,
                        out nearestObj, out iPt ) )
                {
                    CurveItem item = nearestObj as CurveItem;

                    if ( item != null && iPt >= 0 )
                    {
                        if ( item.IsSelected )
                            objState = ContextMenuObjectState.ActiveSelection;
                        else
                            objState = ContextMenuObjectState.InactiveSelection;
                    }
                }
            }

            return objState;
        }
        // Revision: JCarpenter 10/06
        /// <summary>
        /// Perform selection on curves within the drag pane, or under the mouse click.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HandleSelectionFinish( object sender, MouseEventArgs e )
        {
            if ( e.Button != _selectButtons )
            {
                Refresh();
                return;
            }

            PointF mousePtF = BoundPointToRect( new Point( e.X, e.Y ), _dragPane.Chart._rect );

            PointF mousePt = BoundPointToRect( new Point( e.X, e.Y ), _dragPane.Rect );

            Point curPt = ( (Control)sender ).PointToScreen( Point.Round( mousePt ) );

            // Only accept a drag if it covers at least 5 pixels in each direction
            //Point curPt = ( (Control)sender ).PointToScreen( Point.Round( mousePt ) );
            if ( ( Math.Abs( mousePtF.X - _dragStartPt.X ) > 4 ) &&
                      ( Math.Abs( mousePtF.Y - _dragStartPt.Y ) > 4 ) )
            {

                #region New Code to Select on Rubber Band

                double x1, x2, xx1, xx2;
                double[] y1, y2, yy1, yy2;
                PointF startPoint = ( (Control)sender ).PointToClient( new Point( Convert.ToInt32( this._dragPane.Rect.X ), Convert.ToInt32( this._dragPane.Rect.Y ) ) );

                _dragPane.ReverseTransform( _dragStartPt, out x1, out xx1, out y1, out yy1 );
                _dragPane.ReverseTransform( mousePtF, out x2, out xx2, out y2, out yy2 );

                CurveList objects = new CurveList();

                double left = Math.Min( x1, x2 );
                double right = Math.Max( x1, x2 );

                double top = 0;
                double bottom = 0;

                for ( int i = 0; i < y1.Length; i++ )
                {
                    bottom = Math.Min( y1[i], y2[i] );
                    top = Math.Max( y1[i], y2[i] );
                }

                for ( int i = 0; i < yy1.Length; i++ )
                {
                    bottom = Math.Min( bottom, yy2[i] );
                    bottom = Math.Min( yy1[i], bottom );
                    top = Math.Max( top, yy2[i] );
                    top = Math.Max( yy1[i], top );
                }

                double w = right - left;
                double h = bottom - top;

                RectangleF rF = new RectangleF( (float)left, (float)top, (float)w, (float)h );

                _dragPane.FindContainedObjects( rF, this.CreateGraphics(), out objects );

                if ( Control.ModifierKeys == _selectAppendModifierKeys )
                    _selection.AddToSelection( _masterPane, objects );
                else
                    _selection.Select( _masterPane, objects );
                //				this.Select( objects );

                //Graphics g = this.CreateGraphics();
                //this._dragPane.AxisChange( g );
                //g.Dispose();

                #endregion
            }
            else   // It's a single-select
            {
                #region New Code to Single Select

                //Point mousePt = new Point( e.X, e.Y );

                int iPt;
                GraphPane pane;
                object nearestObj;

                using (IGraphics g = new GdiGraphics(CreateGraphics()))
                {
                    if ( MasterPane.FindNearestPaneObject( mousePt, g, out pane,
                                out nearestObj, out iPt ) )
                    {
                        if ( nearestObj is CurveItem && iPt >= 0 )
                        {
                            if ( Control.ModifierKeys == _selectAppendModifierKeys )
                                _selection.AddToSelection( _masterPane, nearestObj as CurveItem );
                            else
                                _selection.Select( _masterPane, nearestObj as CurveItem );
                        }
                        else
                            _selection.ClearSelection( _masterPane );

                        Refresh();
                    }
                    else
                    {
                        _selection.ClearSelection( _masterPane );
                    }
                }
                #endregion New Code to Single Select
            }

            using (IGraphics g2 = new GdiGraphics(CreateGraphics()))
            {
                // always AxisChange() the dragPane
                _dragPane.AxisChange( g2 );

                foreach ( GraphPane pane in _masterPane._paneList )
                {
                    if ( pane != _dragPane && ( _isSynchronizeXAxes || _isSynchronizeYAxes ) )
                        pane.AxisChange( g2 );
                }
            }

            Refresh();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets an enhanced metafile image for the current GraphPane, scaled to the specified size.
        /// </summary>
        /// <remarks>
        /// By definition, a Metafile is a vector drawing, and therefore scaling should not matter.
        /// However, this method is provided because certain options in Zedgraph, such as
        /// <see cref="IsFontsScaled" /> are affected by the size of the expected image.
        /// </remarks>
        /// <param name="width">The "effective" scaled width of the bitmap in pixels</param>
        /// <param name="height">The "effective" scaled height of the bitmap in pixels</param>
        /// <param name="isAntiAlias">true to use anti-aliased drawing mode, false otherwise</param>
        /// <seealso cref="GetImage()"/>
        /// <seealso cref="GetImage(int,int,float)"/>
        /// <seealso cref="GetMetafile()"/>
        public Metafile GetMetafile( int width, int height, bool isAntiAlias )
        {
            Bitmap bm = new Bitmap( 1, 1 );
            using ( Graphics g = Graphics.FromImage( bm ) )
            {
                IntPtr hdc = g.GetHdc();
                Stream stream = new MemoryStream();
                Metafile metafile = new Metafile( stream, hdc, _rect,
                            MetafileFrameUnit.Pixel, EmfType.EmfPlusDual );
                g.ReleaseHdc( hdc );

                using (IGraphics metafileGraphics = new GdiGraphics(Graphics.FromImage(metafile)))
                {
                    //metafileGraphics.TranslateTransform( -_rect.Left, -_rect.Top );
                    metafileGraphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;
                    PointF P = new PointF( width, height );
                    PointF[] PA = new PointF[] { P };
                    metafileGraphics.TransformPoints( CoordinateSpace.Page, CoordinateSpace.Device, PA );
                    //metafileGraphics.PageScale = 1f;

                    // output
                    MakeImage( metafileGraphics, width, height, isAntiAlias );
                    //this.Draw( metafileGraphics );

                    return metafile;
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets an enhanced metafile image for the current GraphPane.
        /// </summary>
        /// <seealso cref="GetImage()"/>
        /// <seealso cref="GetImage(int,int,float)"/>
        /// <seealso cref="GetMetafile(int,int)"/>
        public Metafile GetMetafile()
        {
            Bitmap bm = new Bitmap( 1, 1 );
            using ( Graphics g = Graphics.FromImage( bm ) )
            {
                IntPtr hdc = g.GetHdc();
                Stream stream = new MemoryStream();
                Metafile metafile = new Metafile( stream, hdc, _rect,
                            MetafileFrameUnit.Pixel, EmfType.EmfOnly );

                using (IGraphics metafileGraphics = new GdiGraphics(Graphics.FromImage(metafile)))
                {
                    metafileGraphics.TranslateTransform( -_rect.Left, -_rect.Top );
                    metafileGraphics.PageUnit = GraphicsUnit.Pixel;
                    PointF P = new PointF( _rect.Width, _rect.Height );
                    PointF[] PA = new[] { P };
                    metafileGraphics.TransformPoints( CoordinateSpace.Page, CoordinateSpace.Device, PA );
                    //metafileGraphics.PageScale = 1f;

                    // output
                    Draw( metafileGraphics );

                    g.ReleaseHdc( hdc );
                    return metafile;
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Build a <see cref="Bitmap"/> object containing the graphical rendering of
        /// all the <see cref="GraphPane"/> objects in this list.
        /// </summary>
        /// <value>A <see cref="Bitmap"/> object rendered with the current graph.</value>
        /// <seealso cref="GetImage(int,int,float)"/>
        /// <seealso cref="GetMetafile()"/>
        /// <seealso cref="GetMetafile(int,int)"/>
        public Bitmap GetImage( bool isAntiAlias )
        {
            Bitmap bitmap = new Bitmap( (int) _rect.Width, (int) _rect.Height );
            using (IGraphics bitmapGraphics = new GdiGraphics(Graphics.FromImage(bitmap)))
            {
                bitmapGraphics.TranslateTransform( -_rect.Left, -_rect.Top );
                Draw( bitmapGraphics );
            }

            return bitmap;
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets an image for the current GraphPane, scaled to the specified size and resolution.
        /// </summary>
        /// <param name="width">The scaled width of the bitmap in pixels</param>
        /// <param name="height">The scaled height of the bitmap in pixels</param>
        /// <param name="dpi">The resolution of the bitmap, in dots per inch</param>
        /// <param name="isAntiAlias">true for anti-aliased rendering, false otherwise</param>
        /// <seealso cref="GetImage()"/>
        /// <seealso cref="GetMetafile()"/>
        /// <seealso cref="GetMetafile(int,int)"/>
        /// <seealso cref="Bitmap"/>
        public Bitmap GetImage( int width, int height, float dpi, bool isAntiAlias )
        {
            Bitmap bitmap = new Bitmap( width, height );
            bitmap.SetResolution( dpi, dpi );
            using (IGraphics bitmapGraphics = new GdiGraphics(Graphics.FromImage(bitmap)))
            {
                MakeImage( bitmapGraphics, width, height, isAntiAlias );
            }

            return bitmap;
        }
Ejemplo n.º 12
0
        private void MakeImage( IGraphics g, int width, int height, bool antiAlias )
        {
            //g.SmoothingMode = SmoothingMode.AntiAlias;
            SetAntiAliasMode( g, antiAlias );

            // This is actually a shallow clone, so we don't duplicate all the data, curveLists, etc.
            PaneBase tempPane = ShallowClone();

            // Clone the Chart object for GraphPanes so we don't mess up the minPix and maxPix values or
            // the rect/ChartRect calculations of the original
            //RectangleF saveRect = new RectangleF();
            //if ( this is GraphPane )
            //	saveRect = ( this as GraphPane ).Chart.Rect;

            tempPane.ReSize( g, new RectangleF( 0, 0, width, height ) );

            tempPane.Draw( g );

            //if ( this is GraphPane )
            //{
            //	GraphPane gPane = this as GraphPane;
            //	gPane.Chart.Rect = saveRect;
            //	gPane.XAxis.Scale.SetupScaleData( gPane, gPane.XAxis );
            //	foreach ( Axis axis in gPane.YAxisList )
            //		axis.Scale.SetupScaleData( gPane, axis );
            //	foreach ( Axis axis in gPane.Y2AxisList )
            //		axis.Scale.SetupScaleData( gPane, axis );
            //}

            // To restore all the various state variables, you must redraw the graph in it's
            // original form.  For this we create a 1x1 bitmap (it doesn't matter what size you use,
            // since we're only mimicing the draw.  If you use the 'bitmapGraphics' already created,
            // then you will be drawing back into the bitmap that will be returned.

            Bitmap bm = new Bitmap( 1, 1 );
            using (IGraphics bmg = new GdiGraphics(Graphics.FromImage(bm)))
            {
                ReSize( bmg, Rect );
                SetAntiAliasMode( bmg, antiAlias );
                Draw( bmg );
            }
        }
        /// <summary>
        /// Rendering method used by the print context menu items
        /// </summary>
        /// <param name="sender">The applicable <see cref="PrintDocument" />.</param>
        /// <param name="e">A <see cref="PrintPageEventArgs" /> instance providing
        /// page bounds, margins, and a Graphics instance for this printed output.
        /// </param>
        private void Graph_PrintPage( object sender, PrintPageEventArgs e )
        {
            PrintDocument pd = sender as PrintDocument;

            MasterPane mPane = this.MasterPane;
            bool[] isPenSave = new bool[mPane.PaneList.Count + 1];
            bool[] isFontSave = new bool[mPane.PaneList.Count + 1];
            isPenSave[0] = mPane.IsPenWidthScaled;
            isFontSave[0] = mPane.IsFontsScaled;
            for ( int i = 0; i < mPane.PaneList.Count; i++ )
            {
                isPenSave[i + 1] = mPane[i].IsPenWidthScaled;
                isFontSave[i + 1] = mPane[i].IsFontsScaled;
                if ( _isPrintScaleAll )
                {
                    mPane[i].IsPenWidthScaled = true;
                    mPane[i].IsFontsScaled = true;
                }
            }

            RectangleF saveRect = mPane.Rect;
            SizeF newSize = mPane.Rect.Size;
            if ( _isPrintFillPage && _isPrintKeepAspectRatio )
            {
                float xRatio = (float)e.MarginBounds.Width / (float)newSize.Width;
                float yRatio = (float)e.MarginBounds.Height / (float)newSize.Height;
                float ratio = Math.Min( xRatio, yRatio );

                newSize.Width *= ratio;
                newSize.Height *= ratio;
            }
            else if ( _isPrintFillPage )
                newSize = e.MarginBounds.Size;

            mPane.ReSize( new GdiGraphics( e.Graphics ), new RectangleF( e.MarginBounds.Left,
                e.MarginBounds.Top, newSize.Width, newSize.Height ) );
            mPane.Draw(new GdiGraphics(e.Graphics));

            using (IGraphics g = new GdiGraphics(CreateGraphics()))
            {
                mPane.ReSize( g, saveRect );
                //g.Dispose();
            }

            mPane.IsPenWidthScaled = isPenSave[0];
            mPane.IsFontsScaled = isFontSave[0];
            for ( int i = 0; i < mPane.PaneList.Count; i++ )
            {
                mPane[i].IsPenWidthScaled = isPenSave[i + 1];
                mPane[i].IsFontsScaled = isFontSave[i + 1];
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="outputStream"></param>
        /// <param name="format"></param>
        /// <param name="bShowTransparency">if true, draw squares instead of leaving the
        /// background transparent</param>
        /// <remarks>
        /// bShowTransparency is set to true in design mode, to false otherwise.
        /// </remarks>
        protected MasterPane CreateGraph(Stream outputStream, ImageFormat format,
                    bool bShowTransparency)
        {
            var rect = new RectangleF(0, 0, Width, Height);
            var mp = new MasterPane(string.Empty, rect);
            mp.Margin.All = 0;
            mp.Fill.IsVisible = false;
            mp.Border.IsVisible = false;

            // create all required panes
            //for ( int i = 0; i < this.PaneCount; i++ )
            //{
            mp.Add(new GraphPane(rect, Title, string.Empty, string.Empty));
            //}

            // create output bitmap container
            var image = new Bitmap(Width, Height);
            IGraphics g = new GdiGraphics(Graphics.FromImage(image));

            // Apply layout plan
            //mp.SetLayout( this.PaneLayout );
            mp.ReSize(g, rect);

            // Use callback to gather more settings and data values
            OnDrawPane(g, mp);

            // Allow designer control of axischange
            if (AxisChanged)
            {
                mp.AxisChange(g);
            }

            // Render the graph to a bitmap
            if (bShowTransparency && mp.Fill.Color.A != 255)
            {
                //Show the transparency as white/gray filled squares
                // We need to add the resource namespace to its name
                //string resourceName = string.Format( "{0}.transparency.png", GetType().Namespace );
                string resourceName = "ZedGraph.ZedGraph.transparency.png";
                Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

                if (stream == null)
                {
                    throw new Exception("Does the Build Action of the resource " + resourceName + " is set to Embedded Resource ?");
                }

                using (Image brushImage = new Bitmap(stream))
                {
                    using (var brush = new TextureBrush(brushImage, WrapMode.Tile))
                    {
                        g.FillRectangle(brush, 0, 0, Width, Height);
                    }
                }
                stream.Close();
            }
            mp.Draw(g);

            // Stream the graph out
            var ms = new MemoryStream();
            image.Save(ms, format);

            //TODO: provide caching options
            ms.WriteTo(outputStream);

            return mp;
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Respond to the callback when the MasterPane objects are fully initialized.
 /// </summary>
 /// <param name="sender"></param>
 public void OnDeserialization(object sender)
 {
     var bitmap = new Bitmap(10, 10);
     using (IGraphics g = new GdiGraphics(Graphics.FromImage(bitmap)))
     {
         ReSize(g, _rect);
     }
 }
        private Point HandlePointValues( Point mousePt )
        {
            int iPt;
            GraphPane pane;
            object nearestObj;

            using (IGraphics g = new GdiGraphics(this.CreateGraphics()))
            {

                if ( _masterPane.FindNearestPaneObject( mousePt,
                    g, out pane, out nearestObj, out iPt ) )
                {
                    if ( nearestObj is CurveItem && iPt >= 0 )
                    {
                        CurveItem curve = (CurveItem)nearestObj;
                        // Provide Callback for User to customize the tooltips
                        if ( this.PointValueEvent != null )
                        {
                            string label = this.PointValueEvent( this, pane, curve, iPt );
                            if ( label != null && label.Length > 0 )
                            {
                                this.pointToolTip.SetToolTip( this, label );
                                this.pointToolTip.Active = true;
                            }
                            else
                                this.pointToolTip.Active = false;
                        }
                        else
                        {

                            if ( curve is PieItem )
                            {
                                this.pointToolTip.SetToolTip( this,
                                    ( (PieItem)curve ).Value.ToString( _pointValueFormat ) );
                            }
                            //							else if ( curve is OHLCBarItem || curve is JapaneseCandleStickItem )
                            //							{
                            //								StockPt spt = (StockPt)curve.Points[iPt];
                            //								this.pointToolTip.SetToolTip( this, ( (XDate) spt.Date ).ToString( "MM/dd/yyyy" ) + "\nOpen: $" +
                            //								spt.Open.ToString( "N2" ) +
                            //								"\nHigh: $" +
                            //								spt.High.ToString( "N2" ) + "\nLow: $" +
                            //								spt.Low.ToString( "N2" ) + "\nClose: $" +
                            //								spt.Close.ToString
                            //								( "N2" ) );
                            //							}
                            else
                            {
                                PointPair pt = curve.Points[iPt];

                                if ( pt.Tag is string )
                                    this.pointToolTip.SetToolTip( this, (string)pt.Tag );
                                else
                                {
                                    double xVal, yVal, lowVal;
                                    ValueHandler valueHandler = new ValueHandler( pane, false );
                                    if ( ( curve is BarItem || curve is ErrorBarItem || curve is HiLowBarItem )
                                            && pane.BarSettings.Base != BarBase.X )
                                        valueHandler.GetValues( curve, iPt, out yVal, out lowVal, out xVal );
                                    else
                                        valueHandler.GetValues( curve, iPt, out xVal, out lowVal, out yVal );

                                    string xStr = MakeValueLabel( curve.GetXAxis( pane ), xVal, iPt,
                                        curve.IsOverrideOrdinal );
                                    string yStr = MakeValueLabel( curve.GetYAxis( pane ), yVal, iPt,
                                        curve.IsOverrideOrdinal );

                                    this.pointToolTip.SetToolTip( this, "( " + xStr + ", " + yStr + " )" );

                                    //this.pointToolTip.SetToolTip( this,
                                    //	curve.Points[iPt].ToString( this.pointValueFormat ) );
                                }
                            }

                            this.pointToolTip.Active = true;
                        }
                    }
                    else
                        this.pointToolTip.Active = false;
                }
                else
                    this.pointToolTip.Active = false;

                //g.Dispose();
            }
            return mousePt;
        }
Ejemplo n.º 17
0
        private void PanelPaint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            GdiGraphics gg = new GdiGraphics(g);

            Render(gg);

            g.Flush();
        }
        /// <summary>
        /// A threaded version of the copy method to avoid crash with MTA
        /// </summary>
        private void ClipboardCopyThreadEmf()
        {
            using (Graphics g = this.CreateGraphics())
            {
                IntPtr hdc = g.GetHdc();
                Metafile metaFile = new Metafile(hdc, EmfType.EmfPlusOnly);
                g.ReleaseHdc(hdc);

                IGraphics gMeta = new GdiGraphics(Graphics.FromImage(metaFile));
                {
                    this._masterPane.Draw( gMeta );
                }

                //IntPtr hMeta = metaFile.GetHenhmetafile();
                ClipboardMetafileHelper.PutEnhMetafileOnClipboard( this.Handle, metaFile );
                //System.Windows.Forms.Clipboard.SetDataObject(hMeta, true);

                //g.Dispose();
            }
        }