Hej Aske
Man kunne gøre noget i den her stil:
try
{
string savePath = Request.MapPath("Sti/Hvor/Billedet/Skal/Gemmes/" + billedeFileUpload.FileName);
Bitmap img = new Bitmap(billedeFileUpload.FileContent);
Graphics g = Graphics.FromImage(img);
int maxWidth = 200;
int maxHeight = 150;
//Resize if width > maxWidth
if (img.Width > maxWidth)
{
float aspectRatio = (float)img.Width / (float)img.Height;
int newHeight = (int)(maxWidth / aspectRatio);
Bitmap tmpImg = new Bitmap(100, newHeight);
Graphics.FromImage(tmpImg).DrawImage(img, 0, 0, maxWidth, newHeight);
img = tmpImg;
}
//Resize if height > maxHeight
if (img.Height > maxHeight)
{
float aspectRatio = (float)img.Width / (float)img.Height;
int newWidth = (int)(maxHeight * aspectRatio);
Bitmap tmpImg = new Bitmap(newWidth, maxHeight);
Graphics.FromImage(tmpImg).DrawImage(img, 0, 0, newWidth, maxHeight);
img = tmpImg;
}
img.Save(savePath);
}
catch
{ }
Forudsat at du anvender en FileUpload control.
Cool, den virker nogenlunde.. Der er bare en underlig fejl i..
Men har bare skrevet den om så den virker som den skal

hvis nogen var interreseret :
Bitmap img = new Bitmap(FileUpload_billede.FileContent);
float newWidth = 0;
float newHeight = 0;
float ratio = 0;
float maxWidth = 800;
float maxHeight = 500;
if (img.Width > maxWidth || img.Height > maxHeight) //hvis billedet er for stort
{
if (img.Width > img.Height) //hvis billedet er for bredt
{
newWidth = maxWidth;
ratio = img.Width / maxWidth;
newHeight = img.Height / ratio;
if (newHeight > maxHeight) //hvis billeder stadig er for højt
{
newHeight = maxHeight;
ratio = img.Height / maxHeight;
newWidth = img.Width / ratio;
}
}
else if (img.Height > img.Width) //hvis billedet er for højt
{
newHeight = maxHeight;
ratio = img.Height / maxHeight;
newWidth = img.Width / ratio;
if (newWidth > maxWidth) //hvis billedet stadig er for bredt
{
newWidth = maxWidth;
ratio = img.Width / maxWidth;
newHeight = img.Height / ratio;
}
}
else //hivs billedet er lige så højt som bredt
{
newWidth = maxWidth;
newHeight = maxHeight;
}
}
Bitmap tmpImg = new Bitmap(Convert.ToInt16(newWidth), Convert.ToInt16(newHeight));
Graphics.FromImage(tmpImg).DrawImage(img, 0, 0, newWidth, newHeight);
img = tmpImg;
img.Save(Server.MapPath("billeder/" + FileUpload_billede.FileName));
Indlæg senest redigeret d. 22.12.2007 00:52 af Bruger #11231