コード例 #1
0
        public static List <ThumbnailItem> SaveThumbnails(UserPhotoThumbnailSettings thumbnailSettings, Guid userId)
        {
            if (thumbnailSettings.Size.IsEmpty)
            {
                return(null);
            }

            var thumbnailsData = new ThumbnailsData(userId);

            var resultBitmaps = new List <ThumbnailItem>();

            var img = thumbnailsData.MainImgBitmap;

            if (img == null)
            {
                return(null);
            }

            foreach (var thumbnail in thumbnailsData.ThumbnailList)
            {
                thumbnail.Bitmap = GetBitmap(img, thumbnail.Size, thumbnailSettings);

                resultBitmaps.Add(thumbnail);
            }

            thumbnailsData.Save(resultBitmaps);

            thumbnailSettings.SaveForUser(userId);

            return(thumbnailsData.ThumbnailList);
        }
コード例 #2
0
        public static Bitmap GetBitmap(Image mainImg, Size size, UserPhotoThumbnailSettings thumbnailSettings)
        {
            var thumbnailBitmap = new Bitmap(size.Width, size.Height);

            var scaleX = size.Width / (1.0 * thumbnailSettings.Size.Width);
            var scaleY = size.Height / (1.0 * thumbnailSettings.Size.Height);

            var rect = new Rectangle(-(int)(scaleX * (1.0 * thumbnailSettings.Point.X)),
                                     -(int)(scaleY * (1.0 * thumbnailSettings.Point.Y)),
                                     (int)(scaleX * mainImg.Width),
                                     (int)(scaleY * mainImg.Height));

            using (var graphic = Graphics.FromImage(thumbnailBitmap))
            {
                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphic.PixelOffsetMode   = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphic.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphic.DrawImage(mainImg, rect);

                using var wrapMode = new System.Drawing.Imaging.ImageAttributes();
                wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
                graphic.DrawImage(mainImg, rect, 0, 0, mainImg.Width, mainImg.Height, GraphicsUnit.Pixel, wrapMode);
            }

            return(thumbnailBitmap);
        }
コード例 #3
0
 public ResizeWorkerItem(Guid userId, byte[] data, long maxFileSize, Size size, IDataStore dataStore, UserPhotoThumbnailSettings settings)
 {
     UserId      = userId;
     Data        = data;
     MaxFileSize = maxFileSize;
     Size        = size;
     DataStore   = dataStore;
     Settings    = settings;
 }
コード例 #4
0
 public ResizeWorkerItem(int tenantId, Guid userId, byte[] data, long maxFileSize, Size size, IDataStore dataStore, UserPhotoThumbnailSettings settings)
 {
     _tenantId    = tenantId;
     _userId      = userId;
     _data        = data;
     _maxFileSize = maxFileSize;
     _size        = size;
     _dataStore   = dataStore;
     _settings    = settings;
 }
コード例 #5
0
        private static void SetUserPhotoThumbnailSettings(Guid userId, int width, int height)
        {
            var settings = UserPhotoThumbnailSettings.LoadForUser(userId);

            if (!settings.IsDefault)
            {
                return;
            }

            var max = Math.Max(Math.Max(width, height), SmallFotoSize.Width);
            var min = Math.Max(Math.Min(width, height), SmallFotoSize.Width);

            var pos = (max - min) / 2;

            settings = new UserPhotoThumbnailSettings(
                width >= height ? new Point(pos, 0) : new Point(0, pos),
                new Size(min, min));

            settings.SaveForUser(userId);
        }
コード例 #6
0
        private static string SizePhoto(Guid userID, byte[] data, long maxFileSize, Size size, bool now)
        {
            if (data == null || data.Length <= 0)
            {
                throw new UnknownImageFormatException();
            }
            if (maxFileSize != -1 && data.Length > maxFileSize)
            {
                throw new ImageWeightLimitException();
            }

            var resizeTask = new ResizeWorkerItem(userID, data, maxFileSize, size, GetDataStore(), UserPhotoThumbnailSettings.LoadForUser(userID));

            if (now)
            {
                //Resize synchronously
                ResizeImage(resizeTask);
                return(GetSizedPhotoAbsoluteWebPath(userID, size));
            }
            else
            {
                if (!ResizeQueue.GetItems().Contains(resizeTask))
                {
                    //Add
                    ResizeQueue.Add(resizeTask);
                    if (!ResizeQueue.IsStarted)
                    {
                        ResizeQueue.Start(ResizeImage);
                    }
                }
                return(GetDefaultPhotoAbsoluteWebPath(size));
                //NOTE: return default photo here. Since task will update cache
            }
        }
コード例 #7
0
        public static void ResetThumbnailSettings(Guid userId)
        {
            var thumbSettings = new UserPhotoThumbnailSettings().GetDefault() as UserPhotoThumbnailSettings;

            thumbSettings.SaveForUser(userId);
        }