Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 将该图像转换为缩略图,并将两个图像保存在asp.net-mvc3中_Asp.net Mvc 3_Image_File Upload - Fatal编程技术网

Asp.net mvc 3 将该图像转换为缩略图,并将两个图像保存在asp.net-mvc3中

Asp.net mvc 3 将该图像转换为缩略图,并将两个图像保存在asp.net-mvc3中,asp.net-mvc-3,image,file-upload,Asp.net Mvc 3,Image,File Upload,通过上载控件上载图像并将该图像转换为缩略图,并将文件实际图像保存在图像文件夹中,将缩略图图像保存在asp.net-mvc3的缩略图文件夹中。我在上传图像和保持图像质量方面遇到了困难,这个库帮了我很多忙 在你看来: @model YourProject.ViewModels.ProductImageUploadCreateViewModel @using (Html.BeginForm("Upload", "ProductImage", FormMethod.Post, new { enctype

通过上载控件上载图像并将该图像转换为缩略图,并将文件实际图像保存在图像文件夹中,将缩略图图像保存在asp.net-mvc3的缩略图文件夹中。我在上传图像和保持图像质量方面遇到了困难,这个库帮了我很多忙

在你看来:

@model YourProject.ViewModels.ProductImageUploadCreateViewModel
@using (Html.BeginForm("Upload", "ProductImage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
     <input type="file" name="ImageFile1" id="ImageFile1">
}
您的上传方法:

private void UploadImage(int productId, string imageNo, HttpPostedFileBase imageFile)
{
     string uploadPath = Server.MapPath("~/Assets/Images/Products/" + productId);
     if (!Directory.Exists(uploadPath))
     {
          Directory.CreateDirectory(uploadPath);
     }

     Dictionary<string, string> versions = new Dictionary<string, string>();
     versions.Add("_m", "width=150&height=150&scale=both&format=jpg");  // Medium size

     string filePrefix = productId + "_" + imageNo;
     versions.Add("_s", "width=90&height=90&scale=both&format=jpg");  // Small size
     versions.Add("_l", "width=300&height=300&scale=both&format=jpg");  // Large size

     foreach (string fileSuffix in versions.Keys)
     {
          // Generate a filename
          string fileName = Path.Combine(uploadPath, filePrefix + fileSuffix);

          // Let the image builder add the correct extension based on the output file type
          fileName = ImageBuilder.Current.Build(imageFile, fileName, new ResizeSettings(versions[fileSuffix]), false, true);
     }
}
private void UploadImage(int-productId,string-imageNo,HttpPostedFileBase-imageFile)
{
字符串uploadPath=Server.MapPath(“~/Assets/Images/Products/”+productId);
如果(!Directory.Exists(uploadPath))
{
CreateDirectory(上传路径);
}
字典版本=新字典();
versions.Add(“_m”,“宽度=150,高度=150,比例=both&format=jpg”);//中等大小
字符串filePrefix=productId+“”+imageNo;
versions.Add(“_s”,“宽度=90,高度=90,比例=both&format=jpg”);//小尺寸
versions.Add(“_l”,“width=300&height=300&scale=both&format=jpg”);//大尺寸
foreach(versions.Keys中的字符串文件uffix)
{
//生成文件名
string fileName=Path.Combine(上传路径,filePrefix+fileSuffix);
//让图像生成器根据输出文件类型添加正确的扩展名
fileName=ImageBuilder.Current.Build(imageFile,fileName,新的大小设置(版本[fileSuffix]),false,true);
}
}

看看他们的网站,有几个例子,你可以通过。我希望这有帮助:)

我使用一个名为的第三方库。我在上传图像和保持图像质量方面遇到了困难,这个库帮了我很多忙

在你看来:

@model YourProject.ViewModels.ProductImageUploadCreateViewModel
@using (Html.BeginForm("Upload", "ProductImage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
     <input type="file" name="ImageFile1" id="ImageFile1">
}
您的上传方法:

private void UploadImage(int productId, string imageNo, HttpPostedFileBase imageFile)
{
     string uploadPath = Server.MapPath("~/Assets/Images/Products/" + productId);
     if (!Directory.Exists(uploadPath))
     {
          Directory.CreateDirectory(uploadPath);
     }

     Dictionary<string, string> versions = new Dictionary<string, string>();
     versions.Add("_m", "width=150&height=150&scale=both&format=jpg");  // Medium size

     string filePrefix = productId + "_" + imageNo;
     versions.Add("_s", "width=90&height=90&scale=both&format=jpg");  // Small size
     versions.Add("_l", "width=300&height=300&scale=both&format=jpg");  // Large size

     foreach (string fileSuffix in versions.Keys)
     {
          // Generate a filename
          string fileName = Path.Combine(uploadPath, filePrefix + fileSuffix);

          // Let the image builder add the correct extension based on the output file type
          fileName = ImageBuilder.Current.Build(imageFile, fileName, new ResizeSettings(versions[fileSuffix]), false, true);
     }
}
private void UploadImage(int-productId,string-imageNo,HttpPostedFileBase-imageFile)
{
字符串uploadPath=Server.MapPath(“~/Assets/Images/Products/”+productId);
如果(!Directory.Exists(uploadPath))
{
CreateDirectory(上传路径);
}
字典版本=新字典();
versions.Add(“_m”,“宽度=150,高度=150,比例=both&format=jpg”);//中等大小
字符串filePrefix=productId+“”+imageNo;
versions.Add(“_s”,“宽度=90,高度=90,比例=both&format=jpg”);//小尺寸
versions.Add(“_l”,“width=300&height=300&scale=both&format=jpg”);//大尺寸
foreach(versions.Keys中的字符串文件uffix)
{
//生成文件名
string fileName=Path.Combine(上传路径,filePrefix+fileSuffix);
//让图像生成器根据输出文件类型添加正确的扩展名
fileName=ImageBuilder.Current.Build(imageFile,fileName,新的大小设置(版本[fileSuffix]),false,true);
}
}

看看他们的网站,有几个例子,你可以通过。我希望这有帮助:)

是否允许您使用第三方库来调整大小?我这里有我用来调整图像大小的代码,但是它使用了第三方库。你可以使用第三方库来调整大小吗?我这里有我用来调整图像大小的代码,但它使用了第三方库。