예제 #1
0
        public void Draw(IViewport view, Graphics canvas)
        {
            // This object is readonly.
            Debug.Assert(view != null, "It is necessary to have viewport to draw!");
            Debug.Assert(canvas != null, "It is necessary to have canvas to draw!");

            if (viewport.Equals(view))
            {
                canvas.DrawImage(data, new Point(0, 0));
            }
            else
            {
                var x1 = Math.Max(view.LeftCorner, viewport.LeftCorner);
                var x2 = Math.Min(view.RightCorner, viewport.RightCorner);
                var y1 = Math.Max(view.BottomCorner, viewport.BottomCorner);
                var y2 = Math.Min(view.TopCorner, viewport.TopCorner);

                if ((x1 < x2) && (y1 < y2))
                {
                    // Image is at least partly on the screen.
                    var destLeft = view.XToCanvasX(x1);
                    var destTop  = view.YToCanvasY(y2);
                    var srcLeft  = viewport.XToCanvasX(x1);
                    var srcTop   = viewport.YToCanvasY(y2);

                    canvas.DrawImage(data,
                                     new Rectangle(destLeft, destTop, view.XToCanvasX(x2) - destLeft,
                                                   view.YToCanvasY(y1) - destTop),
                                     new Rectangle(srcLeft, srcTop, viewport.XToCanvasX(x2) - srcLeft,
                                                   viewport.YToCanvasY(y1) - srcTop), GraphicsUnit.Pixel);
                }
            }
        }