How to calculate image size of uploaded image
While in ASP.NET how can I get the attributes of a JPG file so I can determine what the height and width of the picture file is?
Following sample code shows how to get height and width of uploaded image.
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Drawing"%>
<script runat="server">
void SubmitBtn_Click(object sender,EventArgs e){
if (ImageUpload.PostedFile.ContentLength != 0){
try{
Bitmap uploadedimage =
new Bitmap(ImageUpload.PostedFile.InputStream);
MessageLbl.Text =
String.Format("Image Height: {0} and Image Width: {1}",
uploadedimage.Height,uploadedimage.Width);
}catch(Exception ex){
MessageLbl.Text ="Uploaded file is not Image";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Upload image to get its size</p>
<p>
Upload Image
<br />
<asp:FileUpload ID="ImageUpload" runat="server"/>
</p>
<p>
<asp:Button ID="SubmitBtn" runat="server" Text="Submit"
OnClick="SubmitBtn_Click"/>
</p>
</div>
<asp:Label runat="server" ID="MessageLbl" ></asp:Label>
</form>
</body>
</html>
|
 My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.
|