public HttpResponseMessage CropTeamPicture(CropingArgs coords) { User currentUser = userBusiness.GetUserByExternalId(User.Identity.GetUserId()).Data; string root = GetTeamPicturesRootFolder(); string filePath = String.Format("{0}{1}_{2}_{3}_temp.{4}", root, Constants.Images.TeamPicturePrefix, currentUser.UserID, coords.ID, Constants.Images.TeamPictureExtension); string destFilePath = String.Format("{0}{1}_{2}_{3}.{4}", root, Constants.Images.TeamPicturePrefix, currentUser.UserID, coords.ID, Constants.Images.TeamPictureExtension); try { ImageUtil.CropImage(filePath, destFilePath, Constants.Images.TeamImageFormat, coords); // delete temporary file FileInfo fileInfo = new FileInfo(filePath); fileInfo.Delete(); fileInfo = new FileInfo(destFilePath); string retUrl = String.Format("{0}{1}", Constants.Images.TeamPictureRoot, fileInfo.Name); // if all ok update user Team team = competitorBusiness.GetTeamById((long)coords.ID).Data; if (team != null) { team.PictureUrl = retUrl; competitorBusiness.UpdateTeam(team); } return(new HttpResponseMessage() { Content = new StringContent(String.Format("{0}?nocache={1}", retUrl, DateTime.Now.Ticks)) }); } catch (System.Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e)); } }
public HttpResponseMessage CropGamePicture(CropingArgs coords) { //User currentUser = GetUserByEmail(User.Identity.Name); string root = GetGameCategpryPicturesRootFolder(); string filePath = String.Format("{0}{1}_{2}_temp.{3}", root, Constants.Images.GameCategoryPicturePrefix, coords.ID, Constants.Images.GameCategoryPictureExtension); string destFilePath = String.Format("{0}{1}_{2}.{3}", root, Constants.Images.GameCategoryPicturePrefix, coords.ID, Constants.Images.GameCategoryPictureExtension); try { ImageUtil.CropImage(filePath, destFilePath, Constants.Images.GameCategoryImageFormat, coords); // delete temporary file FileInfo fileInfo = new FileInfo(filePath); fileInfo.Delete(); fileInfo = new FileInfo(destFilePath); string retUrl = String.Format("{0}{1}", Constants.Images.GameCategoryPictureRoot, fileInfo.Name); // if all ok update category image GameCategory category = gameCategoryBusiness.GetById((int)(long)coords.ID).Data; category.PictureUrl = retUrl; gameCategoryBusiness.UpdateGameCategory(category); return(new HttpResponseMessage() { Content = new StringContent(String.Format("{0}?nocache={1}", retUrl, DateTime.Now.Ticks)) }); } catch (System.Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public static void CropImage(string sourcePath, string destinationPath, System.Drawing.Imaging.ImageFormat format, CropingArgs cropingCoords) { using (Image image = Image.FromFile(sourcePath)) { var newImage = new Bitmap(cropingCoords.H, cropingCoords.W); using (Graphics gr = Graphics.FromImage(newImage)) { gr.DrawImage(image, new Rectangle(0, 0, cropingCoords.W, cropingCoords.H), new Rectangle(cropingCoords.X, cropingCoords.Y, cropingCoords.W, cropingCoords.H), GraphicsUnit.Pixel); if (File.Exists(destinationPath)) { File.Delete(destinationPath); } newImage.Save(destinationPath, format); } } }