Changing custom bitmap on redraw?

Topics: Windows Forms
Sep 25, 2013 at 1:21 PM
When creating a gMarker, I use a custom bitmap for it but afterwards, I update the marker's position via it's position attribute.

Is there a way to also change the marker's bitmap when updating it's position as well?
Sep 25, 2013 at 1:53 PM
just change it
Sep 25, 2013 at 2:29 PM
radioman wrote:
just change it
How?
Sep 25, 2013 at 4:12 PM
marker.MyImage = myImage;
Sep 25, 2013 at 4:22 PM
Thanks, radioman!
Sep 28, 2013 at 11:49 AM
Radioman,

I don't see an attribute for my marker named .MyImage so I am unable to set = myImage. Only stuff I see are things such as position, bearing, tag, tooltip, ishittestvisible, etc.
Oct 4, 2013 at 7:08 AM
radioman, can you help? I don't see a .MyImage attribute for my marker...
Oct 4, 2013 at 8:19 AM
just add it
Oct 4, 2013 at 8:34 AM
Edited Oct 4, 2013 at 8:34 AM
radioman wrote:
just add it
How? :-S
Oct 4, 2013 at 3:22 PM
Edited Oct 4, 2013 at 3:23 PM
You have to create a custom marker class, or change the GMarkerGoogle class. Here's some code for a custom image marker class (you can also rotate the image), so you get the idea:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using GMap.NET.WindowsForms;
using GMap.NET;
using System.Diagnostics;

namespace GMapEnhanced
{
    public class GMapMarkerImage : GMapMarker
    {

        private Size imgSize = Size.Empty;
        /// <summary>
        /// Gets or sets the Image size
        /// </summary>
        public Size ImageSize
        {
            get
            {
                return imgSize;
            }
            set
            {
                imgSize = value;
                this.Size = new System.Drawing.Size(imgSize.Width, imgSize.Height);
                this.Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
            }
        }

        private bool bDrawBorder = false;
        /// <summary>
        /// gets or sets a value if a border shall be drawn around the image
        /// </summary>
        public bool DrawBorder
        {
            get { return bDrawBorder; }
            set 
            {
                bDrawBorder = value;
                if (bDrawBorder)
                {
                    this.Size = new System.Drawing.Size(imgSize.Width + 2 * (int)penBorder.Width, imgSize.Height + 2 * (int)penBorder.Width);
                    this.Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
                }
                else
                {
                    this.Size = new System.Drawing.Size(imgSize.Width, imgSize.Height);
                    this.Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);
                }
            }
        }


        private Pen penBorder = new Pen(Color.Red, 2f);
        /// <summary>
        /// The Pen for the Image border
        /// </summary>
        public Pen PenBorder
        {
            get { return penBorder; }
            set 
            {
                penBorder = value;

            }
        }


        /// <summary>
        /// ToolTip position in local coordinates
        /// </summary>
        public override Point ToolTipPosition
        {
            get
            {
                Point ret = area.Location;
                ret.Offset(-Offset.X, 0);
                return ret;
            }
        }


        private float rot;
        /// <summary>
        /// Gets or sets the Rotation Angle in degrees
        /// </summary>
        public float RotationAngle
        {
            get { return rot; }
            set 
            { 
                rot = value;
                if (Overlay != null && Overlay.Control != null && !Overlay.Control.HoldInvalidation)
                {
                    Overlay.Control.Refresh();
                }
            }
        }


        private Image img;
        /// <summary>
        /// The image to display as a marker.
        /// </summary>
        public Image MarkerImage
        {
            get
            {
                return img;
            }
            set
            {
                img = value;
                if (img != null)
                {
                    ImageSize = img.Size;
                    //Offset = new System.Drawing.Point(-Size.Width / 2, -Size.Height / 2);

                    if (Overlay != null && Overlay.Control != null)
                    {
                        Overlay.Control.UpdateMarkerLocalPosition(this);
                    }
                }
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="p">The position of the marker</param>
        public GMapMarkerImage(PointLatLng p, Image image)
            : base(p)
        {
            img = image;
            ImageSize = img.Size;
            rot = 0f;
        }


        /// <summary>
        /// Overload with Angle
        /// </summary>
        /// <param name="p"></param>
        /// <param name="image"></param>
        /// <param name="typ"></param>
        /// <param name="angle"></param>
        public GMapMarkerImage(PointLatLng p, Image image, float angle)
            : this(p, image)
        {
            rot = angle;
        }

        /// <summary>
        /// Override Render function
        /// </summary>
        /// <param name="g"></param>
        public override void OnRender(Graphics g)
        {
            var tmp = g.InterpolationMode;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            if (rot != 0f)
            {
                GraphicsState savedState = g.Save();
                g.ResetTransform();
                Matrix transformationMatrix = new Matrix();
                transformationMatrix.RotateAt(rot, new PointF(LocalAreaInControlSpace.Location.X - Offset.X, LocalAreaInControlSpace.Location.Y - Offset.Y));
                g.Transform = transformationMatrix;
                g.DrawImage(img, LocalAreaInControlSpace.Location.X, LocalAreaInControlSpace.Location.Y, imgSize.Width, imgSize.Height);
                g.Restore(savedState);
            }
            else
            {
                if (bDrawBorder)
                {
                    g.DrawRectangle(penBorder, LocalPosition.X, LocalPosition.Y, this.Size.Width - (int)(penBorder.Width / 2f), this.Size.Height - (int)(penBorder.Width / 2f));
                    g.DrawImage(img, LocalPosition.X + penBorder.Width, LocalPosition.Y + penBorder.Width, imgSize.Width, imgSize.Height);
                }
                else
                {
                    g.DrawImage(img, LocalPosition.X, LocalPosition.Y, imgSize.Width, imgSize.Height);

                }
            }

            g.InterpolationMode = tmp;
        }

        bool disposed = false;
        /// <summary>
        /// Dispose all resources
        /// </summary>
        public override void Dispose()
        {
            if (!disposed)
            {
                disposed = true;
                if (MarkerImage != null)
                {
                    MarkerImage.Dispose();
                    MarkerImage = null;
                }
                base.Dispose();
            }
        }
    }
}