Esempio n. 1
0
        private void saveBitmap(Bitmap bmp)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                byte[] array = ms.ToArray();

                using (var log = new OfficeLog())
                {
                    var photo = new Photo
                    {
                        PhotoTime = DateTime.Now,
                        PhotoData = array
                    };
                    log.Database.Log = Console.WriteLine;
                    log.Photos.Add(photo);
                    log.SaveChanges();
                }
            }
        }
Esempio n. 2
0
        private static AjaxResponse UploadPhoto(HttpRequestBase request)
        {
            string userIdStr = null;
            string name = null;
            string albumIdStr = null;
            string sizeStr = null;
            WebImage image = null;
            var methodName = nameof(UploadPhoto);

            try
            {
                userIdStr = request.Cookies["useridcookie"].Value;
                name = request["name"];
                albumIdStr = request["albumid"];
                sizeStr = request["size"];
                image = WebImage.GetImageFromRequest();
            }
            catch (Exception ex)
            {
                return SendError(ex, methodName);
            }

            if (image == null)
            {
                return SendError("Incorrect format of an image", methodName);
            }

            var imageArray = image.GetBytes();
            var mime = "image/" + image.ImageFormat;

            if (imageArray == null || !imageArray.Any() || string.IsNullOrEmpty(mime))
            {
                return SendError("Incorrect format of the image", methodName);
            }

            int albumId = 0, size = 0, userId;

            try
            {
                albumId = Convert.ToInt32(albumIdStr);
                userId = Convert.ToInt32(userIdStr);
                size = Convert.ToInt32(sizeStr);
            }
            catch (Exception ex)
            {
                return SendError(ex, methodName);
            }

            var result = false;
            var photo = new Photo(name, albumId, size, mime, DateTime.Now, userId);

            try
            {
                result = LogicProvider.PhotoLogic.AddPhoto(photo, imageArray);
            }
            catch (Exception ex)
            {
                return SendError(ex, methodName);
            }

            Logger.Log.Info($"User: {userIdStr} uploaded photo: {photo.Id}");

            return new AjaxResponse(null, result);
        }