Esempio n. 1
0
        public void ResizeImage(string OrigFile, string NewFile, int NewWidth, int MaxHeight, bool ResizeIfWider)
        {
            FullSizeImage = Image.FromFile(OrigFile);  // Ensure the generated thumbnail is not being used by rotating it 360 degrees
            FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            if (ResizeIfWider)
            {
                if (FullSizeImage.Width <= NewWidth)
                {
                    NewWidth = FullSizeImage.Width;
                }
            }
            int NewHeight = FullSizeImage.Height * NewWidth / FullSizeImage.Width;

            if (NewHeight > MaxHeight)
            { // Height resize if necessary
                NewWidth  = FullSizeImage.Width * MaxHeight / FullSizeImage.Height;
                NewHeight = MaxHeight;
            }
            // Create the new image with the sizes we've calculated
            NewImage = FullSizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
            FullSizeImage.Dispose();


            jpgEncoder = GetEncoder(ImageFormat.Jpeg);
            // Create an Encoder object based on the GUID for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
            // Create an EncoderParameters object. An EncoderParameters object has an array of EncoderParameter objects. In this case, there is only one EncoderParameter object in the array.
            myEncoderParameters          = new EncoderParameters(1);
            myEncoderParameter           = new EncoderParameter(myEncoder, 50L);
            myEncoderParameters.Param[0] = myEncoderParameter;


            NewImage.Save(NewFile, jpgEncoder, myEncoderParameters);
        }
        private void backgroundWorkerImages_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            IList <FileInfo> FileCollection = e.Argument as IList <FileInfo>;

            foreach (FileInfo File in FileCollection)
            {
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    try
                    {
                        Image image = Image.FromFile(File.FullName);

                        if (image.Height > numericUpDownHeight.Value || image.Width > numericUpDownWidth.Value)
                        {
                            //Keep the ratio aspect
                            decimal ratio = (decimal)image.Height / (decimal)image.Width;
                            Image   NewImage;

                            NewImage = GetResizedImage(image,
                                                       (int)numericUpDownWidth.Value,
                                                       (int)numericUpDownHeight.Value);

                            NewImage.Save(Path.Combine(PathDestinationtextBox.Text,
                                                       Properties.Settings.Default.ResizedPrefix + File.Name));
                            //ImageFormat.Jpeg);
                        }

                        image.Dispose();
                        worker.ReportProgress(1);
                    }

                    catch (Exception ex)
                    {
                        /*MessageBox.Show("Se produjo la siguiente excepción: ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                         * backgroundWorkerImages.CancelAsync();*/
                    }
                }
            }
        }
        public string UploadImage(HttpContext context, string UploadPathTemp, string UploadPath)
        {
            string savedFileName = "";

            bool   IsEncodeFileName = false;
            int    Width            = -1;
            int    Height           = -1;
            bool   isCreateThumb    = false;
            string json             = "";

            foreach (string file in context.Request.Files)
            {
                HttpPostedFile hpf = context.Request.Files[file] as HttpPostedFile;
                if (hpf.ContentLength == 0)
                {
                    continue;
                }
                else
                {
                    IsEncodeFileName = !String.IsNullOrEmpty(context.Request.QueryString["IsEncodeFileName"]) ? Convert.ToBoolean(context.Request.QueryString["IsEncodeFileName"]) : false;
                    savedFileName    = GetFileName(IsEncodeFileName, hpf.FileName);

                    isCreateThumb = !String.IsNullOrEmpty(context.Request.QueryString["isCreateThumb"]) ? Convert.ToBoolean(context.Request.QueryString["isCreateThumb"]) : false;
                    //Upload len thu muc temp
                    hpf.SaveAs(UploadPathTemp + savedFileName);
                    //Upload file



                    //Resize neu can
                    Height = !String.IsNullOrEmpty(context.Request.QueryString["Height"]) ? Convert.ToInt32(context.Request.QueryString["Height"]) : -1;
                    Width  = !String.IsNullOrEmpty(context.Request.QueryString["Width"]) ? Convert.ToInt32(context.Request.QueryString["Width"]) : -1;
                    Image NewImage;
                    if ((Height != -1) && (Width != -1))
                    {
                        NewImage = ResizeImageFromFile(UploadPathTemp + savedFileName, Height, Width, false, false);
                        //save sang thu muc thuc su
                        NewImage.Save(UploadPath + savedFileName);
                    }
                    else if ((Height != -1) || (Width != -1))
                    {
                        NewImage = ResizeImageFromFile(UploadPathTemp + savedFileName, Height, Width, true, true);
                        //save sang thu muc thuc su
                        NewImage.Save(UploadPath + savedFileName);
                    }
                    else
                    {
                        NewImage = Image.FromFile(UploadPathTemp + savedFileName);
                        //save sang thu muc thuc su
                        NewImage.Save(UploadPath + savedFileName);
                    }

                    //=======================================
                    // Tao thumb
                    if (isCreateThumb == true)
                    {
                        NewImage = ResizeImageFromFile(UploadPathTemp + savedFileName, 200, 200, false, true);
                        NewImage.Save(UploadPath + "thumb_" + savedFileName);
                    }

                    NewImage.Dispose();

                    json = json + "{\"Status\":\"Success\",\"File\":\"" + (UploadPath + savedFileName).Replace("\\", "\\\\") + "\",\"Messenger\":\"" + "messsssss" + "\"}";
                }
            }
            return(json);
        }