Exemple #1
0
    public async Task <byte[]> GeneratePreviewAsync(string?url, CancellationToken token)
    {
        if (!string.IsNullOrEmpty(url))
        {
            try
            {
                var client = _httpClientFactory.CreateClient();
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));

                if (await GetPreviewImageUri(client, url, token) is Uri previewImageUri)
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/*"));

                    var imageBytes = await client.GetByteArrayAsync(previewImageUri, token);

                    return(ImageResizer.ResizeJpg(imageBytes));
                }
            }
            catch
            {
            }
        }

        return(await GetDefaultPreviewImageBytes(token));
    }
    public async Task <IActionResult> Save(ReceivedGift model, CancellationToken token = default)
    {
        if (ModelState.IsValid && await this.GetAccountAsync(_accountRepository, token) is Account account)
        {
            if (model.ImageUpload is not null && model.ImageUpload.Length > 0)
            {
                using var imageStream = model.ImageUpload.OpenReadStream();

                model.Image = ImageResizer.ResizeJpg(imageStream);
            }

            account.ReceivedGift[DateHelper.Year] = model;

            await _accountRepository.SaveAsync(account, token);

            this.SetResultMessage("Successfully updated your recieved gift info.");
        }

        return(RedirectToAction("Index"));
    }
        public ActionResult Upload()
        {
            var latestFile = new Artwork();
            var vm         = new ArtworkViewModel();

            for (int i = 0; i < Request.Files.Count; i++)
            {
                var existingBlobs = (List <ArtworkViewModel>)Session["uploads"] ?? new List <ArtworkViewModel>();

                var file = Request.Files[i];

                BinaryReader b       = new BinaryReader(file.InputStream);
                byte[]       binData = b.ReadBytes(file.ContentLength);

                var thumbnail = new byte[0];


                byte[] resizedImage;

                var size        = ImageResizer.GetHeightAndWidth(binData);
                var orientation = ImageResizer.GetOrientation(binData);

                if (orientation == "landscape")
                {
                    thumbnail = ImageResizer.ResizeJpg(binData, 270, 200);
                }
                else
                {
                    thumbnail = ImageResizer.ResizeJpgFixedWidth(binData, 270);
                    thumbnail = ImageResizer.CropJpg(thumbnail, new Rectangle(0, 0, 270, 200));
                    //now crop
                }

                //need to make sure it's not too big
                if (size.Width > 1024)
                {
                    binData = ImageResizer.ResizeJpgFixedWidth(binData, 1024);
                }

                latestFile = new Artwork
                {
                    FileName     = file.FileName.Split('.')[0],
                    Extension    = "." + file.FileName.Split('.')[1],
                    Bytes        = binData,
                    ResizedBytes = thumbnail,
                    WhenCreated  = DateTime.Now,
                    Orientation  = orientation,
                    ForSale      = true
                };


                Session["uploads"] = existingBlobs;

                if (Session["uploadsCount"] == null)
                {
                    Session["uploadsCount"] = 0;
                }
                else
                {
                    var existingCount = (int)Session["uploadsCount"];
                    Session["uploadsCount"] = existingCount++;
                }


                vm = new ArtworkViewModel
                {
                    Artwork = latestFile,
                    Count   = (int)Session["uploadsCount"]
                };

                existingBlobs.Add(vm);

                Session["uploads"] = existingBlobs;
            }



            return(Json("Ok"));
        }