chartクラスで作成した描画する際、通常の設定では表示できない線の種類がある且つ出力先によってDPIが異なり、描画時、以下処理にてChartGraphicsをカスタムした線、解像度を修正してから出力しております。

しかし、出力あたって、現状以下の課題がございます。

  * グラフの数だけ処理を行う必要があるため、数が多い場合、処理に時間が掛かっている
  * PDFなどで出力した際、容量が大きくなる

出力速度と容量を改善したいのですが、何か良い方法はありますでしょうか。

宜しくお願い致します。

**現状のコード:**

```

    try {
    
        ChartGraphics cg = e.ChartGraphics;
    
        if((e.ChartElement is Series)) {
    
            RectangleF backup = cg.Graphics.ClipBounds;
    
            float basex = 0;
            float basey = 2005;
            float baseWidth = 4107;
            float baseHeight = 3543;
            int baseDpi = 600;
    
            float calcDpi = baseDpi / cg.Graphics.DpiX;
    
            RectangleF clipRect = new RectangleF(basex / calcDpi, basey / calcDpi, baseWidth / calcDpi, baseHeight / calcDpi);
    
            cg.Graphics.SetClip(clipRect);
            foreach(Series s in e.Chart.Series) {
                if(e.ChartElement == s) {
                    if(s.BorderDashStyle != ChartDashStyle.Solid) {
                        s.BorderDashStyle = ChartDashStyle.NotSet;
    
                        Color penColor = s.BorderColor;
                        int borderWidth = s.BorderWidth;
                        s.BorderWidth = 0;
    
                        string borderStyleListValue = "";
                        foreach(KeyValuePair<string, string> kvp in borderStyleList) {
                            if(kvp.Key == s.Name) {
                                borderStyleListValue = kvp.Value;
                                break;
                            }
                        }
    
                        Pen blackPen = new Pen(penColor, borderWidth);
    
                        switch(borderStyleListValue) {
                            case "2":
                                blackPen.DashPattern = Const.Border_Dash_Point.Long_Dash;
                                break;
                            case "3":
                                blackPen.DashPattern = Const.Border_Dash_Point.Point_Dash;
                                break;
                            case "4":
                                blackPen.DashPattern = Const.Border_Dash_Point.One_Point_Dash;
                                break;
                            case "5":
                                blackPen.DashPattern = Const.Border_Dash_Point.Two_Point_Dash;
                                break;
                            case "6":
                                blackPen.DashPattern = Const.Border_Dash_Point.Short_Dash;
                                break;
                            case "7":
                                blackPen.DashPattern = Const.Border_Dash_Point.Short_Point_Dash;
                                break;
                            default:
                                break;
                        }
                        int maxCount = 27;
                        for(int i = 0; i < s.Points.Count - 1; i++) {
                            if(maxCount >= s.Points[i + 1].XValue) {
                                System.Drawing.PointF pos = System.Drawing.PointF.Empty;
                                pos.X = (float)cg.GetPositionFromAxis(s.ChartArea, AxisName.X, s.Points[i].XValue);
                                pos.Y = (float)cg.GetPositionFromAxis(s.ChartArea, AxisName.Y, s.Points[i].YValues[0]);
    
                                System.Drawing.PointF pos2 = System.Drawing.PointF.Empty;
                                pos2.X = (float)cg.GetPositionFromAxis(s.ChartArea, AxisName.X, s.Points[i + 1].XValue);
                                pos2.Y = (float)cg.GetPositionFromAxis(s.ChartArea, AxisName.Y, s.Points[i + 1].YValues[0]);
    
                                pos = cg.GetAbsolutePoint(pos);
                                pos2 = cg.GetAbsolutePoint(pos2);
    
                                cg.Graphics.DrawLine(blackPen, pos.X, pos.Y, pos2.X, pos2.Y);
                            }
                        }
                    }
                }
            }
    
            cg.Graphics.SetClip(backup);
        } else if((e.ChartElement is LegendCell)) {
    
            Legend col = e.Chart.Legends[0];
            int count = 0;
            foreach(LegendItem item in col.CustomItems) {
    
                LegendCell cell = item.Cells[1];
                if(e.ChartElement == cell) {
                    if(cell.LegendItem.BorderDashStyle != ChartDashStyle.Solid) {
                        cell.LegendItem.BorderDashStyle = ChartDashStyle.NotSet;
    
                        Color penColor = cell.LegendItem.Color;
                        int borderWidth = cell.LegendItem.BorderWidth;
    
                        Pen blackPen = new Pen(penColor, borderWidth);
    
                        string borderStyleListValue = "";
                        foreach(KeyValuePair<string, string> kvp in borderStyleList) {
                            if(kvp.Key == cell.LegendItem.Name) {
                                borderStyleListValue = kvp.Value;
                                break;
                            }
                        }
    
                        switch(borderStyleListValue) {
                            case "2":
                                blackPen.DashPattern = Const.Border_Dash_Point.Long_Dash;
                                break;
                            case "3":
                                blackPen.DashPattern = Const.Border_Dash_Point.Point_Dash;
                                break;
                            case "4":
                                blackPen.DashPattern = Const.Border_Dash_Point.One_Point_Dash;
                                break;
                            case "5":
                                blackPen.DashPattern = Const.Border_Dash_Point.Two_Point_Dash;
                                break;
                            case "6":
                                blackPen.DashPattern = Const.Border_Dash_Point.Short_Dash;
                                break;
                            case "7":
                                blackPen.DashPattern = Const.Border_Dash_Point.Short_Point_Dash;
                                break;
                            default:
                                break;
                        }
    
                        PointF pos = PointF.Empty;
                        PointF pos2 = PointF.Empty;
    
                        pos.X = e.Position.X;
                        pos.Y = e.Position.Y + e.Position.Height / 2;
                        pos2.X = e.Position.X + e.Position.Width;
                        pos2.Y = e.Position.Y + e.Position.Height / 2;
    
                        pos = cg.GetAbsolutePoint(pos);
                        pos2 = cg.GetAbsolutePoint(pos2);
    
                        cg.Graphics.DrawLine(blackPen, pos.X, pos.Y, pos2.X, pos2.Y);
                    }
                }
                count++;
            }
    
        } else {
            return;
        }
    
    }catch(Exception ex) {
        Console.WriteLine("例外");
    }
    
```
CC BY-SA 4.0
2022-09-20T10:39:45.040
91197
2022-09-20T11:54:39.540
2022-09-20T11:54:39.540
3060
54293
question
c#
chartクラスでグラフを出力する際の描画速度の効率化