Esempio n. 1
0
        /// <summary>
        /// generates a thumbnail given the path of the original images
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="suffix"></param>
        /// <param name="desiredWidth"></param>
        /// <param name="desiredHeight"></param>
        /// <returns></returns>
        private static string thumbnail(string filePath, string suffix, float desiredWidth, float desiredHeight, Imgsize type)
        {
            string thumb = filePath;
            string file  = filePath;
            string ext   = thumb.Substring(thumb.LastIndexOf(".") + 1);

            thumb = thumb.Substring(0, thumb.LastIndexOf(".")) + "_" + suffix + "." + ext;
#if AZURE
            file  = file.Substring(file.LastIndexOf("/") + 1);
            thumb = thumb.Substring(thumb.LastIndexOf("/") + 1);
            bool exists = photoContainer.DoesBlobItemExists(file);
#else
            bool exists = File.Exists(GeneralConstants.APP_ROOT_DIR + file);
#endif
            if (!exists)
            {
                // delete from db
                new Thread(() =>
                {
                    Syslog.Write(String.Concat("Cannot find file: ", GeneralConstants.APP_ROOT_DIR + file));
                    using (var db = new tradelrDataContext())
                    {
                        var repository = new TradelrRepository(db);
                        repository.SetIsolationToNoLock();
                        repository.DeleteImage(file);
                    }
                }).Start();

                return("");
            }
            // These are the ratio calculations
#if AZURE
            Image img = Image.FromStream(photoContainer.GetBlobContentStream(file));
#else
            Image img = Image.FromFile(GeneralConstants.APP_ROOT_DIR + file);
#endif
            int width  = img.Width;
            int height = img.Height;

            img.Dispose();

            float factor = 0;
            if (width > 0 && height > 0)
            {
                float wfactor = desiredWidth / width;
                float hfactor = desiredHeight / height;
                factor = wfactor < hfactor ? wfactor : hfactor;
            }
            if (factor > 0)
            {
                int twidth  = Convert.ToInt32(Math.Floor(factor * width));
                int theight = Convert.ToInt32(Math.Floor(factor * height));
                convert(file, thumb, twidth, theight, type);
            }
            else
            {
#if AZURE
                photoContainer.DeleteBlobItem(thumb);
                photoContainer.CopyContent(file, thumb);
#else
                if (File.Exists(GeneralConstants.APP_ROOT_DIR + thumb))
                {
                    File.Delete(GeneralConstants.APP_ROOT_DIR + thumb);
                }
                File.Copy(GeneralConstants.APP_ROOT_DIR + file, GeneralConstants.APP_ROOT_DIR + thumb);
#endif
            }
            return(thumb);
        }