Click here to Skip to main content
15,895,793 members
Articles / Web Development / ASP.NET

An Open Source RDL Engine

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
20 Dec 2010CPOL3 min read 83.8K   3.5K   55  
An Open Source RDL engine for rendering reports to WinForms or ASP.NET
/*-----------------------------------------------------------------------------------
This file is part of the SawikiSoft RDL Engine.
The SawikiSoft RDL Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

The SawikiSoft RDL Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Rdl.Engine
{
    public class Image : ReportItem
    {
        protected enum ImageSourceEnum
        {
            External,
            Embedded,
            Database
        }
        public enum ImageSizingEnum
        {
            Autosize,
            Fit,
            FitProportional,
            Clip,
        }

        protected ImageSourceEnum _source = ImageSourceEnum.External;
        internal Expression _value = null;
        internal Expression _mIMEType = null;
        protected ImageSizingEnum _sizing = ImageSizingEnum.Autosize;
        internal int _imageIndex = -1;

        public Image(XmlNode node, ReportElement parent)
            : base(node, parent)
        {
        }

        protected override void ParseAttribute(XmlNode attr)
        {
            base.ParseAttribute(attr);
            switch (attr.Name.ToLower())
            {
                case "source":
                    _source = (ImageSourceEnum)Enum.Parse(typeof(ImageSourceEnum), attr.InnerText, true);
                    break;
                case "value":
                    _value = new Expression(attr, this);
                    break;
                case "mimetype":
                    _mIMEType = new Expression(attr, this);
                    break;
                case "sizing":
                    _sizing = (ImageSizingEnum)Enum.Parse(typeof(ImageSizingEnum), attr.InnerText, true);
                    break;
                default:
                    break;
            }
        }

        public System.Drawing.Image GetImage(Rdl.Runtime.Context context)
        {
            System.Drawing.Image img = null;
            string name;

            switch (_source)
            {
                case ImageSourceEnum.Database:
                    img = System.Drawing.Image.FromStream(new MemoryStream((byte[])_value.Exec(context)));
                    break;
                case ImageSourceEnum.Embedded:
                    name = _value.ExecAsString(context);
                    try
                    {
                        EmbeddedImage e = Report.GetEmbeddedImage(name);
                        img = System.Drawing.Image.FromStream(new MemoryStream(e.ImageData));
                    }
                    catch (Exception err)
                    {
                        throw new Exception("Error locating embedded image " + name, err);
                    }
                    break;
                case ImageSourceEnum.External:
                    name = _value.ExecAsString(context);
                    try
                    {
                        Stream s = null;
                        if (name.Contains("://"))
                            s = System.Net.HttpWebRequest.Create(name).GetResponse().GetResponseStream();
                        else
                            s = new FileStream(name, FileMode.Open, FileAccess.Read);
                        img = System.Drawing.Image.FromStream(s);
                        s.Close();
                        s.Dispose();
                    }
                    catch (Exception err)
                    {
                        throw new Exception("Error loading image file " + name, err);
                    }
                    break;
            }

            return img;
        }

        public string MIMEType(Rdl.Runtime.Context context)
        {
            System.Drawing.Image img = GetImage(context);

            return MIMEType(img);
        }

        private string MIMEType(System.Drawing.Image img)
        {
            if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
                return "image/bmp";
            if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
                return "image/gif";
            if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
                return "image/jpeg";
            if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
                return "image/png";

            throw new Exception("Unknown image type " + img.RawFormat.ToString());
        }

        protected override void Render1(Rdl.Render.Container parentBox, Rdl.Runtime.Context context, bool visible)
        {
            if (visible && parentBox != null)
            {
                _box = parentBox.AddImageElement(this, Style, context);
                _box.Name = "Image";
            }
        }

        protected override void Render2(Rdl.Runtime.Context context)
        {
            if (_box != null)
            {
                Rdl.Render.ImageElement ie = (Rdl.Render.ImageElement)_box;

                ie._imageIndex = ie.Render.AddImage(this, context);

                if (_sizing == ImageSizingEnum.Autosize)
                {
                    ie.Width = ie.Render.ImageList[ie._imageIndex].imageData.Width;
                    ie.Height = ie.Render.ImageList[ie._imageIndex].imageData.Height;
                }
            }
        }

        public virtual ImageSizingEnum Sizing
        {
            get { return _sizing; }
        }
    }
}

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
Software Developer (Senior) Sawiki Software
United States United States
I have been a professional software developer for 25+ years, most of it supporting the business community of Maine. I have been working with MS dotnet since 1.0 beta. I organized Sawiki Software LLC as an outlet for some open source software that I have been working on.

Comments and Discussions