/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="originalSourcePath">The original SourcePath to a file on Device OR An url to a picture</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <param name="addToFilename">What string should be after the originalSourcePath. if original is img20161203.jpg and addToFileName is -thumbnail then the outcome will be img20161203-thumbnail.jpg</param> /// <param name="removeFromOriginalSourceFilename">a string that should be removed from original source ex. originalSourcepath = "Image-fullImage.jpg" removeFromOriginalSourceFilename = "-fullImage" the resulting path string will be "Image"+"addToFilename+".jpg"</param> /// <returns></returns> async public Task <string> SmartCrop(string originalSourcePath, int width, int height, string addToFilename, string removeFromOriginalSourceFilename = null) { string newPath = null; if (string.IsNullOrEmpty(VisionApi.Key)) { throw new Exception("You must set VisionApi.Key"); } var originalBytes = File.ReadAllBytes(originalSourcePath); var thumbNailByteArray = await VisionApi.GetThumbNail(originalBytes, VisionApi.Key, width, height); var orSourcePath = originalSourcePath; if (!string.IsNullOrEmpty(removeFromOriginalSourceFilename)) { orSourcePath = orSourcePath.Replace(removeFromOriginalSourceFilename, ""); } var extension = orSourcePath.Substring(orSourcePath.LastIndexOf(".")); newPath = orSourcePath.Replace(extension, addToFilename + extension); File.WriteAllBytes(newPath, thumbNailByteArray); return(newPath); }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="stream">Stream of an image that is used to send to Vision api</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <returns>Byte array of new image</returns> async public Task <byte[]> SmartCrop(Stream stream, int width, int height) { if (stream.Length > 4000000) { throw new NotSupportedException("You are trying to SmartCrop a Stream that is bigger than 4Mb"); } return(await VisionApi.GetThumbNail(stream.ToByteArray(), width, height)); }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="stream">Stream of an image that is used to send to Vision api</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <param name="newFilePath">path to file that is going to be created</param> /// <returns>The path to the cropped image</returns> async public Task <string> SmartCrop(Stream stream, int width, int height, string newFilePath) { if (stream.Length > 4000000) { throw new NotSupportedException("You are trying to SmartCrop a Stream that is bigger than 4Mb"); } var thumbNailByteArray = await VisionApi.GetThumbNail(stream.ToByteArray(), width, height); File.WriteAllBytes(newFilePath, thumbNailByteArray); return(newFilePath); }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="originalSourcePath">The original SourcePath to a file on Device OR An url to a picture</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <returns>Byte array of new image</returns> async public Task <byte[]> SmartCrop(string originalSourcePath, int width, int height) { if (originalSourcePath.IsUrl()) { return(await VisionApi.GetThumbNail(originalSourcePath, width, height)); } else { var downSampledPath = await FixMaxImageSize(originalSourcePath, 4000000); var originalBytes = File.ReadAllBytes(downSampledPath); return(await VisionApi.GetThumbNail(originalBytes, width, height)); } }
/// <summary> /// Uses the Microsoft Vision API to generate a picture that crops automatically to whatever size you choose. /// </summary> /// <param name="originalSourcePath">The original SourcePath to a file on Device OR An url to a picture</param> /// <param name="width">Width of the cropped image</param> /// <param name="height">Height of the cropped image</param> /// <param name="addToFilename">What string should be after the originalSourcePath. if original is img20161203.jpg and addToFileName is -thumbnail then the outcome will be img20161203-thumbnail.jpg</param> /// <param name="removeFromOriginalSourceFilename">a string that should be removed from original source ex. originalSourcepath = "Image-fullImage.jpg" removeFromOriginalSourceFilename = "-fullImage" the resulting path string will be "Image"+"addToFilename+".jpg"</param> /// <returns></returns> public async Task <string> SmartCrop(string originalSourcePath, int width, int height, string addToFilename, string removeFromOriginalSourceFilename = null) { string newPath = null; byte[] thumbNailByteArray = null; if (originalSourcePath.IsUrl()) { thumbNailByteArray = await VisionApi.GetThumbNail(originalSourcePath, width, height); } else { var downSampledPath = await FixMaxImageSize(originalSourcePath, 4000000); var originalBytes = File.ReadAllBytes(downSampledPath); thumbNailByteArray = await VisionApi.GetThumbNail(originalBytes, width, height); } newPath = SetupNewSourcePath(originalSourcePath, removeFromOriginalSourceFilename, addToFilename); File.WriteAllBytes(newPath, thumbNailByteArray); return(newPath); }