Harsh Kumar Singhi
Harsh Kumar Singhi

Reputation: 201

Load Marker image in microsoft chart control from resources

Platform : C# IDE: Microsoft Visual Studio 2010

I am trying to load marker image at a point in chart control from Resources path, but its not able to load the file path. Any suggestions ?

foreach (var pt in chart1.Series["Series1"].Points)
{
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        if (dr["OverNo"].Equals(pt.XValue) && Convert.ToInt32(dr["Fow"]) > 0)
        {
            //   pt.XValue +=5;
            if (Convert.ToInt32(dr["Fow"]) > 0)
            {
                //pt.MarkerImage = s + SC.whiteBall; // this works fine 
                //pt.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;

                if (Convert.ToInt32(dr["Fow"]) == 1)
                {
                    //   Bitmap b = new Bitmap(Properties.Resources.WhiteBall1);
                    pt.MarkerImage = s + SC.whiteBall;
                    pt.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
                }
            }
        }
    }
}

Error : ImageLoader - Cannot load image from this location: System.Drawing.Bitmap in Program.cs

Upvotes: 1

Views: 1973

Answers (2)

Cristian Scutaru
Cristian Scutaru

Reputation: 1507

You can actually use an embedded resource, instead of a file copied to your output directory:

var myImage = new NamedImage("my_image_key", Properties.Resources.my_image);
myChartControl.Images.Add(myImage);
mySeries.MarkerImage = "my_image_key";
  1. Load your image file (like my_image.png) as a resource.
  2. Add it to the Images collection of the chart control, through a NamedImage object.
  3. Pass the key name used for your object into the MarkerImage series/point property.

PS: Setting your resource file to Embedded Resource and the MarkerStyle to None seem optional.

Upvotes: 0

andrei.ciprian
andrei.ciprian

Reputation: 3025

  1. Include the image file as content in your application, copy to output directory.
  2. Use the full path for MarkerImage like MainApplicationPath\Images\yourImage.bmp. Check File.Exists.
  3. You can use the MarkerImage property on both data points and series. If you use a particular image for your data series all data points will inherit that. You can override it for specific data points.
  4. Use MarkerStyle.None when using custom images.
  5. If switching from custom images to regular markers and back use DeleteCustomProperty("MarkerImage") and DeleteCustomProperty("MarkerStyle") on your data points, then reset.

Upvotes: 1

Related Questions