我在我的网站上使用asp.net 3.5和c#。 这是我的问题:
我有一个上传按钮和asp:页面上的图像。用户可以从他的计算机上传图像,该图像将显示在asp:image中。但在我显示图像之前,我想检查上传图像的宽度和高度。我该怎么做?
答案 0 :(得分:22)
Image img = System.Drawing.Image.FromFile("test.jpg");
int width = img.Width;
int height = img.Height;
您可能需要添加System.Drawing参考。
如果您尚未将图像保存到磁盘,也可以使用FromStream
功能,但是看看您如何使用图像(用户可以在图像控件中查看),我怀疑它已经在磁盘。流到图像可能比也可能不比磁盘到图像更快。您可能希望进行一些分析以查看哪个具有更好的性能。
答案 1 :(得分:13)
在ASP.NET中,上传文件时通常会有byte []或Stream。下面,我向您展示了一种方法,其中bytes是上传文件的byte []。如果您要保存文件fisrt,那么您有一个物理文件。并且您可以使用@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;
}
}
答案 2 :(得分:7)
尝试以下方法:
public bool ValidateFileDimensions()
{
using(System.Drawing.Image myImage =
System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
{
return (myImage.Height == 140 && myImage.Width == 140);
}
}
答案 3 :(得分:3)
将图片加载到Image并检查维度服务器端?
Image uploadedImage = Image.FromFile("uploadedimage.jpg");
// uploadedImage.Width and uploadedImage.Height will have the dimensions...
答案 4 :(得分:1)
试试这个:
Stream ipStream = fuAttachment.PostedFile.InputStream;
using (var image = System.Drawing.Image.FromStream(ipStream))
{
float w = image.PhysicalDimension.Width;
float h = image.PhysicalDimension.Height;
}
答案 5 :(得分:0)
试试这个。
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
}
答案 6 :(得分:0)
这是我在Controller 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);
}
}