Bene... ho fatto alcune modifiche nulla di che però ora
sembra funzionare meglio...
Il problema delle GDI si verifica se inserite,contemporaneamente,
più volte lo stesso file quindi per ovviare al problema provvederemo
alla cancellazione del precedente (se verrà permesso !)
Il primo tentativo ha fallito sul mio server.... motivo?
Semplicemente hanno aggiornato il server e la cartella
non ha mantenuto tutti i permessi che gli avevo
precedentemente dato...
Quindi verificate sempre che questi siano stati
correttamente ed effettivamente assegnati.
Codice PHP:
<%@ Page Language="C#" LCID=1040 Debug="true" Trace="false" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<script Language="C#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
titletext2.Text = titletext.Text = ""Upload & Resample On-Fly, delle immagini inviate, su di una cartella del Server" [v.1.0.0]";
Status.Text += "
<center><pre>Coded by <a href=\"http://forum.html.it/forum/member.php?s=&action=getinfo&userid=83362\" title=\"R.B.Riddick\" target=\"blank\">R.B.Riddick ©</a>
on
<a href=\"http://www.html.it/\" title=\"www.html.it\" target=\"blank\">[url]www.html.it[/url]</a></pre></center>";
Status.Text += "
<a href=\"http://validator.w3.org/check?uri=referer\" target=\"blank\"><img src=\"http://www.w3.org/Icons/valid-html401\" alt=\"Valid HTML 4.01 Transitional\" height=\"31\" width=\"88\" border=\"0\"></a></p>";
} // void Page_Load(object sender, System.EventArgs e)
bool IsImage(string fileName)
{
string ext = Path.GetExtension(fileName).ToLower();
bool imageFlag = false;
if (ext != null)
{
switch (ext)
{
case ".emf" : // Enhanced Windows metafile image format
case ".exif" : // Exchangable Image Format
case ".ico" : // Windows icon image format (extension .ico)
case ".wmf" : // Windows metafile image format (extension .wmf)
case ".png" : // Specifies the W3C Portable Network Graphics image format (extension .png)
case ".gif" : // Graphics Interchange Format image format (extension .gif)
case ".bmp" : //Bitmap image format (extension .bmp)
case ".tiff" : // Tag Image File Format (extension .tif)
case ".tif" : // Tag Image File Format (extension .tif)
case ".jpeg" : // Joint Photographic Experts Group image format (extensions .jpg, .jpeg)
case ".jpg" : imageFlag = true; break; // Joint Photographic Experts Group image format (extensions .jpg, .jpeg)
default: imageFlag = false; break; // Not a supported file type.
} // switch (ext)
} // if (ext != null)
else
imageFlag = false;
return imageFlag;
} // bool IsImage(string fileName)
void UploadAndResizeAnImage(HttpPostedFile file, double newWidth, double newHeight, string virPath, bool overwrite)
{
string destPath = ".";
double ThumbnailWidth = 64.0;
double ThumbnailHeight = 64.0;
bool owr = false;
bool exist = false;
if (virPath != "") destPath = virPath;
if (newWidth > 0) ThumbnailWidth = newWidth;
if (newHeight > 0) ThumbnailHeight = newHeight;
if (overwrite)
owr = true;
else
owr = false;
int pathCheck = (destPath.Substring(destPath.LastIndexOf('/') + 1)).Length;
if (pathCheck > 0) destPath += "/";
string fileName = Path.GetFileName(file.FileName);
double orgImgWidth = 0.0;
double orgImgHeight = 0.0;
double rThumbnailWidth = 0.0;
double rThumbnailHeight = 0.0;
ImageCodecInfo[] imgCodec = ImageCodecInfo.GetImageEncoders();
EncoderParameters encPars = new EncoderParameters(2);
EncoderParameter encPar1 = new EncoderParameter( System.Drawing.Imaging.Encoder.Quality, 100L );
EncoderParameter encPar2 = new EncoderParameter( System.Drawing.Imaging.Encoder.Compression, 100L );
encPars.Param[0] = encPar1;
encPars.Param[1] = encPar2;
destPath = Server.MapPath(destPath);
exist = File.Exists(destPath + fileName);
if (!exist && (fileName.Length != 0) || owr)
{
if (!IsImage(fileName))
{
if (fileName.Length != 0)
Status.Text += "<span style=\"color:Red;\">File '" + fileName + "' is not a valid image format. Upload... Aborted.</span>
";
return;
} // if (!IsImage(fileName))
using(System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream))
{
rThumbnailWidth = ThumbnailWidth;
rThumbnailHeight = ThumbnailHeight;
orgImgWidth = image.Width;
orgImgHeight = image.Height;
// Se l'immagine è più piccola del Thumbnail forziamo le dimensioni di
// quest'ultimo alle dimensioni dell'immagine.
if (orgImgWidth < rThumbnailWidth) rThumbnailWidth = image.Width;
if (orgImgHeight < rThumbnailHeight) rThumbnailHeight = image.Height;
// Se le dimensioni dell'immagine e del Thumbnail corrispondono non facciamo nulla.
// quindi se l'immagine è quadrata...
if (image.Width != image.Height)
{
// Se l'immagine non è quadrata
// continuiamo a fare i nostri controlli per calcolarne le
// giuste dimensioni e quindi creare la nostra nuova immagine con
// l'aspectratio corretto.
// Se l'immagine è più larga che alta
if (image.Width > image.Height)
{
// L > H => L = tL
// La larghezza del TN sarà quella da noi stabilita
// quindi dobbiamo solo assegnarla.
rThumbnailWidth = rThumbnailWidth;
// L'altezza del TN invece dovrà essere ricalcolata
// in proporzione alla larghezza dell'immagine originale.
// L > H => tH = (H * tL)/ L
rThumbnailHeight = (double)((image.Height * rThumbnailWidth) / image.Width);
} // if (tmpImage.Width > tmpImage.Height)
else
{
// Altrimenti se l'immagine è più alta
if (image.Width < image.Height)
{
// L < H => H = tH
// L'altezza del TN sarà quella da noi stabilita
// quindi dobbiamo solo assegnarla.
rThumbnailHeight = rThumbnailHeight;
// La larghezza del TN invece dovrà essere ricalcolata
// in proporzione alla altezza dell'immagine originale.
// L < H => tL = (L * tH)/ H
rThumbnailWidth = (double)((image.Width * rThumbnailHeight) / image.Height);
} // if (image.Width < image.Height)
} // else
} // if (image.Width != image.Height)
using(Bitmap bitmap = new Bitmap(image, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
{
string message = "Uploaded";
if (exist && owr)
{
message = "OverWritted";
try
{
File.Delete(destPath + fileName);
}
catch(IOException ioe)
{
message = "<div id=\"error\">Error: " + ioe.GetType().Name + ": The write operation could not be performed because the specified part of the file is locked. Not OverWritted</div>";
}
} // if (exist && owr)
bitmap.Save(destPath + fileName, imgCodec[1], encPars);
Status.Text += "<span style=\"color:Green;\">File '" + fileName + " @ [" + (int)(orgImgWidth) + "px X " + (int)(orgImgHeight) + "px]' " + message + " with this new size [" + (int)(rThumbnailWidth) + "px X " + (int)(rThumbnailHeight) + "px]</span>
";
} // using(Bitmap bitmap = new Bitmap(image, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
} // using(System.Drawing.Image image = System.Drawing.Image.FromFile(file))
} // if (!exist && (fileName.Length != 0) || owr)
else
{
if (exist && (!owr))
Status.Text += "<span style=\"color:Red;\">Error : The file \"<u>[b][i]" + fileName + "[/i][/b]</u>\" already exist... Upload Aborted.</span>
";
}
} // void UploadImage(HttpPostedFile file)
protected void SubmitButton_Click(Object sender, EventArgs e)
{
HttpFileCollection uploadedFiles = Request.Files;
Status.Text = "";
for (int i = 0; i < uploadedFiles.Count; i ++)
{
// Qui sotto definisci le dimensioni che vuoi, la cartella di destinazione
// e se l'applicazione deve sovrascrivere o no i files preesistenti.
//
// UploadAndResizeAnImage(HttpPostedFile file, double newWidth, double newHeight, string virPath, bool overwrite)
//
// Quindi in se scrivo come qui sotto :
UploadAndResizeAnImage(uploadedFiles[i], 1024.0, 768.0, "./public/", true);
// significa che voglio che ogni immagine inviata (uploadedFiles[i])
// venga ridimensionata a 1024x768 (tenendo conto però delle proporzioni originali)
// e che se il file esiste non deve essere sovrascritto (overwrite = false).
// Chiaro? Più di così ;)
}
Status.Text += "
<center><pre>Coded by <a href=\"http://forum.html.it/forum/member.php?s=&action=getinfo&userid=83362\" title=\"R.B.Riddick\" target=\"blank\">R.B.Riddick ©</a>
on
<a href=\"http://www.html.it/\" title=\"www.html.it\" target=\"blank\">[url]www.html.it[/url]</a></pre></center>";
Status.Text += "
<a href=\"http://validator.w3.org/check?uri=referer\" target=\"blank\"><img src=\"http://www.w3.org/Icons/valid-html401\" alt=\"Valid HTML 4.01 Transitional\" height=\"31\" width=\"88\" border=\"0\"></a></p>";
} // protected void SubmitButton_Click(Object sender, EventArgs e)
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<title><asp:Literal id="titletext" runat="server"/></title>
<style type="text/css">
body
{
background:#2288DD;
}
a:link
{
color: Blue;
text-decoration: none;
}
a:active
{
color: Blue;
text-decoration: none;
}
a:visited
{
color: Blue;
text-decoration: none;
}
a:hover
{
color: Blue;
font-weight: bold;
text-decoration: none;
}
div#error
{
color: Red;
font-weight: bold;
text-decoration: underline;
white-space: normal;
}
</style>
</head>
<body>
<div align="center">
<h2><asp:Literal id="titletext2" runat="server"/></h2>
<form name="inviafile" id="inviafile" enctype="multipart/form-data" runat="server">
Select File1:
<input id="File1" type="file" runat="Server"/>
Select File2:
<input id="File2" type="file" runat="Server"/>
Select File3:
<input id="File3" type="file" runat="Server"/>
Select File4:
<input id="File4" type="file" runat="Server"/>
Select File5:
<input id="File5" type="file" runat="Server"/>
Select File6:
<input id="File6" type="file" runat="Server"/>
Select File7:
<input id="File7" type="file" runat="Server"/>
Select File8:
<input id="File8" type="file" runat="Server"/>
Select File9:
<input id="File9" type="file" runat="Server"/>
Select File10:
<input id="File10" type="file" runat="Server"/>
Select File11:
<input id="File11" type="file" runat="Server"/>
Select File12:
<input id="File12" type="file" runat="Server"/>
Select File13:
<input id="File13" type="file" runat="Server"/>
Select File14:
<input id="File14" type="file" runat="Server"/>
Select File15:
<input id="File15" type="file" runat="Server"/>
<div align="center"><input id="Submit1" type="submit" value="Upload Files" runat="Server" onserverclick="SubmitButton_Click"/></div>
<asp:Label id="Status" runat="server"></asp:Label>
</form>
</div>
</body>
</html>
Buon divertimento e se ci sono problemi sono qui...