public static string SaveThumbnail(Guid userID, Image img, ImageFormat format)
        {
            if (TryUpdateThumbnail(userID))
            {
                return(TryGetFromThumbnail(userID, img.Width, img.Height));
            }

            var  moduleID     = Guid.Empty;
            var  widening     = CommonPhotoManager.GetImgFormatName(format);
            Size size         = img.Size;
            var  trueFileName = string.Format("{0}{1}_size_{2}-{3}.{4}", (moduleID == Guid.Empty ? "" : moduleID.ToString()), userID, img.Width, img.Height, widening);

            var store    = GetDataStore();
            var photoUrl = string.Empty;

            using (var s = new MemoryStream(CommonPhotoManager.SaveToBytes(img)))
            {
                img.Dispose();
                photoUrl = store.Save(trueFileName, s).ToString();
            }
            var fileName = Path.GetFileName(photoUrl);

            AddToCache(userID, size, fileName);
            return(photoUrl);
        }
Beispiel #2
0
        private static string SaveOrUpdatePhoto(Guid userID, byte[] data, long maxFileSize, Size size, bool saveInCoreContext, out string fileName)
        {
            ImageFormat imgFormat;
            int         width;
            int         height;

            data = TryParseImage(data, maxFileSize, size, out imgFormat, out width, out height);

            var widening = CommonPhotoManager.GetImgFormatName(imgFormat);

            fileName = string.Format("{0}_orig_{1}-{2}.{3}", userID, width, height, widening);

            if (saveInCoreContext)
            {
                CoreContext.UserManager.SaveUserPhoto(userID, data);
                ClearCache(userID);
            }

            var store = GetDataStore();

            var photoUrl = GetDefaultPhotoAbsoluteWebPath();

            if (data != null && data.Length > 0)
            {
                using (var stream = new MemoryStream(data))
                {
                    photoUrl = store.Save(fileName, stream).ToString();
                }
                //Queue resizing
                SizePhoto(userID, data, -1, SmallFotoSize, true);
                SizePhoto(userID, data, -1, MediumFotoSize, true);
                SizePhoto(userID, data, -1, BigFotoSize, true);
            }
            return(photoUrl);
        }
        public static string SaveTempPhoto(byte[] data, long maxFileSize, int maxWidth, int maxHeight)
        {
            ImageFormat imgFormat;
            int         width;
            int         height;

            data = TryParseImage(data, maxFileSize, new Size(maxWidth, maxHeight), out imgFormat, out width, out height);
            string fileName = Guid.NewGuid().ToString() + "." + CommonPhotoManager.GetImgFormatName(imgFormat);

            var store = GetDataStore();

            using (var stream = new MemoryStream(data))
            {
                return(store.Save(_tempDomainName, fileName, stream).ToString());
            }
        }
        private static void ResizeImage(ResizeWorkerItem item)
        {
            try
            {
                CoreContext.TenantManager.SetCurrentTenant(item.TenantId);

                var data = item.Data;
                using (var stream = new MemoryStream(data))
                    using (var img = Image.FromStream(stream))
                    {
                        var imgFormat = img.RawFormat;
                        if (item.Size != img.Size)
                        {
                            using (var img2 = item.Settings.IsDefault ?
                                              CommonPhotoManager.DoThumbnail(img, item.Size, true, true, true) :
                                              UserPhotoThumbnailManager.GetBitmap(img, item.Size, item.Settings))
                            {
                                data = CommonPhotoManager.SaveToBytes(img2);
                            }
                        }
                        else
                        {
                            data = CommonPhotoManager.SaveToBytes(img);
                        }

                        var widening = CommonPhotoManager.GetImgFormatName(imgFormat);
                        var fileName = string.Format("{0}_size_{1}-{2}.{3}", item.UserId, item.Size.Width, item.Size.Height, widening);

                        using (var stream2 = new MemoryStream(data))
                        {
                            item.DataStore.Save(fileName, stream2).ToString();

                            AddToCache(item.UserId, item.Size, fileName);
                        }
                    }
            }
            catch (ArgumentException error)
            {
                throw new UnknownImageFormatException(error);
            }
        }
        private static void ResizeImage(ResizeWorkerItem item)
        {
            try
            {
                var data = item.Data;
                using (var stream = new MemoryStream(data))
                    using (var img = Image.FromStream(stream))
                    {
                        var imgFormat = img.RawFormat;
                        if (item.Size != img.Size)
                        {
                            using (var img2 = CommonPhotoManager.DoThumbnail(img, item.Size, true, true, true))
                            {
                                data = CommonPhotoManager.SaveToBytes(img2);
                            }
                        }
                        else
                        {
                            data = CommonPhotoManager.SaveToBytes(img);
                        }

                        var widening     = CommonPhotoManager.GetImgFormatName(imgFormat);
                        var trueFileName = string.Format("{0}{1}_size_{2}-{3}.{4}", (item.ModuleId == Guid.Empty ? "" : item.ModuleId.ToString()), item.UserId, item.Size.Width, item.Size.Height, widening);

                        using (var stream2 = new MemoryStream(data))
                        {
                            var photoUrl = item.DataStore.Save(trueFileName, stream2).ToString();
                            //NOTE: Update cache here
                            var fileName = Path.GetFileName(photoUrl);

                            AddToCache(item.UserId, item.Size, fileName, true);
                        }
                    }
            }
            catch (ArgumentException error)
            {
                throw new UnknownImageFormatException(error);
            }
        }
        private static string SaveOrUpdatePhoto(Guid moduleID, Guid userID, byte[] data, long maxFileSize, Size size, bool saveInCoreContext)
        {
            ImageFormat imgFormat;
            int         width;
            int         height;

            data = TryParseImage(data, maxFileSize, size, out imgFormat, out width, out height);

            var widening     = CommonPhotoManager.GetImgFormatName(imgFormat);
            var trueFileName = string.Format("{0}{1}_orig_{2}-{3}.{4}", (moduleID == Guid.Empty ? "" : moduleID.ToString()), userID, width, height, widening);

            if (saveInCoreContext)
            {
                CoreContext.UserManager.SaveUserPhoto(userID, moduleID, data);
                ClearCache(moduleID, userID);
            }
            if (TryUpdateThumbnail(userID)) //If we have thumb - no need to load
            {
                return(TryGetFromThumbnail(userID, size.Width, size.Height));
            }


            var store = GetDataStore();

            var photoUrl = GetDefaultPhotoAbsoluteWebPath();

            if (data != null && data.Length > 0)
            {
                using (var stream = new MemoryStream(data))
                {
                    photoUrl = store.Save(trueFileName, stream).ToString();
                }
                //Queue resizing
                SizePhoto(moduleID, userID, data, -1, SmallFotoSize, true);
                SizePhoto(moduleID, userID, data, -1, MediumFotoSize, true);
                SizePhoto(moduleID, userID, data, -1, BigFotoSize, true);
            }
            return(photoUrl);
        }
        public static string GetSizedTempPhotoAbsoluteWebPath(string fileName, int newWidth, int newHeight)
        {
            var store = GetDataStore();

            if (store.IsFile(_tempDomainName, fileName))
            {
                using (var s = store.GetReadStream(_tempDomainName, fileName))
                    using (var img = Image.FromStream(s))
                    {
                        var    imgFormat = img.RawFormat;
                        byte[] data;

                        if (img.Width != newWidth || img.Height != newHeight)
                        {
                            using (var img2 = CommonPhotoManager.DoThumbnail(img, new Size(newWidth, newHeight), true, true, true))
                            {
                                data = CommonPhotoManager.SaveToBytes(img2);
                            }
                        }
                        else
                        {
                            data = CommonPhotoManager.SaveToBytes(img);
                        }
                        var widening           = CommonPhotoManager.GetImgFormatName(imgFormat);
                        var index              = fileName.LastIndexOf('.');
                        var fileNameWithoutExt = (index != -1) ? fileName.Substring(0, index) : fileName;

                        var trueFileName = fileNameWithoutExt + "_size_" + newWidth.ToString() + "-" + newHeight.ToString() + "." + widening;
                        using (var stream = new MemoryStream(data))
                        {
                            return(store.Save(_tempDomainName, trueFileName, stream).ToString());
                        }
                    }
            }
            return(GetDefaultPhotoAbsoluteWebPath(new Size(newWidth, newHeight)));
        }