Bonjour,


j'ai utilisé un code trouvé sur internet qui me permet de réduire la taille d'une photo jpg. Cependant lors du redimensionnement ma photo est complètement transformés. elle n'a plus rien a voir avec la photo d'origine.

ya t-il un autre moyen de redimensionné une photo.

voici le code que j'utilise :

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
 // ********************* upload de la photo du porduit ********************
    private string UploadPhoto(string RefProd, int id_FamilleProd, string CoupCoeur)
    {
        string photo = this.FileUploadProd.FileName;
        string photoARedim = "";
        string chemin = "";
        if (photo != "") {
            photo = RefProd;
            // enregistrement de la photo dans le repertoire appproprié
            switch (id_FamilleProd)
            {
                    // accessoires
                case 1 :
                    chemin = "../images/photos/accessoires/";
                    this.FileUploadProd.SaveAs(Server.MapPath(chemin + photo + ".jpg"));
                    break;
 
                    // prêt à porter
                case 2 :
                    chemin = "../images/photos/pretsaporter/";
                    this.FileUploadProd.SaveAs(Server.MapPath(chemin + photo + ".jpg"));
                    break;
 
                    // bijoux
                case 3 :
                    chemin = "../images/photos/bijoux/";
                    this.FileUploadProd.SaveAs(Server.MapPath(chemin + photo + ".jpg"));
                    break;
            }
 
        try{
            // redimensionnement de la photo
            photoARedim = chemin + photo;
 
            int imageWidth1 = 0;
            int imageHeight1 = 0;
 
            imageWidth1 = 500;
            imageHeight1 = 600;
 
            System.Drawing.Image fullSizeImg =  System.Drawing.Image.FromFile(Server.MapPath(photoARedim));
            int hautInit = fullSizeImg.Height;
            int LargInit = fullSizeImg.Width;
 
            System.Drawing.Image.GetThumbnailImageAbort dCallBacl =
                new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallBack);
 
            // si l'image initiale est plus grandes que les tailles voulues
            if (LargInit > imageWidth1 && hautInit > imageHeight1)
            {
                System.Drawing.Image thumbnailImg1 = fullSizeImg.GetThumbnailImage(imageWidth1, imageHeight1, dCallBacl, IntPtr.Zero);
 
                // met m'image au format jpg
                thumbnailImg1.Save(Server.MapPath(photoARedim) + ".jpg", ImageFormat.Jpeg);
                thumbnailImg1.Dispose();
                fullSizeImg.Dispose();
            }
            // si la largeur initiale uniquement est supérieure à celle souhaitée
            else if (LargInit > imageWidth1 && hautInit <= imageHeight1)
            {
                System.Drawing.Image thumbnailImg1 = fullSizeImg.GetThumbnailImage(imageWidth1, hautInit, dCallBacl, IntPtr.Zero);
 
                // met m'image au format jpg
                thumbnailImg1.Save(Server.MapPath(photoARedim) + ".jpg", ImageFormat.Jpeg);
                thumbnailImg1.Dispose();
                fullSizeImg.Dispose();
            }
            // si la hauteur uniquement est supérieur à celle souhaitée
            else if (LargInit <= imageWidth1 && hautInit > imageHeight1)
            {
                System.Drawing.Image thumbnailImg1 = fullSizeImg.GetThumbnailImage(LargInit, imageHeight1, dCallBacl, IntPtr.Zero);
 
                // met m'image au format jpg
                thumbnailImg1.Save(Server.MapPath(photoARedim) + ".jpg", ImageFormat.Jpeg);
                thumbnailImg1.Dispose();
                fullSizeImg.Dispose();
            }
            // si l'image initiale est plus petite ou égale aux tailles souhaitée
            else if (LargInit <= imageWidth1 && hautInit <= imageHeight1)
            {
                System.Drawing.Image thumbnailImg1 = fullSizeImg.GetThumbnailImage(LargInit, hautInit, dCallBacl, IntPtr.Zero);
 
                // met m'image au format jpg
                thumbnailImg1.Save(Server.MapPath(photoARedim) + ".jpg", ImageFormat.Jpeg);
                thumbnailImg1.Dispose();
                fullSizeImg.Dispose();
            }
 
            // on efface ensuite la photo d'origine
            if (File.Exists(Server.MapPath(photoARedim)))
                File.Delete(Server.MapPath(photoARedim));
        }
        catch (Exception ex) { throw new Exception (ex.Message); }
        }
 
        return photoARedim.Substring(photoARedim.IndexOf('/')+1) + ".jpg";
    }
en sachant qu'au départ les photos font 2Mo soit 2600x3500 de dimensions et que moi je les réduis en 500x600, car le but c'est qu'elles fasse a peu près 30 ko ou moins

Je pense que le problème vient quand j'enregistra ma nouvelle photo sur le serveur.

soit dans cette partie :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 // met m'image au format jpg
                thumbnailImg1.Save(Server.MapPath(photoARedim) + ".jpg", ImageFormat.Jpeg);
si quelqu'un veut bien m'aider se serait sympa