コード例 #1
0
ファイル: ImageHelper.cs プロジェクト: wes-cutting/Sandbox-V2
        public static Photo ProcessPhoto(int buildingId, byte[] imageBytes, ImageFormat format, string extension,
			BuildingFacade buildingFacade, PhotoFacade photoFacade)
        {
            var photoId = Guid.NewGuid();

            //resolutions
            var vectors = new List<Vector2>();
            vectors.Add(new Vector2(800, 600));
            vectors.Add(new Vector2(600, 395));
            vectors.Add(new Vector2(280, 190));
            vectors.Add(new Vector2(200, 150));
            vectors.Add(new Vector2(115, 85));
            vectors.Add(new Vector2(50, 50));

            //load image
            using(var stream = new MemoryStream(imageBytes))
            {
                stream.Position = 0;
                using(Image image = Image.FromStream(stream))
                {
                    foreach(var v in vectors)
                    {
                        using(Image resized = ImageScaler.Crop(image, v.X, v.Y,
                                  ImageScaler.AnchorPosition.Center))
                        {
                            string fileName = string.Format("{0}-{1}x{2}{3}",
                                                photoId, v.X, v.Y, extension);

                            //UploadPhoto would go here
                            SavePhoto(buildingId, resized, fileName, format);
                        }
                    }
                }
            }

            //create the db reference for the images.
            var photo = new Photo();
            photo.PhotoId = photoId;
            photo.BuildingId = buildingId;
            photo.Extension = extension.Replace(".", string.Empty);

            if(photoFacade.GetPhotos(buildingId).Count(p => p.IsPrimary) == 0)
                photo.IsPrimary = true;

            if(photo.IsPrimary)
            {
                var building = buildingFacade.GetBuilding(buildingId);
                building.PrimaryPhotoId = photo.PhotoId;
                building.PrimaryPhotoExtension = photo.Extension;
                buildingFacade.Save();
            }

            photoFacade.AddPhoto(buildingId, photo);
            photoFacade.Save();
            return photo;
        }
コード例 #2
0
        private void UploadPhotoToAzure(Stream file, Photo photo, int width, int height)
        {
            // create a resized image
            using (Image resized = ImageScaler.Crop(
                Image.FromStream(file),
                width, height, ImageScaler.AnchorPosition.Center))
            {
                using (MemoryStream upload = new MemoryStream())
                {
                    // encode and save that image to a memory stream
                    ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
                    var encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                    resized.Save(upload, info[1], encoderParams);

                    // reset the image
                    upload.Position = 0;

                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Photos EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPhotos(Photo photo)
 {
     base.AddObject("Photos", photo);
 }
コード例 #4
0
        public Status<Photo[]> UploadPhotos(string username, long buildingId, UploadFileStream[] files)
        {
            using (var context = new RentlerContext())
            {
                try
                {
                    // get the building
                    var building = (from b in context.Buildings
                                    where b.User.Username == username &&
                                    b.BuildingId == buildingId
                                    select b).SingleOrDefault();

                    if (building == null)
                        return Status.NotFound<Photo[]>();

                    int nextPosition = building.Photos.Count;

                    List<Photo> final = new List<Photo>();

                    foreach (var upload in files)
                    {
                        if (upload == null) continue;

                        Photo photo = new Photo();
                        photo.Extension = Path.GetExtension(upload.FileName).ToLower();
                        photo.BuildingId = building.BuildingId;
                        photo.CreateDateUtc = DateTime.UtcNow;
                        photo.CreatedBy = "photoadapter";
                        photo.UpdateDateUtc = DateTime.UtcNow;
                        photo.UpdatedBy = "photoadapter";
                        photo.PhotoId = Guid.NewGuid();
                        photo.SortOrder = nextPosition;

                        if (!Rentler.Configuration.Photos.Current.SupportedExtensions.Contains(photo.Extension))
                            continue;

                        UploadPhotoToAzure(upload.Stream, photo, 800, 600);
                        UploadPhotoToAzure(upload.Stream, photo, 600, 395);
                        UploadPhotoToAzure(upload.Stream, photo, 280, 190);
                        UploadPhotoToAzure(upload.Stream, photo, 200, 150);
                        UploadPhotoToAzure(upload.Stream, photo, 115, 85);
                        UploadPhotoToAzure(upload.Stream, photo, 50, 50);

                        building.Photos.Add(photo);

                        // see if it's the first photo uploaded
                        if (nextPosition == 0)
                        {
                            building.PrimaryPhotoId = photo.PhotoId;
                            building.PrimaryPhotoExtension = photo.Extension;
                        }

                        context.SaveChanges();

                        photo.Building = null;
                        final.Add(photo);

                        nextPosition++;
                    }

                    InvalidateCache(buildingId);

                    return Status.OK<Photo[]>(final.ToArray());
                }
                catch (Exception ex)
                {
                    return Status.Error<Photo[]>(ex.Message, null);
                }
            }
        }
コード例 #5
0
 /// <summary>
 /// Create a new Photo object.
 /// </summary>
 /// <param name="photoId">Initial value of the PhotoId property.</param>
 /// <param name="buildingId">Initial value of the BuildingId property.</param>
 /// <param name="isPrimary">Initial value of the IsPrimary property.</param>
 /// <param name="sortOrder">Initial value of the SortOrder property.</param>
 /// <param name="extension">Initial value of the Extension property.</param>
 /// <param name="createDateUtc">Initial value of the CreateDateUtc property.</param>
 /// <param name="createdBy">Initial value of the CreatedBy property.</param>
 /// <param name="updateDateUtc">Initial value of the UpdateDateUtc property.</param>
 /// <param name="updatedBy">Initial value of the UpdatedBy property.</param>
 public static Photo CreatePhoto(global::System.Guid photoId, global::System.Int64 buildingId, global::System.Boolean isPrimary, global::System.Int32 sortOrder, global::System.String extension, global::System.DateTime createDateUtc, global::System.String createdBy, global::System.DateTime updateDateUtc, global::System.String updatedBy)
 {
     Photo photo = new Photo();
     photo.PhotoId = photoId;
     photo.BuildingId = buildingId;
     photo.IsPrimary = isPrimary;
     photo.SortOrder = sortOrder;
     photo.Extension = extension;
     photo.CreateDateUtc = createDateUtc;
     photo.CreatedBy = createdBy;
     photo.UpdateDateUtc = updateDateUtc;
     photo.UpdatedBy = updatedBy;
     return photo;
 }