Reputation: 11
I'm struggling on a massively answered problem but none of the given solutions is working for me. I have a ASP.NET WebForms Application where I want to store pictures and be able to retrieve it, change rotation, remove, etc... The thing is that everything works fine on my devlopment environment (with visual studio or even with IIS 8.5 on my local machine) but when I try to deploy it on my production server (IIS 10.0), I can't even upload a picture, I can only read pictures. If I try to save, rotate or remove an image, I get a GDI+ generic error. It makes me think it is a config or permission issue because the application is the same as on my machine and it works fine.
What I have tried :
The thing that disturbs me is that I have a directory for file upload (not pictures) in the same Application and it works so I guess the permissions are OK, I have the same permissions for both directories.
Here is my code for picture upload :
public static Int64 InsertPhoto(Photo maPhoto)
{
////////////////////////////////////////////////
// ETAPE 1 : Récupération du numéro de photo
////////////////////////////////////////////////
try
{
int? numero = PecV2.DAL.Photo.GetNumeroPhotoMaxByIDIntervention(maPhoto.IDIntervention);
if (numero.HasValue)
{
maPhoto.Numero = numero.Value + 1;
}
else
{
maPhoto.Numero = 0;
}
////////////////////////////////////////////////
// ETAPE 2 : Génération des miniatures
////////////////////////////////////////////////
Intervention monIntervention = Intervention.GetIntervention(maPhoto.IDIntervention);
maPhoto.IDImmeuble = monIntervention.IDImmeuble;
maPhoto.FileName = maPhoto.IDImmeuble.ToString() + "_" + maPhoto.IDIntervention.ToString() + "_" + maPhoto.Numero.ToString() + ".jpg";
maPhoto.EnregistrerMiniature(maPhoto.PhotoFull, global::PecV2.BL.Properties.Settings.Default.LargeurPhotoMedium, global::PecV2.BL.Properties.Settings.Default.RepertoirePhotosMedium, maPhoto.FileName);
maPhoto.EnregistrerMiniature(maPhoto.PhotoFull, global::PecV2.BL.Properties.Settings.Default.LargeurPhotoSmall, global::PecV2.BL.Properties.Settings.Default.RepertoirePhotosSmall, maPhoto.FileName);
// Encoder...
EncoderParameters encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
// Codec...
ImageCodecInfo codecJpeg = null;
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
if (codec.MimeType == "image/jpeg")
codecJpeg = codec;
//GC.Collect();
maPhoto.PhotoFull.Save(Path.Combine(global::PecV2.BL.Properties.Settings.Default.RepertoirePhotosFull, maPhoto.FileName), codecJpeg, encParams);
}
catch (Exception ex)
{
throw ex;
}
////////////////////////////////////////////////
// ETAPE 3 : Enregistrement en base
////////////////////////////////////////////////
return PecV2.DAL.Photo.InsertPhoto(maPhoto.IDImmeuble,
maPhoto.IDIntervention,
maPhoto.Remarques,
maPhoto.Date,
maPhoto.Numero,
maPhoto.FileName);
}
private void EnregistrerMiniature(Bitmap PhotoSource, int DimMax, string RepertoireDestination, string NomFichier)
{
double RatioMedium;
// détermination de l'orientation de la photo originale
if (PhotoSource.Width >= PhotoSource.Height)
{
// photo horizontale
RatioMedium = PhotoSource.Width / DimMax;
//
}
else
{
// photo verticale
RatioMedium = PhotoSource.Height / DimMax;
}
if (RatioMedium < 1)
RatioMedium = 1;
// Generation de la photo medium
Int32 dW;
Int32 dH;
// Calcul de la résolution de la vignette par rapport à la largeur
dW = (Int32)Math.Round(PhotoSource.Width / RatioMedium);
dH = (Int32)Math.Round(PhotoSource.Height / RatioMedium);
Bitmap bVignetteMedium = new Bitmap(dW, dH, PixelFormat.Format32bppRgb);
using (Graphics g = Graphics.FromImage((Image)bVignetteMedium))
{
// Temp pour supprimer bordure (G+H) noire
SolidBrush br = new SolidBrush(Color.White);
g.FillRectangle(br, 0, 0, dW, dH);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
g.DrawImage(PhotoSource, 0, 0, dW, dH);
}
// Encoder...
EncoderParameters encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
// Codec...
ImageCodecInfo codecJpeg = null;
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
if (codec.MimeType == "image/jpeg")
codecJpeg = codec;
//Enregistrement de la vignette
try
{
bVignetteMedium.Save(Path.Combine(RepertoireDestination, NomFichier), codecJpeg, encParams);
bVignetteMedium.Dispose();
}
catch (Exception)
{
throw;
}
}
Here is the full eror stack (btnEnregistrer_Click calls InsertPhoto):
[ExternalException (0x80004005): Une erreur générique s'est produite dans GDI+.]
PecV2.WebApp.Intervention.Onglets.o07_Photos.btnEnregistrer_Click(Object sender, EventArgs e) +509
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11802193
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +150
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1735
------------------------------------------------UPDATE-----------------------------------------------
I have found out where the issue came from, It was a web.config problem, I was trying to open 'photos" directory instead of 'photo' directory... Don't laugh at me please!! I wonder how I have missed that X'D
thank you very much, Nicolas.
Upvotes: 0
Views: 281
Reputation: 11
I have found out where the issue came from, It was a web.config problem, I was trying to open 'photos" directory instead of 'photo' directory... Don't laugh at me please!! I wonder how I have missed that X'D
Upvotes: 1