Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / Visual Basic

AGE, Another Graphic Engine in .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (36 votes)
10 May 2007CPOL8 min read 136.5K   5.9K   170  
A library that allows some GDI+ manipulation at runtime in an easy way
/////////////////////////////////////////////////////////////////////////////////
// NeoDataType Another Graphic Engine
// --------------------
// Project Copyright (C)    : Fabio Zanetta, email: support@neodatatype.net
// Portions Copyright (C)   : Microsoft Corporation. All Rights Reserved.
// License                  : docs/license.txt
// ------------------------------------------------------------------------------
// File created by          : Fabio Zanetta, email: support@neodatatype.net
// ------------------------------------------------------------------------------
// Please, if you modify some parts of this file mark them as described in
// docs/modify_guidelines.txt
/////////////////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Collections.Generic;
using NeoDataType.Documents;

namespace NeoDataType.Graphic
{

    #region GraphicLayer

    [SaveThisClass()]
    public class GraphicLayer : DocumentGroup
    {

        bool _visible = true;
        GraphicItemCollection _items = new GraphicItemCollection();
        bool _refreshItemsCache = true;

        internal void RequireRefreshItemList()
        {
            _refreshItemsCache = true;
        }


        [SaveThisProperty]
        public bool Visible
        {
            get { return _visible; }
            set
            {
                _visible = value;
                PropertyChanged();
            }
        }

        protected void PropertyChanged()
        {
            if (Document != null)
            {
                ((GraphicDocument)Document).NotifyPropertyChanged(this);
            }
        }

        /// <summary>
        /// Collection of GraphicItem in the layer
        /// </summary>
        public GraphicItemCollection GraphicItems
        {
            get
            {

                if (_refreshItemsCache)
                {
                    _items.Clear();

                    foreach (DocumentItem di in Items)
                    {
                        if (di is GraphicItem)
                        {
                            _items.Add((GraphicItem)di);
                        }
                    }

                    _refreshItemsCache = false;
                }

                return _items;
            }
        }

        /// <summary>
        /// Save the GraphicDocument as an image file.
        /// </summary>
        public Image GetImage()
        {
            Rectangle docBounds = ((GraphicDocument)Document).GetBounds();          
            Bitmap bmp;
            if (!docBounds.IsEmpty)
            {
                bmp = new Bitmap(docBounds.Width, docBounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                Graphics g = Graphics.FromImage(bmp);

                SolidBrush back = new SolidBrush(Color.White);
                g.FillRectangle(back, 0, 0, docBounds.Width, docBounds.Height);
                g.TranslateTransform(-docBounds.X, -docBounds.Y);
                foreach (GraphicItem item in GraphicItems)
                    if (item.Visible)
                        item.Painter.PaintItem(g);
                back.Dispose();
                g.Dispose();
            }
            else
            {
                bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            }
            return bmp;
        }

        /// <summary>
        /// Shift the group down in the group list.
        /// </summary>
        public void MoveDown()
        {
            if (Document != null)
            {
                int i = Document.GroupsInternal.IndexOf(this);
                if (i < Document.GroupsInternal.Count - 1)
                {
                    Document.GroupsInternal.Move(this, i + 1);
                    DocumentActionEventArgs e = new DocumentActionEventArgs(Document, this, null);
                    OnMoved(e);
                }
            }
        }

        /// <summary>
        /// Shift the group up in the group list.
        /// </summary>
        public void MoveUp()
        {
            if (Document != null)
            {
                int i = Document.GroupsInternal.IndexOf(this);
                if (i > 0)
                {
                    Document.GroupsInternal.Move(this, i - 1);
                    DocumentActionEventArgs e = new DocumentActionEventArgs(Document, this, null);
                    OnMoved(e);
                }
            }
        }

        
        public override void OnItemAdded(DocumentActionEventArgs e)
        {
            base.OnItemAdded(e);
            _refreshItemsCache = true;
            PropertyChanged();
        }

        public override void OnItemRemoved(DocumentActionEventArgs e)
        {
            base.OnItemRemoved(e);
            _refreshItemsCache = true;
            PropertyChanged();
        }

        protected override void OnMoved(DocumentActionEventArgs e)
        {
            base.OnMoved(e);
            ((GraphicDocument)Document).RequireRefreshLayerList();
            PropertyChanged();
        }
    }
    #endregion

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions