0

I have a method where I want to copy the contents of one object to another so modifying one won't change the other. So as discussed here I need to clone the two objects: Clone two objects.

But my problem is the LightningChartUltimate class is from a dll. And I can't make it implement IClonable. Can anyone tell me what my options are to create a copy of the chart so any changes to the new object won't affect the chart?

public void SaveImage(string fileName,
                      TargetImageFormat imgFormat,
                      double width, double height, double fontscale,
                      bool launchExplorerAfterSave,LightningChartUltimate chart)
{
    double originalMarkerFontSize = 0;

    try
    {
        if (File.Exists(fileName))
            File.Delete(fileName);

        TargetImageFormat imageFormat = imgFormat;

        BitmapAntialiasOptions option = new BitmapAntialiasOptions();
        option.ActualPixelWeight = 1;
        option.ResolutionDivider = 1;
        option.BlurRadius = 0;

        LightningChartUltimate TempChart = new LightningChartUltimate();
        TempChart = chart; //THE PROBLEM IS IF I UPDATE TempChart IT INTERNALLY AFFECTS chart object. AND chart is being used at so many other places.

        originalMarkerFontSize = m_ChartFontForSeriesEventMarker.Size;
        TempChart.Title.Font.Size = TempChart.Title.Font.Size * fontscale;
        TempChart.ViewXY.XAxes[0].Title.Font.Size = TempChart.ViewXY.XAxes[0].Title.Font.Size * fontscale;
        TempChart.ViewXY.YAxes[0].Title.Font.Size = TempChart.ViewXY.YAxes[0].Title.Font.Size * fontscale;
        TempChart.ViewXY.XAxes[0].LabelsFont.Size = TempChart.ViewXY.XAxes[0].LabelsFont.Size * fontscale;
        TempChart.ViewXY.YAxes[0].LabelsFont.Size = TempChart.ViewXY.YAxes[0].LabelsFont.Size * fontscale;
        TempChart.ViewXY.LegendBoxes[0].SeriesTitleFont.Size *= fontscale;

        SeriesEventMarker marker = new SeriesEventMarker();
        marker.Label.Font.Size = m_ChartFontForSeriesEventMarker.Size * fontscale;

        TempChart.Width = width;
        TempChart.Height = height;

        using (FileStream stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
        {
            TempChart.SaveToStream(stream, imageFormat, option,
                    Convert.ToInt32(width), Convert.ToInt32(height));
                    //chart.SaveToFile(string.Format("{0}.{1}",fileName,imgFormat.ToLower()), option, (int)imageSaveSize.Width, (int)imageSaveSize.Height);
        }
    }
    catch (Exception exception)
    {
        SystemDebugLogLogger.LogError(exception, "LightningChartUserControl:Error in SaveImage");
        throw exception;
    }
    finally
    {
        m_ChartFontForSeriesEventMarker.Size = originalMarkerFontSize;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nikhil ANS
  • 53
  • 1
  • 8
  • Well, i don't know LightningChartUltimate, but unless it has some Clone or deep-copy method/facility, you will have to create a second LightningChartUltimate object to be used in your cloned object. Your code will have to setup this second LightningChartUltimate with a copy of the data from the first chart, and with the same parameters as the original LightningChartUltimate (as far as necessary). :( –  Mar 30 '18 at 17:59
  • Hmm yea I was thinking of the same but thought if there was a different approach. Thanks anyway I will try the same. – Nikhil ANS Mar 30 '18 at 18:00
  • You can create your own copy routine by using reflection and looping through all the properties and copy to the new object. I do this all the time in WCF when i need to use DTO objects and go back and forth to Entity objects. – Kevbo Mar 30 '18 at 18:17

0 Answers0