public static Fotka AddPhotoTypeToPhoto(Fotka photo, TypFotky type)
        {
            var doSave = false;

            if(photo.TypyFotek == null)
            {
                photo.TypyFotek = new List<TypFotky>{type};
                doSave = true;
            }
            else
            {
                if(photo.TypyFotek.All(p => p.SystemName.ToLower() != type.SystemName.ToLower()))
                {
                    photo.TypyFotek.Add(type);
                    doSave = true;
                }
            }

            if(doSave)
            {
                Save(photo);
            }

            return photo;
        }
        public static void ResizeImage(Fotka fotka, TypFotky fromType, TypFotky targetType)
        {
            if(FotkaManager.FotkaTypExist(fotka, targetType))
            {
                //TODO: double check photoType really exists on disk.
                return;
            }

            ResizeImage(fotka, fromType, targetType, null);
        }
        public static string GetPhotoFullPath(Fotka fotka, TypFotky photoType)
        {
            if (fotka == null || photoType == null) { throw new ArgumentException("fotka nebo typ"); }

            if (fotka.TypyFotek != null && fotka.TypyFotek.Any(f => f.SystemName.ToLower() == photoType.SystemName.ToLower()))
            {
                var typeFolder = fotka.TypyFotek.First(f => f.SystemName.ToLower() == photoType.SystemName.ToLower()).Adresar;
                return HttpContext.Current.Server.MapPath(string.Format("{0}/{1}/{2}", fotka.BaseFotkaVirtualPath, typeFolder, fotka.NazevSouboru));
            }

            throw new Exception(string.Format("U fotky id: {0} není definice originálu!", fotka.Id));
        }
        public static Fotka CreateFotka(string ownerId, string fullFileName, DateTime uploadDate)
        {
            string photoSystemName;

            try
            {
                photoSystemName = RenameToSystemFileName(fullFileName);
            }
            catch(Exception ex)
            {
                throw;
            }

            var photoToSave = new Fotka {NazevSouboru = photoSystemName, OwnerId = ownerId, DatumUploadu = uploadDate};
            Save(photoToSave);

            return GetFotka(photoToSave.Id);
        }
        public static List<OrigPhotosWaiting> GetWaitingPhotos(User user)
        {
            var retColl = new List<OrigPhotosWaiting>();
            var currentUserDir = user.UserNameSEO;
            var files = GetFilesInUploadDirectory(currentUserDir);
            if(files.Length > 0)
            {
                var uploadTempType = GetBySystemName("minithumb");
                var uploadType = GetBySystemName("upload");

                for(int i=0; i<files.Length; i++)
                {
                    var photoAlreadyInDb = GetFotkaByFileName(files[i].Name);

                    string fileName;
                    string id;
                    if(photoAlreadyInDb == null) //fotku sme jeste nezpracovavali
                    {
                        fileName = RenameToSystemFileName(files[i].FullName);
                        var fotka = new Fotka {NazevSouboru = fileName, User = user, DatumUploadu = files[i].CreationTime};

                        Save(fotka);
                        AddPhotoTypeToPhoto(fotka, uploadType);
                        try
                        {
                            ImageProcessingManager.ResizeImage(fotka, uploadType, uploadTempType);
                            AddPhotoTypeToPhoto(fotka, uploadTempType);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        id = fotka.Id;
                    }
                    else
                    {
                        fileName = photoAlreadyInDb.NazevSouboru;
                        id = photoAlreadyInDb.Id;
                    }

                    var photoWaiting = new OrigPhotosWaiting { FileName = fileName, UploadedDate = files[i].CreationTime, Id = id};

                    //X,Y dimensions of originally uploaded photo - uncomment if you need to diplay it on ProcessUploadPhotos view.
                    /*
                    var origPath = HttpContext.Current.Server.MapPath(string.Format("/{0}/{1}/{2}/{3}", ConfigurationManager.AppSettings["GalerieRootDirVirtualPath"], currentUserDir, uploadType.Adresar, fileName));
                    var origDimensions = ImageProcessingManager.GetImageDimensions(origPath);
                    photoWaiting.X = origDimensions[0];
                    photoWaiting.Y = origDimensions[1];
                    */

                    photoWaiting.ThumbPath = string.Format("/{0}/{1}/{2}/{3}", ConfigurationManager.AppSettings["GalerieRootDirVirtualPath"], currentUserDir, uploadTempType.Adresar, fileName);

                    retColl.Add(photoWaiting);
                }
            }
            return retColl;
        }
 public static bool FotkaTypExist(Fotka fotka, TypFotky typ)
 {
     return fotka.TypyFotek.Any(tf => tf.Id == typ.Id);
 }
 public static void Delete(Fotka fotka)
 {
     DbContext.Current.Delete<Fotka>(d => d.Id == fotka.Id);
 }
 public static void Save(Fotka fotka)
 {
     DbContext.Current.Add(fotka);
 }
 public GalleryPhoto(Fotka photo, int order)
 {
     Id = photo.Id;
     OwnerId = photo.OwnerId;
     NazevSouboru = photo.NazevSouboru;
     DatumUploadu = photo.DatumUploadu;
     DatumFotky = photo.DatumFotky;
     Popis = photo.Popis;
     TypyFotek = photo.TypyFotek;
     Order = order;
 }
        public static void ResizeImage(Fotka fotka, TypFotky fromType, TypFotky targetType, string savePath)
        {
            var thumb = DoResizeImage(fotka, fromType, targetType);

            if(string.IsNullOrEmpty(savePath))
            {
                SaveImage(thumb, fotka, targetType);
            }
            else
            {
                SaveImage(thumb, savePath, fotka.NazevSouboru);
            }
        }
        private static void SaveImage(Bitmap thumbImage, Fotka fotka, TypFotky targetType)
        {
            var targetTypeDirectory = HttpContext.Current.Server.MapPath(string.Format(@"{0}\{1}", fotka.BaseFotkaVirtualPath, targetType.Adresar));

            SaveImage(thumbImage, targetTypeDirectory, fotka.NazevSouboru);
        }
 public static int[] GetImageDimensions(Fotka fotka, TypFotky photoType)
 {
     var fullPath = GetPhotoFullPath(fotka, photoType);
     return GetImageDimensions(fullPath);
 }
        private static Bitmap DoResizeImage(Fotka fotka, TypFotky fromType, TypFotky targetType)
        {
            var fullPath = GetPhotoFullPath(fotka, fromType);
            var origImage = LoadImage(fullPath);

            int resX = targetType.X;
            int resY;
            float ratio;

            //obrazek na sirku nebo ctverec:
            if (origImage.Width >= origImage.Height)
            {
                ratio = (float)origImage.Height / (float)origImage.Width;
                resY = Convert.ToInt32(resX * ratio);
            }
            else //obrazek na vysku.
            {
                if(targetType.Y.HasValue) //mam zadanou vysku
                {
                    resY = targetType.Y.Value;
                    ratio = (float)origImage.Width / (float)origImage.Height;
                    resX = Convert.ToInt32(resY * ratio);
                }
                else //pokud nemam zadanou vysku, musim to resizovat podle zadane sirky
                {
                    ratio = (float)origImage.Height / (float)origImage.Width;
                    resY = Convert.ToInt32(resX * ratio);
                }
            }

            var thumbImage = new Bitmap(resX, resY, origImage.PixelFormat);
            Graphics g = Graphics.FromImage(thumbImage);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;

            var oRectangle = new Rectangle(0, 0, resX, resY);
            g.DrawImage(origImage, oRectangle);

            if (resX < origImage.Width)
            {
                var si = new SharpeningImage();
                si.Filter(thumbImage);
            }

            origImage.Dispose();

            return thumbImage;
        }