Pages

Showing posts with label image size. Show all posts
Showing posts with label image size. Show all posts

Sunday, December 13, 2015

Calculate Height and Width of a image in JQuery.


Use the JQuery inbuilt functions :


$(<image selector>).height() to calculate the height of the image.

$(<image selector>).width() to calculate the width of the image.

Here <image selector> is the selector of image, you can select the image by its tag,id, class etc.


for ex, 

you can select an image by its id (use #)
$('#imgdesert').height()

you can select an image by its class (use dot '.')

$('.myimage').height()





<script type="text/javascript">
    $(window).load(function () {
        $('#imageheightwidth').html(
            "Image height: " + $('#imgdesert').height() + " <br/> Image width: " + $('#imgdesert').width()
            );
    });
</script>
<body>
    <label id="imageheightwidth" class="myimage"></label><br />
    <img id="imgdesert" src="Desert.jpg" />
</body>



Output:


Image height: 700


Image Width : 800


Notes: if you calculate the Image height & width in $(document).ready event , you will get the Image's height and width both Zero. 

Reason is $(document).ready event fires before the image fully loaded and find no image when calculate the dimensions and will show default value i.e 0











Monday, September 2, 2013

Get Image Size in KB/MB

<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
    &nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>



Check Image size, before save to the folder :-

protected void Button1_Click(object sender, EventArgs e)
    {

long fileupload = FileUpload1.PostedFile.ContentLength;
double fileSizeKB = fileupload / 1024.0;                                    // get size in KB
double fileSizeMB = fileupload / (1024.0*1024.0);                   // get size in MB

OR-------------

long fileupload = FileUpload1.FileContent.Length; 
double fileSizeKB = fileupload / 1024.0;
double fileSizeMB = fileupload / (1024.0*1024.0);
}



Check Image size, saved to a location :-

FileInfo fileInfo = new FileInfo(MapPath("c.jpg"));       // 'c.jpg' is the image
double fileSizeKB = fileInfo.Length / 1024.0;