IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Windows Discussion :

Création d'une image temporaire puis suppression


Sujet :

Développement Windows

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 17
    Points : 15
    Points
    15
    Par défaut Création d'une image temporaire puis suppression
    Bonjour,
    J'essaie d'ajouter une image à un fichier PDF (en utilisant le Package NuGet PDFSharp), cette image est simplement l'image du Chart de ma WindowsForm : chart1.SaveImage(path + "\\chart.png", System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);. la variable path est le paramètre de ma méthode CreatePDF(string path) et elle représente le chemin vers le dossier où l'utilisateur souhaite enregistrer le PDF (ici un dossier créer spécialement dans le dossier de ma solution).

    Puis je l'ajoute au PDF avec: gfx.DrawImage(PdfSharp.Drawing.XImage.FromFile(folderPath + "\\chart.png"), 14, 242, page.Width - 26, 500); jusqu'ici aucune erreur.

    Mais au moment où je souhaite supprimer cette image: File.Delete(folderPath + "\\chart.png"); une erreur se lève Nom : Erreur_Suppr_Image.png
Affichages : 347
Taille : 494,6 Ko

    Je pense comprendre l'erreur car vhost.exe tourne toujours et je ne peux donc pas supprimer de fichier tant qu'il tourne.
    Avez vous une idée pour contourner ce problème je pensais créer un fichier temporaire de l'image ce qui devrait me permettre de pouvoir le supprimer même si vhost.exe tourne toujours. Est-ce la bonne méthode? si oui savez vous comment je doit m'y prendre?

    Merci d'avance

  2. #2
    Expert éminent sénior

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 757
    Points : 10 697
    Points
    10 697
    Billets dans le blog
    21
    Par défaut
    Bonjour,

    XImage implémente l'interface IDisposable pour libérer correctement les ressources. D'après l'extrait de code que j'ai pu voir, tu initialises une image, tu l'utilises, mais tu n'appelles pas la méthode Dispose() après en avoir fini avec elle.

    A voir si la libération de la ressource avant la suppression du fichier temporaire permettra de résoudre ton problème.
    François DORIN
    Consultant informatique : conception, modélisation, développement (C#/.Net et SQL Server)
    Site internet | Profils Viadéo & LinkedIn
    ---------
    Page de cours : fdorin.developpez.com
    ---------
    N'oubliez pas de consulter la FAQ C# ainsi que les cours et tutoriels

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 17
    Points : 15
    Points
    15
    Par défaut
    Citation Envoyé par François DORIN Voir le message
    XImage implémente l'interface IDisposable pour libérer correctement les ressources.
    D'accord je vais regarder dans cette direction puis je ferais un retour sur le résultat. Merci beaucoup.

  4. #4
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 17
    Points : 15
    Points
    15
    Par défaut
    Alors j'ai tester ce que tu m'as conseiller en faisant ceci: PdfSharp.Drawing.XImage.FromFile(folderPath + "\\chart.png").Dispose();
    Je ne sais pas si tu avais ça en tête mais en tous cas l'erreur est toujours là au moment de la suppression du fichier.
    J'ai un peu regarder sur d'autre forum et pas mal de monde utilise FileStream est-ce que ça pourrait être une solution viable pour mon problème?

  5. #5
    Expert éminent sénior

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 757
    Points : 10 697
    Points
    10 697
    Billets dans le blog
    21
    Par défaut
    Alors, dans mon idée, c'était plus comme ça
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    PdfSharp.Drawing.XImage image = PdfSharp.Drawing.XImage.FromFile(folderPath + "\\chart.png")
    gfx.DrawImage(image, 14, 242, page.Width - 26, 500);
    image.Dispose(); // Potentiellement, mettre cette ligne après avoir sauvegarder le document pdf.
    J'ai un peu de mal à voir comment tu as pu faire ton appel à la méthode Dispose(), car si tu fais PdfSharp.Drawing.XImage.FromFile(folderPath + "\\chart.png").Dispose(); alors tu créés un objet XImage que tu libères directement sans pouvoir l'utiliser !

    Il serait peut être bon de nous donner la portion de code que tu utilises pour instancier et utiliser ton image.

    Peut être que le FileStream pourrait fonctionner ici, mais encore faut-il vérifier que les accès aux ressources sont corrects avant d'essayer une solution alternative.
    François DORIN
    Consultant informatique : conception, modélisation, développement (C#/.Net et SQL Server)
    Site internet | Profils Viadéo & LinkedIn
    ---------
    Page de cours : fdorin.developpez.com
    ---------
    N'oubliez pas de consulter la FAQ C# ainsi que les cours et tutoriels

  6. #6
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 17
    Points : 15
    Points
    15
    Par défaut
    Citation Envoyé par François DORIN Voir le message
    Il serait peut être bon de nous donner la portion de code que tu utilises pour instancier et utiliser ton image.
    Je vais vous donner les parties du code concernant mon image, tous se passe dans ma méthode CreatePDF(string path).

    Tous d'abbord j'enregistre mon chart au format .png dans le dossier souhaiter par l'utilisateur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    chart1.SaveImage(path + "\\chart.png", System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);
    (je créer mon fichier pdf et son contenu sans l'enregistrer pour le moment).
    Ensuite j'ajoute ce .png dans mon fichier PDF à l'emplacement voulus
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    gfx.DrawImage(PdfSharp.Drawing.XImage.FromFile(path + "\\chart.png"), 14, 242, page.Width - 26, 500);
    (J'enregistre ici mon fichier PDF dans le dossier spécifier avec un simple monPDF.Save(path+\\+filename)
    Et enfin j'essaie de supprimer le PNG du dossier:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    File.Delete(path + "\\chart.png");
    j'espère que vous visualisez mieux la structure du code qui touche à l'image.

  7. #7
    Expert éminent sénior

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 757
    Points : 10 697
    Points
    10 697
    Billets dans le blog
    21
    Par défaut
    Peux-on avoir tout le code de la méthode CreatePDF ? Cela sera beaucoup plus simple que d'en avoir que quelques lignes...
    Merci
    François DORIN
    Consultant informatique : conception, modélisation, développement (C#/.Net et SQL Server)
    Site internet | Profils Viadéo & LinkedIn
    ---------
    Page de cours : fdorin.developpez.com
    ---------
    N'oubliez pas de consulter la FAQ C# ainsi que les cours et tutoriels

  8. #8
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 17
    Points : 15
    Points
    15
    Par défaut
    Citation Envoyé par François DORIN Voir le message
    Peux-on avoir tout le code de la méthode CreatePDF ? Cela sera beaucoup plus simple que d'en avoir que quelques lignes...
    Merci
    Oui pas de soucis le voici:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
            //###############################################################################
            #region PDF
            public void CreatePDF(string path)
            {
                string filename;
     
     
                if (ConfigOptions.ConfigFile.nameFile == 0)
                {
                    filename = infoCptTab[5].Replace('.', ',') + ".pdf";
                }
                else if (ConfigOptions.ConfigFile.nameFile == 1)
                {
                    filename = infoCptTab[5].Replace('.', ',') + "_" + infoCptTab[4].Replace("+/-", "+-") + ".pdf";
                }
                else if (ConfigOptions.ConfigFile.nameFile == 2)
                {
                    filename = infoCptTab[5].Replace('.', ',') + "_" + DateTime.Now.Day.ToString("00") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Year.ToString("0000") + ".pdf";
                }
                else if (ConfigOptions.ConfigFile.nameFile == 3)
                {
                    filename = infoCptTab[4].Replace("+/-", "+-") + "_" + DateTime.Now.Day.ToString("00") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Year.ToString("0000") + ".pdf";
                }
                else if (ConfigOptions.ConfigFile.nameFile == 4)
                {
                    filename = infoCptTab[4].Replace("+/-", "+-")+".pdf";
                }
                else if (ConfigOptions.ConfigFile.nameFile == 5)
                {
                    filename = DateTime.Now.Day.ToString("00") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Year.ToString("0000") + ".pdf";
                }
                else if (ConfigOptions.ConfigFile.nameFile == 6)
                {
                    filename = infoCptTab[5].Replace('.', ',') + "_" + infoCptTab[4].Replace("+/-", "+-") + "_" + DateTime.Now.Day.ToString("00") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Year.ToString("0000") + ".pdf";
                }
                else
                {
                    filename = infoCptTab[5].Replace('.', ',') + "_" + infoCptTab[4].Replace("+/-", "+-") + "_" + DateTime.Now.Day.ToString("00") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Year.ToString("0000") + ".pdf";
                }
     
                chart1.SaveImage(path + "\\chart.png", System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);
     
                PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();
                PdfSharp.Pdf.PdfPage page = document.AddPage();
                //page.Width = PdfSharp.Drawing.XUnit.FromMillimeter(297);
                //page.Height = PdfSharp.Drawing.XUnit.FromMillimeter(420);
                PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
                PdfSharp.Drawing.XFont font = new PdfSharp.Drawing.XFont("Verdana", 20, PdfSharp.Drawing.XFontStyle.Bold);
                PdfSharp.Drawing.XPen pen = new PdfSharp.Drawing.XPen(PdfSharp.Drawing.XColors.Black, 1);
                gfx.DrawString("CERTIFICAT D'ETALONNAGE", font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(10, 15, page.Width - 10, page.Height - 15), PdfSharp.Drawing.XStringFormat.TopCenter);
                gfx.DrawLines(pen, 10, 10, 10, page.Height - 10);
                gfx.DrawLines(pen, 10, page.Height - 10, page.Width - 10, page.Height - 10);
                gfx.DrawLine(pen, page.Width - 10, page.Height - 10, page.Width - 10, 10);
                gfx.DrawLine(pen, page.Width - 10, 10, 10, 10);
                //Console.Write("page width: " + page.Width);
                //Console.WriteLine("Page height: " + page.Height);
                //Console.WriteLine("Page size: " + page.Size);
                //** Contour premier tableau
                gfx.DrawLine(pen, 15, 40, page.Width - 15, 40);
                gfx.DrawLine(pen, page.Width - 15, 40, page.Width - 15, 110);
                gfx.DrawLine(pen, page.Width - 15, 110, 15, 110);
                gfx.DrawLine(pen, 15, 110, 15, 40);
     
                //** Ligne intérieur premier tableau
                for (int i = 40; i <= 110; i += 10)
                {
                    gfx.DrawLine(pen, 15, i, page.Width - 15, i);
                }
                gfx.DrawLine(pen, page.Width / 2, 40, page.Width / 2, 110);
     
                //** ligne horizontal deuxième tableau
                for (int i = 130; i <= 190; i += 10)
                {
                    gfx.DrawLine(pen, 15, i, page.Width - 15, i);
                }
                //** ligne verticales deuxième tableau
                gfx.DrawLine(pen, 15, 130, 15, 190);
                gfx.DrawLine(pen, (page.Width - 15) / 3, 130, (page.Width - 15) / 3, 190);
                gfx.DrawLine(pen, (page.Width - 15) - ((page.Width - 15) / 3), 130, (page.Width - 15) - ((page.Width - 15) / 3), 190);
                gfx.DrawLine(pen, page.Width - 15, 130, page.Width - 15, 190);
     
     
                int numberColumn = tabPositionRef.Length + 1;
     
                //** ajout du troisème tableau
                //ajout de la première colonne gauche
                gfx.DrawLine(pen, 15, 200, 15, 240);
                gfx.DrawLine(pen, 97, 200, 97, 240);
                //** nombre de colone restante = numberColumn 
                for (int i = 1; i <= numberColumn - 1; i++)
                {
                    int t = (97 + (i * 22));
                    if (t < page.Width - 15)
                    {
                        gfx.DrawLine(pen, t, 200, t, 240);
                    }
                    else
                    {
                        t = (int)page.Width - 15;
                        gfx.DrawLine(pen, t, 200, t, 240);
                    }
     
                }
                //nombre de ligne =4
                for (int a = 200; a <= 240; a += 10)
                {
                    gfx.DrawLine(pen, 15, a, page.Width - 36, a);
     
                }
     
                //** Remplissage des tableau
                //** 2 Boucle
                //** la première pour le balayage des colonne
                //** la deuxième pour le balayage des lignes
                int nbColumns = 0;
                int nbLines = 0;
                //pdfSharp.Drawing.XRect(double x, double y, width, height)
                double w = 0;
                double h = 0;
                double x = 0;
                double y = 0;
                StringBuilder IdnBuffer = new StringBuilder(256);
                Methods.GCS2.qIDN(ID, IdnBuffer, 255);
                string[] textLeft = new string[] { "Date: ", "Moyen de controle: ", "Capteur etalon: ", "Certificat: ", "Norme appliquée: ", "Température: ", "Opérateur: ", "Constructeur: ", "Produit: ", "S/N: ", "Course: ", "Resolution: ", "Mise en service: ", "Prochaine vérification: " };
                string[] textRight = new string[] { DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Year.ToString(), "Metro MultiCAL", Convert.ToString(IdnBuffer).Substring(0, 40), "", "", SaveInformation.temperature, SaveInformation.operateur, infoCptTab[1], infoCptTab[2], "", infoCptTab[4], infoCptTab[5], SaveInformation.date, "" };
                font = new PdfSharp.Drawing.XFont("Verdana", 8, PdfSharp.Drawing.XFontStyle.Regular);
     
                //Ajout des valeurs dans le premier tableau
                nbColumns = 2;
                nbLines = 7;
                w = (page.Width - 15) - 17;
                h = 0;
                x = 15;
                {
                    x = 17;
                    y = 48;
                    double xY = 0;
                    for (int c = 1; c <= nbColumns; c++)
                    {
                        if (c == 2)
                        {
                            xY = 48;
                        }
     
                        for (int l = 1; l <= nbLines; l++)
                        {
     
                            if (c == 2)
                            {
                                x = (page.Width / 2) + 2;
                                gfx.DrawString(textLeft[l + 6], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, xY, w, h));
                                gfx.DrawString(textRight[l + 6], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x + 200, xY, w, h));
                                xY = xY + 10;
                            }
                            else
                            {
                                gfx.DrawString(textLeft[l - 1], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y, w, h));
                                gfx.DrawString(textRight[l - 1], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x + 110, y, w, h));
                                y = y + 10;
                            }
                        }
     
                    }
     
                    //Ajout du deuxième tableau
                    font = new PdfSharp.Drawing.XFont("Verdana", 8, PdfSharp.Drawing.XFontStyle.Regular);
                    textLeft = new string[] { "Erreur: ", "Maximum: ", "Justesse totale: ", "Justesse locale: ", "Hystérésis", "Fidelité: ", "Mesuré: ", tabError.Max().ToString(), justesse.ToString(), "", "", "", "Limite: ", "", "", "", "", "" };
                    nbLines = 6;
                    nbColumns = 3;
                    for (int c = 1; c <= nbColumns; c++)
                    {
                        y = 138;
     
                        for (int l = 1; l <= nbLines; l++)
                        {
                            if (c == 2)
                            {
                                x = ((page.Width - 15) / 3) + 2;
                                gfx.DrawString(textLeft[l + 5], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y, w, h));
                                y = y + 10;
                            }
                            if (c == 1)
                            {
                                x = 17;
                                gfx.DrawString(textLeft[l - 1], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y, w, h));
                                y = y + 10;
                            }
                            if (c == 3)
                            {
                                x = (page.Width - 15) - ((page.Width - 15) / 3) + 2;
                                gfx.DrawString(textLeft[l + 11], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y, w, h));
                                y = y + 10;
                            }
                        }
                    }
                    //Ajout troisième tableau
                    font = new PdfSharp.Drawing.XFont("Verdana", 8, PdfSharp.Drawing.XFontStyle.Regular);
                    textLeft = new string[] { "N° echantillon: ", "Position test: ", "Position ref: ", "Erreur: " };
                    nbLines = 4;
                    nbColumns = numberColumn;
                    y = 208;
                    x = 242;
     
                    for (int c = 1; c <= nbColumns - 1; c++)
                    {
                        for (int l = 1; l <= nbLines; l++)
                        {
                            if (c == 1)
                            {
                                x = 17;
                                gfx.DrawString(textLeft[l - 1], font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y, w, h));
                                y = y + 10;
                            }
                        }
                    }
     
                    //Ajout des lignes
                    font = new PdfSharp.Drawing.XFont("Verdana", 6, PdfSharp.Drawing.XFontStyle.Regular);
                    y = 208;
                    for (int i = 0; i <= nbColumns - 2; i++)
                    {
                        x = (98 + (i * 22));
     
                        gfx.DrawString(Convert.ToString(i), font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y, w, h));
                        gfx.DrawString(Convert.ToString(tabPosition[i]), font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y + 10, w, h));
                        gfx.DrawString(Convert.ToString(tabPositionRef[i]), font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y + 20, w, h));
                        gfx.DrawString(Convert.ToString(tabError[i]), font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(x, y + 30, w, h));
                    }
                }
     
                gfx.DrawImage(PdfSharp.Drawing.XImage.FromFile(path + "\\chart.png"), 14, 242, page.Width - 26, 500);
                //File.SetAttributes(folderPath + "\\chart.png", FileAttributes.Temporary);
                //** Ajout pied de page
                gfx.DrawLine(pen, 15, page.Height - 100, page.Width - 15, page.Height - 100);
                font = new PdfSharp.Drawing.XFont("Verdana", 13, PdfSharp.Drawing.XFontStyle.Bold);
                gfx.DrawString("Obesrvation:", font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(15, page.Height - 80, page.Width / 2, page.Height), PdfSharp.Drawing.XStringFormat.TopLeft);
                gfx.DrawString("Signature:___________________", font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(page.Width / 2, page.Height - 80, page.Width - 15, page.Height), PdfSharp.Drawing.XStringFormat.TopLeft);
                font = new PdfSharp.Drawing.XFont("Verdana", 7, PdfSharp.Drawing.XFontStyle.Bold);
                gfx.DrawString(ConfigOptions.ConfigFile.footer, font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(15, page.Height - 25, page.Width - 15, page.Height), PdfSharp.Drawing.XStringFormat.TopCenter);
                //string filename = "Certificat_Etalonnage_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Hour.ToString() + "h_" + DateTime.Now.Minute.ToString() + "min_" + infoCptTab[2] + ".pdf";
                document.Save(path + "\\" + filename);
                //PdfSharp.Drawing.XImage.FromFile(folderPath + "\\chart.png").Dispose();
                System.Drawing.Image.FromFile(path + "\\chart.png").Dispose();
     
                File.Delete(path + "\\chart.png");
                MessageBox.Show("PDF  enregistré au chemin: " + path + "\\" + filename);
            }
            #endregion
    Pour information ConfigFile est une structure et à l'intérieur de celle ci j'ai des méthode me permettant de lire et ecrire dans un fichier XML les options qu'auras choisis l'utilisateur (sauvegarde automatique, impression automatique, chemin de sauvegarde,...)

  9. #9
    Expert éminent sénior

    Avatar de François DORIN
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juillet 2016
    Messages
    2 757
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2016
    Messages : 2 757
    Points : 10 697
    Points
    10 697
    Billets dans le blog
    21
    Par défaut
    C'est normal que cela ne fonctionne pas
    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
                gfx.DrawImage(PdfSharp.Drawing.XImage.FromFile(path + "\\chart.png"), 14, 242, page.Width - 26, 500);
                //File.SetAttributes(folderPath + "\\chart.png", FileAttributes.Temporary);
                //** Ajout pied de page
                gfx.DrawLine(pen, 15, page.Height - 100, page.Width - 15, page.Height - 100);
                font = new PdfSharp.Drawing.XFont("Verdana", 13, PdfSharp.Drawing.XFontStyle.Bold);
                gfx.DrawString("Obesrvation:", font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(15, page.Height - 80, page.Width / 2, page.Height), PdfSharp.Drawing.XStringFormat.TopLeft);
                gfx.DrawString("Signature:___________________", font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(page.Width / 2, page.Height - 80, page.Width - 15, page.Height), PdfSharp.Drawing.XStringFormat.TopLeft);
                font = new PdfSharp.Drawing.XFont("Verdana", 7, PdfSharp.Drawing.XFontStyle.Bold);
                gfx.DrawString(ConfigOptions.ConfigFile.footer, font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(15, page.Height - 25, page.Width - 15, page.Height), PdfSharp.Drawing.XStringFormat.TopCenter);
                //string filename = "Certificat_Etalonnage_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Hour.ToString() + "h_" + DateTime.Now.Minute.ToString() + "min_" + infoCptTab[2] + ".pdf";
                document.Save(path + "\\" + filename);
                //PdfSharp.Drawing.XImage.FromFile(folderPath + "\\chart.png").Dispose();
                System.Drawing.Image.FromFile(path + "\\chart.png").Dispose();
    Dans ce code, tu créés 2 instances de XImage, mais tu ne libères que la seconde.

    Voici comment il faut écrire les choses :
    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
                PdfSharp.Drawing.XImage. image = PdfSharp.Drawing.XImage.FromFile(path + "\\chart.png");
                gfx.DrawImage(image, 14, 242, page.Width - 26, 500);
                //File.SetAttributes(folderPath + "\\chart.png", FileAttributes.Temporary);
                //** Ajout pied de page
                gfx.DrawLine(pen, 15, page.Height - 100, page.Width - 15, page.Height - 100);
                font = new PdfSharp.Drawing.XFont("Verdana", 13, PdfSharp.Drawing.XFontStyle.Bold);
                gfx.DrawString("Obesrvation:", font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(15, page.Height - 80, page.Width / 2, page.Height), PdfSharp.Drawing.XStringFormat.TopLeft);
                gfx.DrawString("Signature:___________________", font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(page.Width / 2, page.Height - 80, page.Width - 15, page.Height), PdfSharp.Drawing.XStringFormat.TopLeft);
                font = new PdfSharp.Drawing.XFont("Verdana", 7, PdfSharp.Drawing.XFontStyle.Bold);
                gfx.DrawString(ConfigOptions.ConfigFile.footer, font, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(15, page.Height - 25, page.Width - 15, page.Height), PdfSharp.Drawing.XStringFormat.TopCenter);
                //string filename = "Certificat_Etalonnage_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Hour.ToString() + "h_" + DateTime.Now.Minute.ToString() + "min_" + infoCptTab[2] + ".pdf";
                document.Save(path + "\\" + filename);
                image.Dispose();
    
                File.Delete(path + "\\chart.png");
                MessageBox.Show("PDF  enregistré au chemin: " + path + "\\" + filename);
    mes modifications sont en gras.

    Si cela ne fonctionne pas, alors il faudra effectivement essayer de trouver une solution alternative, car cela signifiera que les ressources ne sont pas correctement libérées (mais là, ce sera un problème lié à l'implémentation de PdfSharp et non plus de ton code)
    François DORIN
    Consultant informatique : conception, modélisation, développement (C#/.Net et SQL Server)
    Site internet | Profils Viadéo & LinkedIn
    ---------
    Page de cours : fdorin.developpez.com
    ---------
    N'oubliez pas de consulter la FAQ C# ainsi que les cours et tutoriels

  10. #10
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 17
    Points : 15
    Points
    15
    Par défaut
    Citation Envoyé par François DORIN Voir le message
    C'est normal que cela ne fonctionne pas [...]
    mes modifications sont en gras.
    Si cela ne fonctionne pas, alors il faudra effectivement essayer de trouver une solution alternative, car cela signifiera que les ressources ne sont pas correctement libérées (mais là, ce sera un problème lié à l'implémentation de PdfSharp et non plus de ton code)
    Un grand merci ça fonctionne parfaitement, merci beaucoup c'était le dernier bug que j'ai détecter pour avoir une version stable pour ma soutenance. Vraiment merci beaucoup.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Création d'une image à partir de son code binaire
    Par thefaycal dans le forum ASP
    Réponses: 11
    Dernier message: 18/05/2006, 17h39
  2. [ImageMagick] Création d'une image miniature
    Par popeye82 dans le forum Bibliothèques et frameworks
    Réponses: 5
    Dernier message: 01/02/2006, 20h10
  3. Réponses: 1
    Dernier message: 03/01/2006, 17h46
  4. [ImageMagick] Header envoyé lors de la création d'une image
    Par KLiFF dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 25/10/2005, 16h35
  5. [Débutante] Création d'une image avec un composant
    Par gwendo dans le forum AWT/Swing
    Réponses: 9
    Dernier message: 09/07/2004, 09h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo