Пример #1
0
        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);
        }
        private static String ExecResizeImage(byte[] imageData, Size fotoSize, IDataStore dataStore, String photoPath)
        {
            var data = imageData;

            using (var stream = new MemoryStream(data))
                using (var img = new Bitmap(stream))
                {
                    var imgFormat = img.RawFormat;
                    if (fotoSize != img.Size)
                    {
                        using (var img2 = CommonPhotoManager.DoThumbnail(img, fotoSize, false, false, false))
                        {
                            data = CommonPhotoManager.SaveToBytes(img2, Global.GetImgFormatName(imgFormat));
                        }
                    }
                    else
                    {
                        data = Global.SaveToBytes(img);
                    }

                    using (var fileStream = new MemoryStream(data))
                    {
                        var photoUri = dataStore.Save(photoPath, fileStream).ToString();
                        photoUri = String.Format("{0}?cd={1}", photoUri, DateTime.UtcNow.Ticks);
                        return(photoUri);
                    }
                }
        }
Пример #3
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);
        }
Пример #4
0
        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());
            }
        }
Пример #5
0
        private static void ResizeLogo(WhiteLabelLogoTypeEnum type, String fileName, byte[] data, long maxFileSize, Size size, IDataStore store)
        {
            //Resize synchronously
            if (data == null || data.Length <= 0)
            {
                throw new UnknownImageFormatException();
            }
            if (maxFileSize != -1 && data.Length > maxFileSize)
            {
                throw new ImageWeightLimitException();
            }

            try
            {
                using (var stream = new MemoryStream(data))
                    using (var img = Image.FromStream(stream))
                    {
                        var imgFormat = img.RawFormat;
                        if (size != img.Size)
                        {
                            using (var img2 = CommonPhotoManager.DoThumbnail(img, size, false, true, false))
                            {
                                data = CommonPhotoManager.SaveToBytes(img2);
                            }
                        }
                        else
                        {
                            data = CommonPhotoManager.SaveToBytes(img);
                        }

                        //fileExt = CommonPhotoManager.GetImgFormatName(imgFormat);

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

                            //AddToCache(item.UserId, item.Size, fileNPath, true);
                        }
                    }
            }
            catch (ArgumentException error)
            {
                throw new UnknownImageFormatException(error);
            }
        }
Пример #6
0
        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 ExecResizeImage(ResizeWorkerItem resizeWorkerItem)
        {
            foreach (var fotoSize in resizeWorkerItem.RequireFotoSize)
            {
                var data = resizeWorkerItem.ImageData;
                using (var stream = new MemoryStream(data))
                    using (var img = new Bitmap(stream))
                    {
                        var imgFormat = img.RawFormat;
                        if (fotoSize != img.Size)
                        {
                            using (var img2 = CommonPhotoManager.DoThumbnail(img, fotoSize, false, false, false))
                            {
                                data = CommonPhotoManager.SaveToBytes(img2, Global.GetImgFormatName(imgFormat));
                            }
                        }
                        else
                        {
                            data = Global.SaveToBytes(img);
                        }

                        var fileExtension = String.Concat("." + Global.GetImgFormatName(imgFormat));

                        var photoPath = !resizeWorkerItem.UploadOnly
                                        ? BuildFilePath(resizeWorkerItem.ContactID, fotoSize, fileExtension)
                                        : BuildFileTmpPath(resizeWorkerItem.ContactID, fotoSize, fileExtension, resizeWorkerItem.TmpDirName);

                        using (var fileStream = new MemoryStream(data))
                        {
                            var photoUri = resizeWorkerItem.DataStore.Save(photoPath, fileStream).ToString();
                            photoUri = String.Format("{0}?cd={1}", photoUri, DateTime.UtcNow.Ticks);

                            if (!resizeWorkerItem.UploadOnly)
                            {
                                ToCache(resizeWorkerItem.ContactID, photoUri, fotoSize);
                            }
                        }
                    }
            }
        }
Пример #8
0
        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);
            }
        }
Пример #9
0
        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);
        }
Пример #10
0
        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)));
        }
Пример #11
0
 public static byte[] SaveToBytes(Image img)
 {
     return(CommonPhotoManager.SaveToBytes(img, GetImgFormatName(img.RawFormat)));
 }
Пример #12
0
        private static byte[] TryParseImage(byte[] data, long maxFileSize, Size maxsize, out ImageFormat imgFormat, out int width, out int height)
        {
            if (data == null || data.Length <= 0)
            {
                throw new UnknownImageFormatException();
            }
            if (maxFileSize != -1 && data.Length > maxFileSize)
            {
                throw new ImageSizeLimitException();
            }

            data = ImageHelper.RotateImageByExifOrientationData(data);

            try
            {
                using (var stream = new MemoryStream(data))
                    using (var img = new Bitmap(stream))
                    {
                        imgFormat = img.RawFormat;
                        width     = img.Width;
                        height    = img.Height;
                        var maxWidth  = maxsize.Width;
                        var maxHeight = maxsize.Height;

                        if ((maxHeight != -1 && img.Height > maxHeight) || (maxWidth != -1 && img.Width > maxWidth))
                        {
                            #region calulate height and width

                            if (width > maxWidth && height > maxHeight)
                            {
                                if (width > height)
                                {
                                    height = (int)((double)height * (double)maxWidth / (double)width + 0.5);
                                    width  = maxWidth;
                                }
                                else
                                {
                                    width  = (int)((double)width * (double)maxHeight / (double)height + 0.5);
                                    height = maxHeight;
                                }
                            }

                            if (width > maxWidth && height <= maxHeight)
                            {
                                height = (int)((double)height * (double)maxWidth / (double)width + 0.5);
                                width  = maxWidth;
                            }

                            if (width <= maxWidth && height > maxHeight)
                            {
                                width  = (int)((double)width * (double)maxHeight / (double)height + 0.5);
                                height = maxHeight;
                            }

                            #endregion

                            using (var b = new Bitmap(width, height))
                                using (var gTemp = Graphics.FromImage(b))
                                {
                                    gTemp.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    gTemp.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                                    gTemp.SmoothingMode     = SmoothingMode.HighQuality;
                                    gTemp.DrawImage(img, 0, 0, width, height);

                                    data = CommonPhotoManager.SaveToBytes(b);
                                }
                        }
                        return(data);
                    }
            }
            catch (OutOfMemoryException)
            {
                throw new ImageSizeLimitException();
            }
            catch (ArgumentException error)
            {
                throw new UnknownImageFormatException(error);
            }
        }