Пример #1
0
        public async Task LoadClubImages()
        {
            string filePath = HttpContext.Current.Server.MapPath("/Images/Clubs");
            var    clubs    = _clubRepository.GetAllList();

            foreach (var club in clubs)
            {
                string      imagefilename = string.Format("Club{0}.jpg", club.Id);
                string      imageFile     = Path.Combine(filePath, imagefilename);
                ImageFormat imgFormat     = ImageFormat.Jpeg;

                if (!File.Exists(imageFile))
                {
                    imageFile = Path.ChangeExtension(imageFile, "png");
                    imgFormat = ImageFormat.Png;
                }
                if (File.Exists(imageFile))
                {
                    Image image = Image.FromFile(imageFile);
                    var   input = new UpdateClubImageInput {
                        ClubId = club.Id
                    };
                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Save(ms, imgFormat);
                        input.Image = ms.ToArray();
                    }
                    var clubImage = _clubImageRepository.FirstOrDefault(input.ClubId);
                    if (clubImage == null)
                    {
                        await _clubImageRepository.InsertAsync(new ClubImage { Id = input.ClubId, Image = input.Image });
                    }
                    else
                    {
                        clubImage.Image = input.Image;
                        await _clubImageRepository.UpdateAsync(clubImage);
                    }
                }
            }
        }
Пример #2
0
        public async Task UpdateClubImage(UpdateClubImageInput input)
        {
            if (input.Image == null || input.Image.Length == 0)
            {
                await _clubImageRepository.DeleteAsync(input.ClubId);
            }
            else
            {
                input.Image = ImageHelper.ResizeImage(input.Image, 480, 272);
                var clubImage = _clubImageRepository.FirstOrDefault(input.ClubId);

                if (clubImage == null)
                {
                    await _clubImageRepository.InsertAsync(new ClubImage { Id = input.ClubId, Image = input.Image });
                }
                else
                {
                    clubImage.Image = input.Image;
                    await _clubImageRepository.UpdateAsync(clubImage);
                }
            }
        }