Search code examples
c#teechart

How to clip some part TeeChart image


I don't have that much deep knowledge in c#. I am using TeeChart to plot chart. I am able to save image of chart in .jpg, .bmp e.t.c. What i need to do is before going to save I want to clip some part of image and then save rest as it is without changing pixel information or any other things. enter image description here

I want to clip inside the block box part. the remaining graph should be as it is. In the same way i can able to clip end part graph also if i want. there should be no change in pixel or height of the image. as well the remaining image should cover the entire graph. Is it possible. Can any one help me please how to do it.


Solution

  • You can get the chart drawing area coordinates from tChart1.Chart.ChartRect. Here's an example clipping the chart legend into an image:

    public Form1()
    {
      InitializeComponent();
      InitializeChart();
    }
    
    private Bitmap chartBmp;
    
    private void InitializeChart()
    {
      tChart1.Series.Add(new Steema.TeeChart.Styles.Bar()).FillSampleValues();
    
      chartBmp = tChart1.Bitmap;
    
      tChart1.GetLegendRect += tChart1_GetLegendRect;
    }
    
    void tChart1_GetLegendRect(object sender, Steema.TeeChart.GetLegendRectEventArgs e)
    {
      Rectangle cropRect = e.Rectangle;
      Bitmap legendImg = new Bitmap(cropRect.Width, cropRect.Height);
    
      using (Graphics g = Graphics.FromImage(legendImg))
      {
        g.DrawImage(chartBmp, new Rectangle(0, 0, legendImg.Width, legendImg.Height),
                         cropRect,
                         GraphicsUnit.Pixel);
      }
    
      legendImg.Save(@"c:\temp\legend.png");
    }