<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fileImage" runat="server" />
<asp:CustomValidator ID="valFile" runat="server"
ErrorMessage="No file uploaded"
onservervalidate="valFile_ServerValidate" Display="Dynamic" />
<asp:CustomValidator ID="valFileType" runat="server"
ErrorMessage="This isn't not an image"
onservervalidate="valFileType_ServerValidate" Display="Dynamic" />
<asp:CustomValidator ID="valFileSize" runat="server"
ErrorMessage="The image exceeds 10 MB"
onservervalidate="valFileSize_ServerValidate" Display="Dynamic" /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Upload"
onclick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void valFile_ServerValidate(object source, ServerValidateEventArgs args)
{
if (!fileImage.HasFile)
{
args.IsValid = false;
}
}
protected void valFileType_ServerValidate(object source, ServerValidateEventArgs args)
{
if (fileImage.HasFile)
{
string[] acceptedTypes = new string[]
{
"image/bmp",
"image/jpeg",
"image/tiff",
"image/gif",
"image/png"
};
if (!acceptedTypes.Contains(fileImage.PostedFile.ContentType))
{
args.IsValid = false;
}
}
}
protected void valFileSize_ServerValidate(object source, ServerValidateEventArgs args)
{
if (IsValid)
{
int maxSize = 5 * 1024 * 1024;
if (fileImage.PostedFile.ContentLength > maxSize)
{
args.IsValid = false;
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (IsValid)
{
Bitmap image = ResizeImage(fileImage.PostedFile.InputStream, 200, 400);
image.Save(Server.MapPath("~/Photos/") + Guid.NewGuid().ToString() + ".jpg", ImageFormat.Jpeg);
image.Dispose();
}
}
private Bitmap ResizeImage(Stream streamImage, int maxWidth, int maxHeight)
{
Bitmap originalImage = new Bitmap(streamImage);
int newWidth = originalImage.Width;
int newHeight = originalImage.Height;
double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
if (aspectRatio <= 1 && originalImage.Width > maxWidth)
{
newWidth = maxWidth;
newHeight = (int)Math.Round(newWidth / aspectRatio);
}
else if (aspectRatio > 1 && originalImage.Height > maxHeight)
{
newHeight = maxHeight;
newWidth = (int)Math.Round(newHeight * aspectRatio);
}
Bitmap newImage = new Bitmap(originalImage, newWidth, newHeight);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
originalImage.Dispose();
return newImage;
}
}
No comments:
Post a Comment