public async Task SaveProductImage(List <IFormFile> formFiles, int productId) { List <ItemImageDto> productImages = new List <ItemImageDto>(); string shortLocation = $"\\images\\items\\{productId}\\"; string uploads = Path.Combine(_hostingEnvironment.WebRootPath + shortLocation); if (!Directory.Exists(uploads)) { DirectoryInfo di = Directory.CreateDirectory(uploads); } foreach (var file in formFiles) { ////string fileName = file.FileName; //using (var memoryStream = new MemoryStream()) //{ // await file.CopyToAsync(memoryStream); // using (var img = new Bitmap(memoryStream)) // { // var bitmapa= MakeSquarePhoto(img, 800); // bitmapa.Save(memoryStream,); // } //} if (file.Length > 0) { //item.CopyTo(fileStream); var timeSpan = DateTime.Now.ToString("yyyyMMddHHmmssffff"); var extension = System.IO.Path.GetExtension(file.FileName); string newName = $"item_{timeSpan}.{extension}"; var filePath = Path.Combine(uploads, newName); //var image = FixedSize((Image)file, 620, 440, true); if (file == null || file.Length == 0) { return; } using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); using (var img = Image.FromStream(memoryStream)) { var resizedImage = FixedSize(img, 620, 440, false); using (var fileStream = new FileStream(filePath, FileMode.Create)) { resizedImage.Save(fileStream, ImageFormat.Jpeg); } } } ItemImageDto product = new ItemImageDto(); product.Source = $"/images/items/{productId}/{newName}"; product.ItemId = productId; var newProduct = await _itemImageService.Add(product); } } }
public bool AddItemImages(ItemImageDto dataDto) { if (dataDto != null) { using (EAharaDB context = new EAharaDB()) { if (dataDto.Id > 0) { var data = context.ItemImages.FirstOrDefault(x => x.Id == dataDto.Id); if (data != null) { data.Name = dataDto.Name; data.ItemId = dataDto.ItemId; //data.Image = itemDto.Image; context.Entry(data).Property(x => x.Name).IsModified = true; context.Entry(data).Property(x => x.ItemId).IsModified = true; //context.Entry(data).Property(x => x.Image).IsModified = true; if (dataDto.Image != null && dataDto.Image != "") { Guid id = Guid.NewGuid(); var imgData = dataDto.Image.Substring(dataDto.Image.IndexOf(",") + 1); byte[] bytes = Convert.FromBase64String(imgData); Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); } Bitmap b = new Bitmap(image); string filePath = System.Web.HttpContext.Current.Server.MapPath("~") + "UploadedFiles\\" + id + ".jpg"; b.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); data.Image = string.Concat("UploadedFiles\\" + id + ".jpg"); } context.SaveChanges(); return(true); } return(false); } else { ItemImage item = new ItemImage(); item.Name = dataDto.Name; item.ItemId = dataDto.ItemId; //item.Image = itemDto.Image; item.IsActive = true; if (dataDto.Image != null && dataDto.Image != "") { Guid id = Guid.NewGuid(); var imgData = dataDto.Image.Substring(dataDto.Image.IndexOf(",") + 1); byte[] bytes = Convert.FromBase64String(imgData); Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); } Bitmap b = new Bitmap(image); string filePath = System.Web.HttpContext.Current.Server.MapPath("~") + "UploadedFiles\\" + id + ".jpg"; b.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); item.Image = string.Concat("UploadedFiles\\" + id + ".jpg"); } context.ItemImages.Add(item); context.SaveChanges(); return(true); } } } return(false); }