public void ResizeImage(string _path, IFormFile uploadedFile, string file_name, int desiredWidth, int desiredHeight) { string webroot = _path; try { if (uploadedFile.Length > 0) { using (var stream = uploadedFile.OpenReadStream()) { var uploadedImage = System.Drawing.Image.FromStream(stream); //decide how to scale dimensions if (desiredHeight == 0 && desiredWidth > 0) { var img = ImageResize.ScaleByWidth(uploadedImage, desiredWidth); // returns System.Drawing.Image file img.SaveAs(Path.Combine(webroot, file_name)); } else if (desiredWidth == 0 && desiredHeight > 0) { var img = ImageResize.ScaleByHeight(uploadedImage, desiredHeight); // returns System.Drawing.Image file img.SaveAs(Path.Combine(webroot, file_name)); } else { var img = ImageResize.Scale(uploadedImage, desiredWidth, desiredHeight); // returns System.Drawing.Image file img.SaveAs(Path.Combine(webroot, file_name)); } } } } catch { } return; }
public async Task <UserPhoto> AddPhoto(IFormFile file) { if (file.Length > 0) { var uploadsFolderPath = Path.Combine(host.ContentRootPath, "wwwroot/uploads"); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadsFolderPath, fileName); using (var stream = file.OpenReadStream()) { var uploadedImage = Image.FromStream(stream); var imageWidth = uploadedImage.Width < 800?uploadedImage.Width:800; var img = ImageResize.ScaleByWidth(uploadedImage, imageWidth); img.SaveAs(filePath); await stream.DisposeAsync(); } var UserPhoto = new UserPhoto { ImageFullPath = filePath, ImageUrl = fileName }; return(UserPhoto); } throw new RestException(HttpStatusCode.BadRequest, new { UserPhoto = "Please select an image file" }); }
public void CreateScaledImage(Image image, string path, int width, int height = 0) { if (height > 0) { var finalImage = ImageResize.Scale(image, width, height); finalImage.SaveAs(path); } else { var finalImage = ImageResize.ScaleByWidth(image, width); finalImage.SaveAs(path); } }
public async Task <IActionResult> Get(string NewsID, string Size, string ImageName) { string str = ""; bool isSeek = false; var req = new ThumbnailRequest(); //req.RequestedPath = ImageName; req.ThumbnailSize = ParseSize(Size); req.SourceImagePath = GetPhysicalPath(NewsID + "/" + ImageName); if (IsSourceImageExists(req)) { using (Image img = Image.FromFile(req.SourceImagePath)) { try { Image file = null; str = "Width: " + req.ThumbnailSize.Value.Width + " Height: " + req.ThumbnailSize.Value.Height; if (req.ThumbnailSize.Value.Height >= req.ThumbnailSize.Value.Width) { file = ImageResize.ScaleByHeight(img, req.ThumbnailSize.Value.Height); } str = "2 Width: " + req.ThumbnailSize.Value.Width + " Height: " + req.ThumbnailSize.Value.Height; if (req.ThumbnailSize.Value.Width > req.ThumbnailSize.Value.Height) { file = ImageResize.ScaleByWidth(img, req.ThumbnailSize.Value.Width); } str = "3 Width: " + req.ThumbnailSize.Value.Width + " Height: " + req.ThumbnailSize.Value.Height; file = ImageResize.ScaleAndCrop(file, req.ThumbnailSize.Value.Width, req.ThumbnailSize.Value.Height, TargetSpot.Center); str = "4 Width: " + req.ThumbnailSize.Value.Width + " Height: " + req.ThumbnailSize.Value.Height; Stream outputStream = new MemoryStream(); file.Save(outputStream, System.Drawing.Imaging.ImageFormat.Jpeg); outputStream.Seek(0, SeekOrigin.Begin); str = "5 Width: " + req.ThumbnailSize.Value.Width + " Height: " + req.ThumbnailSize.Value.Height; return(this.File(outputStream, "image/png")); } catch (Exception ex) { return(NotFound(str + " " + ex.Message + " inner " + Convert.ToString(ex.InnerException))); } } } return(NotFound()); }
public byte[] ResizeImage(byte[] file, int desiredWidth) { if (file.Length > 0) { using (var stream = new MemoryStream(file)) { var image = System.Drawing.Image.FromStream(stream); var scaledImage = ImageResize.ScaleByWidth(image, desiredWidth); using (var ms = new MemoryStream()) { scaledImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); return(ms.ToArray()); } } } else { return(null); } }
public static byte[] SignatureImage(byte[] byteImage, int width) { using (var ms = new MemoryStream(byteImage)) { var image = Image.FromStream(ms); if (image.Width < width) { width = image.Width; } var newImg = ImageResize.ScaleByWidth(image, width); //add text watermark newImg.TextWatermark("RxFarmcias.com", "#77ffffff", //text color hex8 value, 77 is opacity (00-FF) "#22000000", //background color, 22 is for opacity (00-FF) "Calibri", //font family 34, //font size TargetSpot.BottomLeft, //target spot to place text watermark text FontStyle.Italic, //font style 10); //bg margin from border image.Dispose(); return(ImageToByteArray(newImg)); } }