Search code examples
c#mappinggismapxtreme

How can I make a Bitmap from MapXtreme Styles


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


Solution

  • 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;
        }