Prova questo (opportunamente modificalo per le tue esigenze)
codice:
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System"%>
<%@ import Namespace="System.IO"%>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Text.RegularExpressions" %>
<script Language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
int ThumbnailWidth = 64;
int ThumbnailHeight = 64;
string strFileName = "";
string destinazione = "./";
double orgImgWidth = 0.0;
double orgImgHeight = 0.0;
double rThumbnailWidth = 0.0;
double rThumbnailHeight = 0.0;
System.Drawing.Image orgImage;
if (Request.QueryString["w"] != "") ThumbnailWidth = Convert.ToInt32(Request.QueryString["w"]);
if (Request.QueryString["h"] != "") ThumbnailHeight = Convert.ToInt32(Request.QueryString["h"]);
if (Request.QueryString["src"] != "")
{
strFileName = Convert.ToString(Request.QueryString["src"]);
if (Request.QueryString["percorso"] != "") destinazione = Convert.ToString(Server.MapPath(Request.QueryString["percorso"]));
else
destinazione = Convert.ToString(Server.MapPath(destinazione));
if (strFileName != "")
{
try
{
orgImage = System.Drawing.Image.FromFile(Server.MapPath(strFileName));
using(Bitmap tmpImage = new Bitmap(orgImage))
{
rThumbnailWidth = ThumbnailWidth;
rThumbnailHeight = ThumbnailHeight;
orgImgWidth = tmpImage.Width;
orgImgHeight = tmpImage.Height;
// Se l'immagine è più piccola del Thumbnail forziamo le dimensioni di
// quest'ultimo alle dimensioni dell'immagine.
if (orgImgWidth < ThumbnailWidth) ThumbnailWidth = tmpImage.Width;
if (orgImgHeight < ThumbnailHeight) ThumbnailHeight = tmpImage.Height;
// Se le dimensioni dell'immagine e del Thumbnail corrispondono non facciamo nulla.
if (tmpImage.Width != tmpImage.Height)
{
// Altrimenti continuiamo a fare i nostri controlli per calcolarne le
// giuste dimensioni e quindi creare la nostra nuova immagine con
// l'aspectratio corretto.
if (tmpImage.Width > tmpImage.Height)
{
// L > H => tH = (H * tL)/ L
rThumbnailWidth = ThumbnailWidth;
rThumbnailHeight = (double)((tmpImage.Height * ThumbnailWidth) / tmpImage.Width);
} // if (tmpImage.Width > tmpImage.Height)
else
{
if (tmpImage.Width < tmpImage.Height)
{
// L < H => tL = (L * tH)/ H
rThumbnailHeight = ThumbnailWidth;
rThumbnailWidth = (double)((tmpImage.Width * ThumbnailHeight) / tmpImage.Height);
} // if (tmpImage.Width < tmpImage.Height)
} // else
} // if (tmpImage.Width != tmpImage.Height)
} // using(Bitmap tmpImage = new Bitmap(orgImage))
using(Bitmap bitmap = new Bitmap(orgImage, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
{
Response.ContentType = orgImage.RawFormat.ToString();
Response.Clear();
Response.BufferOutput = true;
Response.AddHeader("Content-Disposition", "filename=" + strFileName);
// In questo caso viene creata e salvata la Thumbnail dell'immagine in /thumbs/
if ((orgImage.Width > ThumbnailWidth) || (orgImage.Height > ThumbnailHeight))
{
bitmap.Save(Response.OutputStream, orgImage.RawFormat);
bitmap.Save(destinazione + "/public/" + strFileName, orgImage.RawFormat);
} // if ((orgImage.Width > ThumbnailWidth) || (orgImage.Height > ThumbnailHeight))
else // L'immagine rientra già nella dimensione da noi decisa quindi non serve creargli il Thumbnail.
{
bitmap.Save(Response.OutputStream, orgImage.RawFormat);
} // else
Response.Flush();
} // using(Bitmap bitmap = new Bitmap(orgImage, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
} // try
catch (Exception ex)
{
Response.Write(ex.Message);
} // catch (Exception ex)
finally
{
Response.Write("\nRe-Coded by R.B. Riddick");
}
} // if (strFileName != "")
} // if (Request.QueryString["src"] != "")
}
</script>
codice:
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System"%>
<html>
<body>
</br>Originale (2272px X 1704px): </br>
[img]DSCN0644.jpg[/img]</br>
</br>Ridimensionata (800px X 150px)</br>
[img]img.aspx?src=DSCN0644.jpg&w=800&h=150[/img]
</br>Ridimensionata (400px X 150px)</br>
[img]img.aspx?src=DSCN0644.jpg&w=400&h=150[/img]
</br>Ridimensionata (300px X 150px)</br>
[img]img.aspx?src=DSCN0644.jpg&w=300&h=150[/img]
</br>Ridimensionata (100px X 150px)</br>
[img]img.aspx?src=DSCN0644.jpg&w=100&h=150[/img]
</br>Originale (190px X 224px): </br>
[img]mappa_to_pic_a.gif[/img]</br>
</br>Ridimensionata (185px X 100px)</br>
[img]img.aspx?src=mappa_to_pic_a.gif&w=&h=[/img]</br>
</br>Ridimensionata (100px X 100px)</br>
[img]img.aspx?src=mappa_to_pic_a.gif&w=&h=[/img]</br>
</body>
</html>
http://deepabyss.org/test/test.aspx