【发布时间】:2011-07-08 14:02:40
【问题描述】:
我在我的网站上使用 asp.net 3.5 和 c#。这是我的问题:
我在页面上有一个上传按钮和 asp:Image。用户可以从他的计算机上传图像,该图像将显示在 asp:image 中。但在显示图像之前,我想检查上传图像的宽度和高度。我该怎么做?
【问题讨论】:
标签: c# asp.net image dimensions
我在我的网站上使用 asp.net 3.5 和 c#。这是我的问题:
我在页面上有一个上传按钮和 asp:Image。用户可以从他的计算机上传图像,该图像将显示在 asp:image 中。但在显示图像之前,我想检查上传图像的宽度和高度。我该怎么做?
【问题讨论】:
标签: c# asp.net image dimensions
这就是我检查控制器 ActionResult 的方式:
public ActionResult ThumbnailUpload(HttpPostedFileBase image, PressRelease pr)
{
var id = pr.Id;
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
PressRelease pressRelease = db.PressReleases.Find(id);
if (pressRelease == null)
{
return HttpNotFound();
}
bool success = false;
var imgName = "";
var path = "";
ViewBag.Message = "";
try
{
//Check for uploaded file
//You can set max file upload size in your Web.config file
if (image.ContentLength > 0)
{
//Check the uploaded file is of the type needed/required
if (Path.GetExtension(image.FileName) == ".jpg")
{
//Get the uploaded file from HttpPostedFileBase
System.IO.Stream stream = image.InputStream;
//Convert the uploaded file to Image
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
//Get the Width and Height of the uploaded file
int width = img.Width;
int height = img.Height;
//Check the Width & Height are the dimensions you want
if (width != 150 && height != 175)
{
//If the Width & Height do not meet your specifications, write a message to the user
success = false;
ViewBag.Message = "Thumbnail upload failed.";
ViewBag.Exception = "Image dimensions must 150w by 175h.";
//Return the view with the Model so error messages get displayed
return View("UploadImages", pressRelease);
}
else
{
//If Width & Height meet your specs, continue with uploading and saving the image
imgName = Path.GetFileName(image.FileName);
path = Path.Combine(Server.MapPath("...Your File Path..."), imgName);
image.SaveAs(path);
success = true;
ViewBag.Success = success;
ViewBag.Message = "Thumbnail uploaded successfully.";
pressRelease.ThumbName = Path.GetFileNameWithoutExtension(image.FileName);
TryUpdateModel(pressRelease);
db.SaveChanges();
}
}
else if(Path.GetExtension(image.FileName) != ".jpg")
{
//If the uploaded file is not of the type needed write message to user
success = false;
ViewBag.Message = "Thumbnail upload failed.";
ViewBag.Exception = "Uploaded file must have '.jpg' as the file extension.";
return View("UploadImages", pressRelease);
}
}
return View("UploadImages", pressRelease);
}
catch (Exception ex)
{
ViewBag.Success = success;
ViewBag.Exception = ex.Message;
ViewBag.Message = "Thumbnail upload failed.";
return View("UploadImages", pressRelease);
}
}
【讨论】:
试试这个。
public boolean CheckImgDimensions(string imgPath, int ValidWidth , int ValidHeight){
var img = Image.FromFile(Server.MapPath(imgPath));
return (img.width == ValidWidth && img.height == ValidHeight );
}
用途:
if ( CheckImgDimensions("~/Content/img/MyPic.jpg",128,128) ){
/// what u want
}
【讨论】:
试试这个:
Stream ipStream = fuAttachment.PostedFile.InputStream;
using (var image = System.Drawing.Image.FromStream(ipStream))
{
float w = image.PhysicalDimension.Width;
float h = image.PhysicalDimension.Height;
}
【讨论】:
尝试以下方法:
public bool ValidateFileDimensions()
{
using(System.Drawing.Image myImage =
System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
{
return (myImage.Height == 140 && myImage.Width == 140);
}
}
【讨论】:
在 ASP.NET 中,您通常在上传文件时使用 byte[] 或 Stream。下面,我将向您展示一种方法,其中 bytes 是上传文件的 byte[]。如果您首先保存文件,那么您有一个物理文件。你可以使用@Jakob 或@Fun Mun Pieng 向你展示的内容。
无论哪种方式,请务必像我在这里展示的那样处理您的 Image 实例。这很重要(其他人没有展示这一点)。
using (Stream memStream = new MemoryStream(bytes))
{
using (Image img = System.Drawing.Image.FromStream(memStream))
{
int width = img.Width;
int height = img.Height;
}
}
【讨论】:
Image img = System.Drawing.Image.FromFile("test.jpg");
int width = img.Width;
int height = img.Height;
您可能需要添加 System.Drawing 引用。
如果您还没有将图像保存到磁盘,您也可以使用FromStream 函数,但是看看您如何使用图像(用户可以在图像控件中查看),我怀疑它已经在磁盘上。流到映像可能比磁盘到映像快,也可能不快。您可能需要进行一些分析,看看哪个性能更好。
【讨论】:
HttpPostedFileBase.ContentLength 属性,以免上传大于 1-2 兆字节的图片。
将图像加载到Image 并检查服务器端的尺寸?
Image uploadedImage = Image.FromFile("uploadedimage.jpg");
// uploadedImage.Width and uploadedImage.Height will have the dimensions...
【讨论】: