检查上传图片的尺寸
我在我的网站上使用 asp.net 3.5 和 c#。这是我的问题:
我的页面上有一个上传按钮和 asp:Image。用户可以从他的计算机上传图像,该图像将显示在 asp:image 中。但在显示图像之前,我想检查上传图像的宽度和高度。我该怎么做?
I'm using asp.net 3.5 and c# on my web site. Here is my question:
I have an upload button and asp:Image on a page. An user can upload an image from his computer and that image will be displayed in the asp:image. But before I display the image, I would like to check the width and height of the uploaded image. How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能需要添加 System.Drawing 引用。
如果您尚未将图像保存到磁盘,您也可以使用
FromStream
函数,但看看您如何使用图像(用户可以在图像控件中查看),我怀疑它已经打开磁盘。流到图像可能比磁盘到图像快,也可能不快。您可能需要进行一些分析以查看哪个具有更好的性能。You may need to add the System.Drawing reference.
You may also use the
FromStream
function if you have not saved the image to disk yet, but looking at how you're using the image (viewable by user in an Image control), I suspect it's already on disk. Stream to image may or may not be faster than disk to image. You might want to do some profiling to see which has better performance.在 ASP.NET 中,上传文件时通常会使用 byte[] 或 Stream。下面,我向您展示一种方法,其中 bytes 是上传文件的 byte[]。如果您首先保存文件,那么您就有了一个物理文件。您可以使用@Jakob 或@Fun Mun Pieng 向您展示的内容。
无论哪种方式,请务必像我在此处所示的那样处理您的 Image 实例。这非常重要(其他人没有展示这一点)。
In ASP.NET you typically have the byte[] or the Stream when a file is uploaded. Below, I show you one way to do this where bytes is the byte[] of the file uploaded. If you're saving the file fisrt then you have a physical file. and you can use what @Jakob or @Fun Mun Pieng have shown you.
Either ways, be SURE to dispose your Image instance like I've shown here. That's very important (the others have not shown this).
请尝试以下操作:
Try the following:
将图像加载到 图像 并检查服务器端的尺寸?
Load the image into an Image and check the dimensions serverside?
试试这个:
Try this:
试试这个。
使用:
Try this.
Use:
这就是我检查控制器 ActionResult 的方法:
我希望这可以帮助遇到这个问题的其他人。
This is how I do my checking in the Controller ActionResult:
I hope this helps someone else who runs in to this problem.