Пример #1
0
        public ActionResult SaveSimpleImage()
        {
            ApplicationUser user = null;
            string          id   = User.Identity.GetUserId();

            if (id != string.Empty && id != null)
            {
                user = context.Users.Where(a => a.Id == id).First();
            }

            var img = IdenticonImage.GenerateSimpleImage(user.Email);

            img = ResizeImage(img, 500, 500);
            var filename = Guid.NewGuid() + ".jpg"; // Unikalny identyfikator + rozszerzenie
                                                    //W jakim folderze ma byc umiesczony dany plik oraz jego nazwa! Oraz zapis
            var path = Path.Combine(Server.MapPath(AppConfig.ImagesUserFolder), filename);

            img.Save(path);
            user.MainPicture = filename;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bitmapData = ms.ToArray();
            return(File(bitmapData, "image/jpeg"));
        }
Пример #2
0
        private void SaveNormalImage()
        {
            ApplicationUser user = null;
            string          id   = User.Identity.GetUserId();

            if (id != string.Empty && id != null)
            {
                user = context.Users.Where(a => a.Id == id).First();
            }

            var img = IdenticonImage.GenerateNormalImage(user.Email);

            img = ResizeImage(img, 500, 500);
            var filename   = Guid.NewGuid() + ".jpg"; // Unikalny identyfikator + rozszerzenie
            var currentImg = user.MainPicture;

            var path         = Path.Combine(Server.MapPath(AppConfig.ImagesUserFolder), filename);
            var pathToDelete = Path.Combine(Server.MapPath(AppConfig.ImagesUserFolder), currentImg);

            System.IO.File.Delete(pathToDelete);

            img.Save(path);
            user.MainPicture = filename;
            context.SaveChanges();
        }
Пример #3
0
        public ActionResult NormalImage()
        {
            string email = User.Identity.Name;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            IdenticonImage.GenerateNormalImage(email).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bitmapData = ms.ToArray();
            return(File(bitmapData, "image/jpeg"));
        }
Пример #4
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            HttpPostedFileBase file = model.ImageFile;

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, UserInformation = new UserInformation(model.Email), Nickname = model.NickName
                };
                if (file != null && file.ContentLength > 0 && model.IsPersonOnImage == "yes")
                {
                    if (Path.GetExtension(file.FileName).ToLower() == ".jpg" || Path.GetExtension(file.FileName).ToLower() == ".png")
                    {
                        var sourceImage = Image.FromStream(file.InputStream);

                        sourceImage = ResizeImage(sourceImage, 500, 500);

                        //Generowanie plik
                        var fileExt  = Path.GetExtension(file.FileName);
                        var filename = Guid.NewGuid() + fileExt; // Unikalny identyfikator + rozszerzenie
                        //W jakim folderze ma byc umiesczony dany plik oraz jego nazwa! Oraz zapis
                        var path = Path.Combine(Server.MapPath(AppConfig.ImagesUserFolder), filename);
                        //file.SaveAs(path);
                        sourceImage.Save(path);
                        user.MainPicture = filename;
                    }
                }
                else
                {
                    Random rand = new Random();
                    Image  img;

                    while (true)
                    {
                        //Generate custom image
                        if (rand.NextDouble() <= 0.33)
                        {
                            img = IdenticonImage.GenerateCustomImage(model.Email);
                            break;
                        }

                        //Generate normal image
                        if (rand.NextDouble() <= 0.33)
                        {
                            img = IdenticonImage.GenerateNormalImage(model.Email);
                            break;
                        }
                        //Generate simple image
                        if (rand.NextDouble() <= 0.33)
                        {
                            img = IdenticonImage.GenerateSimpleImage(model.Email);
                            break;
                        }
                    }
                    img = ResizeImage(img, 500, 500);
                    var filename = Guid.NewGuid() + ".jpg"; // Unikalny identyfikator + rozszerzenie
                    //W jakim folderze ma byc umiesczony dany plik oraz jego nazwa! Oraz zapis
                    var path = Path.Combine(Server.MapPath(AppConfig.ImagesUserFolder), filename);
                    img.Save(path);
                    user.MainPicture = filename;
                }
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }