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";
} |
Partager