private void btn_compressImage_Click(object sender, EventArgs e) { var inpath = this.lb_imagePath.Text; if (string.IsNullOrWhiteSpace(inpath)) { MessageBox.Show("请选择要压缩的图片"); return; } var outpath = string.Empty; FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); if (folderBrowserDialog.ShowDialog() != DialogResult.OK) { return; } outpath = Path.Combine(folderBrowserDialog.SelectedPath, Guid.NewGuid().ToString() + Path.GetExtension(inpath)); try { var imageReductionMultiplier = (int)num_imageReductionMultiplier.Value; var compressRatio = (int)num_compressionRatio.Value; ImageCompressHelper.imageCompress(inpath, outpath, imageReductionMultiplier, compressRatio); MessageBox.Show("压缩完成"); } catch { MessageBox.Show("压缩失败,请确认文件格式是否正确!"); } }
public IActionResult UpdateImage(string ImageName, string ModuleName, int RowId) { var response = new OperationResponse <ProductImages>(); try { string OriginalImagePath = _configuration["ImagePathConfiguration:OriginalImagePath"]; string ThumbnailImagePath = _configuration["ImagePathConfiguration:ThumbnailImagePath"]; if (Request.Form.Files.Count > 0) { var file = Request.Form.Files[0]; var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), OriginalImagePath); var fullPath = Path.Combine(pathToSave, ImageName); if (System.IO.File.Exists(fullPath)) { ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, ImageName); } else { int MaxId = _productImageService.GetMaxProductImageId(); var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fileExt = fileName.Remove(0, fileName.LastIndexOf('.')); fileName = (MaxId + 1).ToString() + fileExt; ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, fileName); ProductImages request = new ProductImages(); request.ImageName = fileName; request.ImagePath = Path.Combine(OriginalImagePath, fileName); request.ThumbnailPath = Path.Combine(ThumbnailImagePath, fileName); request.Description = ""; request.DisplayOrder = 1; request.IsDisplay = true; response.Data = _productImageService.AddProductImages(request); // Update Image in Module as well _productImageService.UpdateImage(request.RowId, ModuleName, RowId); } } else { response.Messages.Add("Please upload Image"); } } catch (Exception exception) { response.State = ResponseState.Error; response.Messages.Add(exception.Message); _logger.LogError(exception, "Error in UpdateImage ==>" + exception.StackTrace); } return(new JsonResult(response)); }
public IActionResult UploadMultipleFiles(int ProductId) { var response = new OperationResponse <ProductImages>(); try { if (Request.Form.ContainsKey("ProductId")) { ProductId = Convert.ToInt32(Request.Form["ProductId"].ToString()); } if (Request.Form.Files.Count > 0) { foreach (var item in Request.Form.Files) { int MaxId = _productImageService.GetMaxProductImageId(); var file = item; string OriginalImagePath = _configuration["ImagePathConfiguration:OriginalImagePath"]; string ThumbnailImagePath = _configuration["ImagePathConfiguration:ThumbnailImagePath"]; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fileExt = fileName.Remove(0, fileName.LastIndexOf('.')); fileName = (MaxId + 1).ToString() + fileExt; ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, fileName); ProductImages request = new ProductImages(); request.ImageName = fileName; request.ImagePath = Path.Combine(OriginalImagePath, fileName); request.ThumbnailPath = Path.Combine(ThumbnailImagePath, fileName); request.Description = ""; request.DisplayOrder = 1; request.IsDisplay = true; response.Data = _productImageService.AddProductImages(request); ProductImageProduct request2 = new ProductImageProduct(); request2.ProductId = Convert.ToInt32(ProductId); request2.ProductImageId = response.Data.RowId; _productImageService.AddProductImage_Product(request2); } } else { response.Messages.Add("Please upload Image"); } } catch (Exception exception) { response.State = ResponseState.Error; response.Messages.Add(exception.Message); } return(new JsonResult(response)); }
public IActionResult UploadMultipleFiles(int ProductId) { var response = new OperationResponse <ProductImages>(); try { if (Request.Form.Files.Count > 0) { foreach (var item in Request.Form.Files) { int MaxId = _productImageService.GetMaxProductImageId(); var file = item; string OriginalImagePath = _configuration["ImagePathConfiguration:OriginalImagePath"]; string ThumbnailImagePath = _configuration["ImagePathConfiguration:ThumbnailImagePath"]; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fileExt = fileName.Remove(0, fileName.LastIndexOf('.')); fileName = (MaxId + 1).ToString() + fileExt; ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, fileName); //var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), "Resources"); //var fullPath = Path.Combine(pathToSave, fileName); //using (var stream = new FileStream(fullPath, FileMode.Create)) //{ // file.CopyTo(stream); //} ProductImages request = new ProductImages(); request.ImageName = fileName; request.ImagePath = Path.Combine(OriginalImagePath, fileName); request.ThumbnailPath = Path.Combine(ThumbnailImagePath, fileName); request.Description = ""; request.DisplayOrder = 1; request.IsDisplay = true; response.Data = _productImageService.AddProductImages(request); } } else { response.Messages.Add("Please upload Image"); } } catch (Exception exception) { response.State = ResponseState.Error; response.Messages.Add(exception.Message); _logger.LogError(exception, "Error in AddProductCategory ==>" + exception.StackTrace); } return(new JsonResult(response)); }
public IActionResult Upload() { var response = new OperationResponse <ProductImages>(); try { if (Request.Form.Files.Count > 0) { int MaxId = _productImageService.GetMaxProductImageId(); var file = Request.Form.Files[0]; string OriginalImagePath = _configuration["ImagePathConfiguration:OriginalImagePath"]; string ThumbnailImagePath = _configuration["ImagePathConfiguration:ThumbnailImagePath"]; var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fileExt = fileName.Remove(0, fileName.LastIndexOf('.')); fileName = (MaxId + 1).ToString() + fileExt; ImageCompressHelper.CompressImage(OriginalImagePath, ThumbnailImagePath, file, fileName); ProductImages request = new ProductImages(); request.ImageName = fileName; request.ImagePath = Path.Combine(OriginalImagePath, fileName); request.ThumbnailPath = Path.Combine(ThumbnailImagePath, fileName); request.Description = ""; request.DisplayOrder = 1; request.IsDisplay = true; response.Data = _productImageService.AddProductImages(request); } else { response.Messages = new List <string>(); response.Messages.Insert(0, @"Please upload logo."); } } catch (Exception exception) { response.State = ResponseState.Error; response.Messages.Add(exception.Message); _logger.LogError(exception, "Error in AddProductCategory ==>" + exception.StackTrace); } return(new JsonResult(response)); }
public IActionResult UploadMultipleFilesNew() { var response = new OperationResponse <bool>(); try { if (Request.Form.Files.Count > 0) { foreach (var item in Request.Form.Files) { int MaxId = _productImageService.GetMaxProductImageId(); var file = item; string Temp1ImagePath = _configuration["ImagePathConfiguration:Temp1ImagePath"]; string Temp2ImagePath = _configuration["ImagePathConfiguration:Temp2ImagePath"]; Random r = new Random(); var x = r.Next(0, 1000000); string number = x.ToString("000000"); var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fileExt = fileName.Remove(0, fileName.LastIndexOf('.')); fileName = number + fileExt; ImageCompressHelper.CompressImageNew(Temp1ImagePath, Temp2ImagePath, file, fileName); response.Data = true; } } else { response.Messages.Add("Please upload Image"); } } catch (Exception exception) { response.State = ResponseState.Error; response.Messages.Add(exception.Message); } return(new JsonResult(response)); }