Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Respected Professionals

Hope you all are fine. I want to implement a javascript module where i can check dimensions of an image file,
The image file is selected from client side by FileUpload Control of asp.net.


The source code is

.aspx portion

<asp:FileUpload ID="FileUpload1" runat="server" ClientIDMode ="Static" />


.aspx.cs portion
C#
if (!IsPostBack)
        {
            FileUpload1.Attributes.Add("onchange", "CheckFileDimension();");

        }


Javascript portion is
JavaScript
<script type="text/javascript">
        function CheckFileDimension() {
            var Uploader = document.getElementById("FileUpload1");
            var uploadedImage=Uploader.value;
            alert(uploadedImage); // This only shows the image file name not the //full path, I want to get the full path of the image and get the  dimension of //the image
        
         
            
        }
    </script>


If it is possible in javascript then guide me in this regard. if not how to check in C# at server side, with out effecting the input stream before saving that image. if the image is of the exact dimension then i will save the file to the server otherwise i will ask the user for exact dimensions.
Posted

Hi,

Try this...
bool withImage = true;
Byte[] imgByte = null;
var img = FileUpload1;
if  (img != null)
{
   if (img.HasFile && img.PostedFile != null)
   {
       withImage = true;
       //To create a PostedFile
       HttpPostedFile File = img.PostedFile;
       //Create byte Array with file len
       imgByte = new Byte[File.ContentLength];
       //Force the control to load data in array
       File.InputStream.Read(imgByte, 0, File.ContentLength);
       originalSize = File.ContentLength;

       System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imgByte)

       // Here you get the Height and the Width of original image
       var imgHeight = oldSize.Height;
       var imgWidth =  oldSize.Width
    }
}
else
{
   withImage = false;
}


Hope this could help...

Regards,
Algem
 
Share this answer
 
v2
Comments
TanzeelurRehman 6-Oct-11 13:55pm    
Is this possible to get the image Dimension using javascript, so that if the file to be uploaded verifies the appropriate dimension will be uploaded other wise it will show alert and cancel the uploading process.
Al Moje 7-Oct-11 1:40am    
Hi, I did not yet made a javascript rather function on such. Maybe you could google it.
By the way why you should check the dimension of an image? If your target is to save
such uploaded file into fix dimension eg. Width and Height, you could resize such uploaded image and save it with your target dimension you like...
see How to resize image next answer...
TanzeelurRehman 7-Oct-11 2:39am    
I can resize the image to my specified dimensions. But i want to get from user the appropriate image to upload. I google it a lot and read article in this regard. All the article says that it is impossible to do in javascript, due to security reason.
Hi,

This is a only a sample how to resize image:

private static byte[] ResizeImageFile(byte[] imageFile, Size targetSize)
        {
            using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
            {
                Size newSize = CalculateDimensions(oldImage.Size, targetSize.Height, targetSize.Width);
                using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                {
                    using (Graphics canvas = Graphics.FromImage(newImage))
                    {
                        canvas.SmoothingMode = SmoothingMode.AntiAlias;
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                        MemoryStream m = new MemoryStream();
                        newImage.Save(m, ImageFormat.Jpeg);
                        return m.GetBuffer();
                    }
                }
            }
        }

        private static Size CalculateDimensions(Size oldSize, int targetH, int targetW)
        {
            Size newSize = new Size();
            if (oldSize.Height > oldSize.Width)
            {
                newSize.Width = targetW;
                newSize.Height = targetH;
            }
            else
            {
                //Make the image as uniform with fix size.
                newSize.Width = targetW;
                newSize.Height = targetH;
            }
            return newSize;
        }


Hope this could help...

Regards,
Algem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900