4

I'm using MS Chart in my WindowsForms app and I want to have a lot of different markers visible on chart. In principle it's done by specifying a MarkerImage for Series or DataPoint. The problem is that the MarkerImage property is of string type with no documentation for it's usage. According to the MS Chart samples, it is the path to the image file.

I managed to make it work by putting the images into project/images folder, specifying 'Copy to Output Directory' for them, and then using series.MarkerImage = "images/image.bmp"; in the code. It works in principle but in runtime when it comes to first displaying the marker image on chart, the application freezes for some time. I assume it's because of a disk access time for loading a new file. And it happens every time a new image should first appear on screen, making the application really unresponsive until all the images are cached.

What I'm looking for is some solution to improve this. At best I'd prefer to have all the images stored as embedded resources, which is much better than having to ship additional folder of images with the app. According to documentation for the LegendCell.Image (which have a similar meaning) the string is actually an URL. I found that it's possible to get URL for the embedded resource in a ASP.NET applications using Page.ClientScript.GetWebResourceUrl, but my application if just plain WindowsForms. Is it possible to get some kind of URL for the embedded resource in windows forms?

I am already thinking of implementing my own Series or DataPoint descendants to handle resource references, but does not seem to be a right way, as I see nothing related to actual rendering of the Series that I can override...

Another solution I think of is just pre-loading all the image files myself at the application start, but this seem to be a hack. And what if I want to provide the markers together with a separate assembly (.dll)?

Update. I tried:

  • Preloading images with File.ReadAllBytes. Does not speedup anything.
  • Using Data URI. Fails with exception with text "The given path's format is not supported."
Mikhail
  • 1,223
  • 14
  • 25

1 Answers1

1

Even though it might be too late for you, this answer might help people like me who share a similar problem nowerdays.

The MSChart Control offers an Images-Property that is a collection of NamedImages. Those images are a combination of an unique identifier and an image that can be loaded from your resources:

  1. If not done yet - Create a Resources file and add it to your project
  2. Add the Images to that file in combination with unique identifiers (e.g. MyImage). Ensure that you set the Persistence-property of the images you want to embed to "Embedded in .resx"
  3. In your Code define NamedImages that you add to your Chart.Images-collection.

    NamedImage namedImage1 = new NamedImage("UniqueIdentifier", Resources.MyImage); Chart.Images.Add(namedImage1);

In your chart you can now use UniqueIdentifier where usually a path to an icon is expected.

This prevents loading the image from disk and might speed up your application as the image is cached as resource.

best regards

Florian Grummel
  • 332
  • 3
  • 14
  • Thank you for the answer, but I have no way to check if it actually works right now. The project I was working on is too outdated. – Mikhail Dec 26 '15 at 10:57
  • I don't understand how do you set MarkerImage? If I try with `seriesWind.Points[cnt].MarkerImage = "UniqueIdentifier"` it says that this resource is already defined. – c0dehunter Jun 02 '16 at 09:04