Esempio n. 1
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(PathPhoto))
            {
                MessageBox.Show("Choose photo.");
            }

            string imageName = Guid.NewGuid().ToString() + ".jpg"; //08f71055-72b0-4075-bbc9-8472782e3b7f.jpg

            System.Drawing.Image img = System.Drawing.Image.FromFile(PathPhoto);
            img = CompressImage.CreateImage((Bitmap)img, 500, 500);
            img.Save(Environment.CurrentDirectory + "//" + imageName, ImageFormat.Jpeg);


            User user = new User()
            {
                FirstName   = tbFirstName.Text,
                LastName    = tbLastName.Text,
                DateOfBirth = dpDateOfBirth.SelectedDate ?? DateTime.Now,
                Phone       = tbPhone.Text,
                Image       = imageName,
                RoleId      = ((Role)cbRole.SelectedItem).Id
            };

            using (EFContext context = new EFContext())
            {
                context.Users.Add(user);
                context.SaveChanges();
            }

            this.Close();
        }
Esempio n. 2
0
        public Optimize(CompressImage CompressImage)
        {
            compressImage = CompressImage;

            if (compressImage.DestinationFileFullPath != null)
            {
                File.Delete(compressImage.DestinationFileFullPath);
            }

            if (vgyClient.BaseAddress == null)
            {
                vgyClient.BaseAddress = new Uri("https://vgy.me/");
                vgyClient.DefaultRequestHeaders.Accept.Clear();
                vgyClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            }
        }
Esempio n. 3
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.ShowDialog();

            string imageName = Guid.NewGuid().ToString() + ".jpg"; //08f71055-72b0-4075-bbc9-8472782e3b7f.jpg

            System.Drawing.Image img = System.Drawing.Image.FromFile(dlg.FileName);
            img = CompressImage.CreateImage((Bitmap)img, 500, 500);
            img.Save(Environment.CurrentDirectory + "//" + imageName, ImageFormat.Jpeg);

            Data.Models.User_Photo user_Photo = new User_Photo();
            {
                user_Photo.Name    = imageName;
                user_Photo.User_Id = StartWindow.selectID;
            }

            PhotoService photoService = new PhotoService();

            photoService.Add(user_Photo);
        }
Esempio n. 4
0
        public async Task <ActionResult> UploadFiles(UploadFileModel model)
        {
            String fileName, fileLoc, attachedFileLoc;

            if (model?.File != null)
            {             //File
                String fileDir = Server.MapPath($"~/Content/Laboratory/{model.Course}");
                if (!Directory.Exists(fileDir))
                {
                    Directory.CreateDirectory(fileDir);
                }

                fileName = "image_" + Guid.NewGuid().ToString() + $"_{model.File.FileName}";
                fileLoc  = Server.MapPath($"~/Content/Laboratory/{model.Course}/") + fileName;
                if (model.File.ContentLength > 0 && model.File.ContentLength < 200000000)                 // 200mb maximum
                {
                    model.File.SaveAs(fileLoc);
                }

                if (model.FileType == 4 && model.AttachedFile != null)
                {                 //Excel
                    String attachedFileName = "excel_" + Guid.NewGuid().ToString() + $"_{model.AttachedFile.FileName}";
                    attachedFileLoc = Server.MapPath($"~/Content/Laboratory/{model.Course}/") + attachedFileName;
                    if (model.AttachedFile.ContentLength > 0 && model.AttachedFile.ContentLength < 200000000)                     // 200mb maximum
                    {
                        model.AttachedFile.SaveAs(attachedFileLoc);
                    }
                    await db.CreateLaboratoryFile(model, fileLoc, attachedFileLoc);
                }
                else if (model.FileType == 5)
                {
                    new Thread(async() =>
                    {                     //new thread for image compress
                        using (Image image = Image.FromFile(fileLoc))
                        {
                            if (image.Width > 1080)
                            {
                                using (CompressImage compress = new CompressImage(new Bitmap(image), image.Width, 1080))
                                {                                  // image compress
                                    compress.OnCompressing   += (e) => { HttpContext.Cache["UploadPercent"] = e.CompressingPercent; };
                                    compress.OnCompressStart += () => { HttpContext.Cache["UploadPercent"] = 0; };
                                    compress.OnCompressEnd   += () => { HttpContext.Cache["UploadPercent"] = 100; };
                                    compress.Resize();

                                    String attachedFileName = "small_" + Guid.NewGuid().ToString() + $"_{model.File.FileName}";
                                    attachedFileLoc         = Server.MapPath($"~/Content/Laboratory/{model.Course}/") + attachedFileName;
                                    compress.OutputImage.Save(attachedFileLoc);
                                    await db.CreateLaboratoryFile(model, fileLoc, attachedFileLoc);
                                }
                            }
                            else
                            {
                                HttpContext.Cache["UploadPercent"] = 100;
                                await Task.Delay(1000);
                                await db.CreateLaboratoryFile(model, fileLoc, fileLoc);
                            }
                        }
                    }).Start();
                }

                Session["Message"] = "Файл успешно добавлен";
            }
            return(await GetCourseTopicFile(model.CourseId, true));
        }