tags:

views:

390

answers:

2

I have posted this question on The MapXtreme forum but since nobody ever answers questions there I am hoping someone here has some experience with this product (mapxtreme is a GIS SDK made by the people who make MapInfo)

I am working on a MapXtreme Desktop app and we need bitmaps of our features styles

I have tried two ways but all I get is a grey bitmap with a dark X.

here is the code I have used both ways are in the code but one is commented out:

    public static Bitmap GetStyleBitmap(Style style)
    {
        var bm = new Bitmap(16, 16, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        var rect = new System.Drawing.Rectangle(0, 0, 16, 16);
        var ss = new StyleSample();
        ss.Bounds = rect;
        if (style is CompositeStyle)
        {
            ss.ApplyAreaStyle(((CompositeStyle)style).AreaStyle);
            ss.ApplyLineStyle(((CompositeStyle)style).LineStyle);
        }
        if (style is AreaStyle)
        {
            ss.ApplyAreaStyle((AreaStyle)style);
        }
        if (style is SimpleLineStyle)
        {
            ss.ApplyLineStyle((SimpleLineStyle)style);
        }

        //using MapExport
        var me = new MapExport(ss.Map);
        var image = me.Export();
        return new Bitmap(image);

        //using StyleSample.DrawToBitmap
        //ss.DrawToBitmap(bm, rect);
        //return bm;
    }

TIA

A: 

After waiting for an answer - and trying countless other ways - all to no avail, I decided on doing it all 'by hand' ie I simply look in the style object get the colour of it and draw a bitmap appropriate for the layer type (line or polygon).

It doesnt handle every case and also doesnt handle line styles or interior colours but it serves my purposes for now.

here is the code that does it.

    public static Bitmap GetStyleBitmap(FeatureLayer fl)
    {
        Feature f = GetFirstFeature(fl);
        if (f == null) return null;

        var style = f.Style;
        Color c;
        var bm = new Bitmap(16, 16, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        PointF[] poly = new PointF[] 
        {
            new PointF(2,5),
            new PointF(5,2),
            new PointF(14,7),
            new PointF(14,14),
            new PointF(2,14),
            new PointF(2,4)
        };

        SimpleLineStyle line = null;
        if (style is CompositeStyle)
            line = ((CompositeStyle)style).AreaStyle.Border as SimpleLineStyle;
        if (style is AreaStyle)
            line = ((AreaStyle)style).Border as SimpleLineStyle;

        if (line != null)
        {
            c = line.Color;

            using (var gr = Graphics.FromImage(bm))
            {
                gr.DrawPolygon(new Pen(c, 2), poly);
            }
            return bm;
        }

        line = style as SimpleLineStyle;

        if (line != null)
        {
            c = line.Color;

            using (var gr = Graphics.FromImage(bm))
            {
                gr.DrawLine(new Pen(c, 2), new PointF(2,2), new PointF(14,14));
            }
        }
        return bm;
    }
Gabe
A: 

The first code already almost worked. I just tweaked it a little bit to fix it. I have tested it for a composite style that contains a simplevectorpointstyle in it.

    /// <summary>
    /// Creates an icon for the specified style.
    /// </summary>
    /// <param name="style">The style.</param>
    /// <returns></returns>
    private static Bitmap CreateStyleIcon(Style style)
    {
        const int iconSize = 16; //the size of the icon
        System.Drawing.Rectangle iconArea = new System.Drawing.Rectangle(0, 0, iconSize, iconSize); //a rectangle area for the icon
        StyleSample ss = new StyleSample { Bounds = iconArea };
        if (style is CompositeStyle)
        {
            CompositeStyle compsiteStyle = style as CompositeStyle;
            if (compsiteStyle.AreaStyle != null) //do we have an area style?
            {
                ss.ApplyAreaStyle(compsiteStyle.AreaStyle);
            }
            if (compsiteStyle.LineStyle != null) //do we have an LineStyle style?
            {
                ss.ApplyLineStyle(compsiteStyle.LineStyle);
            }
            if (compsiteStyle.SymbolStyle != null) //do we have an SymbolStyle style?
            {
                ss.ApplySymbol(compsiteStyle.SymbolStyle);
            }
        }
        if (style is AreaStyle)
        {
            ss.ApplyAreaStyle((AreaStyle)style);
        }
        if (style is BaseLineStyle)
        {
            ss.ApplyLineStyle((BaseLineStyle)style);
        }

        //draw the bitmap
        Bitmap iconBitmap = new Bitmap(iconSize, iconSize, System.Drawing.Imaging.PixelFormat.Format32bppArgb);//the bitmap to draw the icon to
        ss.DrawToBitmap(iconBitmap, iconArea);
        return iconBitmap;
    }
Marcel