示例#1
0
        public async Task <IActionResult> OnPostCrop([FromBody] CropRequest cropmodel)
        {
            FileStorage _storage = new FileStorage();
            string      filename = await _storage.Crop(cropmodel, _configuration["Photo"]);

            var result = new object();

            try
            {
                result = new
                {
                    status = "success",
                    url    = filename
                };
            }
            catch (Exception e)
            {
                result = new
                {
                    status = "fail",
                    url    = "",
                };
            }
            return(new JsonResult(result));
        }
示例#2
0
        public async System.Threading.Tasks.Task <string> Crop(CropRequest cropmodel, string path)
        {
            //// extract original image ID and generate a new filename for the cropped result
            var extension = Path.GetExtension(cropmodel.imgUrl);
            var croppedId = GenerateFileName(extension);
            var result    = new object();

            try
            {
                // load the original picture and resample it to the scaled values
                Stream imgstream = await GetAsync(path + cropmodel.imgUrl);

                var img = System.Drawing.Image.FromStream(imgstream);

                var bitmap = ImageUtils.Resize(img, (int)cropmodel.imgW, (int)cropmodel.imgH);
                System.Drawing.Image croppedBitmap = ImageUtils.Crop(bitmap, cropmodel.imgX1, cropmodel.imgY1, (int)cropmodel.cropW, (int)cropmodel.cropH);
                croppedBitmap.Save(path + croppedId);
                File.Delete(path + cropmodel.imgUrl);

                return(croppedId);
            }
            catch (Exception e)
            {
                return(e.Message + "//" + e.StackTrace);
            }
        }
示例#3
0
        public async Task <IActionResult> Post(CropRequest request)
        {
            try
            {
                Crop crop = _mapper.Map(request);

                crop = await _repository.AddAsync(crop);

                return(CreatedAtAction(nameof(GetById), new { id = crop.Id }, _mapper.Map(crop)));
            }

            catch (DataStoreException e)
            {
                _logger.LogError(e.Message, e, request);
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
示例#4
0
        public async Task <ActionResult> Crop([FromBody] CropRequest model)
        {
            // extract original image ID and generate a new filename for the cropped result
            var originalUri = new Uri(model.imgUrl);
            var originalId  = originalUri.Query.Replace("?id=", "");           //.Last();

            //var extension = Path.GetExtension(originalId);
            //var croppedId = GenerateIdFor(model.CroppedWidth, model.CroppedHeight, extension);

            try
            {
                var ms  = new MemoryStream(await _imageService.GetImageAsync(ImageTypes.Avatar, originalId));
                var img = Image.FromStream(ms);

                // load the original picture and resample it to the scaled values
                var bitmap = ImageUtils.Resize(img, (int)model.imgW, (int)model.imgH);

                var croppedBitmap = ImageUtils.Crop(bitmap, model.imgX1, model.imgY1, model.cropW, model.cropH);

                ImageConverter converter = new ImageConverter();
                byte[]         imgArray  = (byte[])converter.ConvertTo(croppedBitmap, typeof(byte[]));

                await _imageService.SaveImageAsync(ImageTypes.Avatar, originalId, imgArray);
            }
            catch (Exception e)
            {
                return(BadRequest());
            }

            var obj = new
            {
                status = CroppicStatuses.Success,
                url    = originalId
            };

            return(CreatedAtAction(nameof(Crop), new { id = obj.url }, obj));
        }
示例#5
0
        public HttpResponseMessage Crop([FromBody] CropRequest model)
        {
            // extract original image ID and generate a new filename for the cropped result
            var originalUri = new Uri(model.imgUrl);
            var originalId  = originalUri.Query.Replace("?id=", "");           //.Last();

            //var extension = Path.GetExtension(originalId);
            //var croppedId = GenerateIdFor(model.CroppedWidth, model.CroppedHeight, extension);

            try
            {
                var ms  = new MemoryStream(_imageService.GetImage(ImageTypes.Avatar, originalId));
                var img = Image.FromStream(ms);

                // load the original picture and resample it to the scaled values
                var bitmap = ImageUtils.Resize(img, (int)model.imgW, (int)model.imgH);

                var croppedBitmap = ImageUtils.Crop(bitmap, model.imgX1, model.imgY1, model.cropW, model.cropH);

                ImageConverter converter = new ImageConverter();
                byte[]         imgArray  = (byte[])converter.ConvertTo(croppedBitmap, typeof(byte[]));

                _imageService.SaveImage(ImageTypes.Avatar, originalId, imgArray);
            }
            catch (Exception e)
            {
                throw HttpStatusCode.InternalServerError.AsException();
            }

            var obj = new
            {
                status = CroppicStatuses.Success,
                url    = originalId
            };

            return(Request.CreateResponse(HttpStatusCode.OK, obj));
        }
示例#6
0
        public async Task <IActionResult> Put(Guid id, CropRequest request)
        {
            try
            {
                Crop crop = await _repository.GetByIdAsync(id);

                if (crop == null)
                {
                    return(NotFound());
                }

                _mapper.Map(crop, request);

                await _repository.UpdateAsync(crop);

                return(Ok());
            }

            catch (DataStoreException e)
            {
                _logger.LogError(e.Message, e, request);
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
示例#7
0
 public async Task <ActionResult <Picture> > AddText(string id, [FromBody] CropRequest req) =>
 await ModifyPictureAndSaveForUser(id, pic => _modifier.Crop(pic, req.Rectangle));