如何在Javascript中检查图像的尺寸 [英] How to check Dimension of a Image in Javascript

查看:63
本文介绍了如何在Javascript中检查图像的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专业人士

希望大家一切都好.我想实现一个javascript模块,可以在其中检查图像文件的尺寸,
该图像文件是由asp.net的FileUpload Control从客户端选择的.


源代码是

.aspx部分

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部分



.aspx.cs portion

if (!IsPostBack)
        {
            FileUpload1.Attributes.Add("onchange", "CheckFileDimension();");

        }



JavaScript部分是



Javascript portion is

<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>



如果有可能使用javascript,请在​​这方面指导我.如果不是,如何在服务器端检入C#,而在保存该映像之前不影响输入流.如果图像的尺寸正确,那么我会将文件保存到服务器,否则我将询问用户确切的尺寸.



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.

推荐答案

您好,

试试这个...
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




这只是如何调整图像大小的一个示例:

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


这篇关于如何在Javascript中检查图像的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆