Esempio n. 1
0
        public Image ResizeImage(Image imgToResize, int size, EnumC.Dimensions dimension, string dest)
        {
            int sourceWidth  = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent  = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            switch (dimension)
            {
            case EnumC.Dimensions.Width:
                nPercent = (size / (float)sourceWidth);
                break;

            default:
                nPercent = ((float)size / sourceHeight);
                break;
            }


            var destWidth  = (int)(sourceWidth * nPercent);
            var destHeight = (int)(sourceHeight * nPercent);

            var b = new Bitmap(destWidth, destHeight);
            var g = Graphics.FromImage(b);

            //g.SmoothingMode = SmoothingMode.HighQuality;
            //g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            //g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            //g.Dispose();

            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode      = SmoothingMode.HighQuality;
            g.InterpolationMode  = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            b.Save(dest, imgToResize.RawFormat);
            g.Dispose();
            b.Dispose();
            //   imgToResize.Dispose();

            return(b);
        }
Esempio n. 2
0
        public string UploadPicResize(
            IFormFile file,
            string path,
            int maxPicWidth,
            ref long serverFileSize,
            int[] size,
            EnumC.Dimensions dimension,
            ref string[] resized
            )
        {
            var image           = Image.FromStream(file.OpenReadStream(), true, true);
            var allowedMimeType = new[] { "image/png", "image/x-png", "image/pjpeg", "image/jpeg", "image/gif" };
            var serverfileName  = "";
            var webroot         = _hostingEnvironment.WebRootPath;

            path = webroot + path;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }


            int result = 0; //Failor Flag

            if (file != null && file.Length != 0)
            {
                var a            = file.OpenReadStream();
                var filePath     = "";
                var userFileName = file.FileName;
                if (IsExtensionAllowed(allowedMimeType, file.ContentType))
                {
                    var r = new Random();
                    if (File.Exists(Path.Combine(path, userFileName)))
                    {
                        string newstr;
                        do
                        {
                            int randomnumber = r.Next(1, 10);
                            newstr = string.Concat(DateTime.Now.ToShortPersianDateString().Replace("/", ""), randomnumber,
                                                   userFileName.Replace(" ", ""));
                        } while (File.Exists(path + newstr));

                        filePath = Path.Combine(path, newstr);

                        if (image.Width > maxPicWidth)
                        {
                            var resizedImg = ResizeImage(image, maxPicWidth, EnumC.Dimensions.Width, filePath);
                            // resizedImg.Save(filePath);
                        }
                        else
                        {
                            using (var fileStream = new FileStream(filePath, FileMode.Create))
                            {
                                file.CopyTo(fileStream);
                            }
                        }

                        serverfileName = newstr;
                        serverFileSize = file.Length / 1024; //Save server file size in KB
                    }
                    else
                    {
                        filePath = Path.Combine(path, userFileName.Replace(" ", ""));

                        if (image.Width > maxPicWidth)
                        {
                            var resizedImg = ResizeImage(image, maxPicWidth, EnumC.Dimensions.Width, filePath);
                            //    resizedImg.Save(filePath);
                        }
                        else
                        {
                            using (var fileStream = new FileStream(filePath, FileMode.Create))
                            {
                                file.CopyTo(fileStream);
                            }
                        }
                        serverfileName = userFileName.Replace(" ", "");
                        serverFileSize = file.Length / 1024; //Save server file size in KB
                    }

                    for (int i = 0; i < size.Length; i++)
                    {
                        resized[i] = ResizeImage(image, serverfileName, path, size[i], EnumC.Dimensions.Width);
                    }



                    return(serverfileName);
                }
                throw new Exception("unexpected extension in upload Service");
            }
            throw new Exception("No File were selected in upload Service");
        }
Esempio n. 3
0
        public string ResizeImage(Image imgToResize, string fileName, string path, int size, EnumC.Dimensions dimension)
        {
            string dest = path + "/" + size;

            if (!Directory.Exists(dest))
            {
                Directory.CreateDirectory(dest);
            }



            var   sourceWidth  = imgToResize.Width;
            var   sourceHeight = imgToResize.Height;
            float nPercent     = 0;

            switch (dimension)
            {
            case EnumC.Dimensions.Width:
                nPercent = ((float)size / sourceWidth);
                break;

            case EnumC.Dimensions.Height:
                break;

            default:
                nPercent = (size / (float)sourceHeight);
                break;
            }
            var destWidth  = (int)(sourceWidth * nPercent);
            var destHeight = (int)(sourceHeight * nPercent);
            var b          = new Bitmap(destWidth, destHeight);
            var g          = Graphics.FromImage(b);

            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode      = SmoothingMode.HighQuality;
            g.InterpolationMode  = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            b.Save(dest + "/" + fileName, imgToResize.RawFormat);
            g.Dispose();
            b.Dispose();
            imgToResize.Dispose();
            return(fileName);
        }
Esempio n. 4
0
        /// <summary>
        /// This method used to resize images and like resizefile method, but this has most quality
        /// </summary>
        /// <param name="path"></param>
        /// <param name="size">determinde a int for width/height of image and set its height/width proportion to width/height</param>
        /// <param name="dimension">dimension determine size for width or height</param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string ResizeImage(string fileName, string path, string album, int size, EnumC.Dimensions dimension)
        {
            var strAddress = _hostingEnvironment.WebRootPath + "/" + path + "/" + fileName;

            if (!File.Exists(strAddress))
            {
                return("no Exist"); // not exist source file
            }
            string dest = _hostingEnvironment.WebRootPath + "/" + path + "/" + album + "/" + size;

            if (!Directory.Exists(dest))
            {
                Directory.CreateDirectory(dest);
            }

            Stream st          = new FileStream(strAddress, FileMode.Open);
            var    imgToResize = Image.FromStream(st);

            st.Close();
            st.Dispose();
            var   sourceWidth  = imgToResize.Width;
            var   sourceHeight = imgToResize.Height;
            float nPercent     = 0;

            switch (dimension)
            {
            case EnumC.Dimensions.Width:
                nPercent = ((float)size / sourceWidth);
                break;

            case EnumC.Dimensions.Height:
                break;

            default:
                nPercent = (size / (float)sourceHeight);
                break;
            }
            var destWidth  = (int)(sourceWidth * nPercent);
            var destHeight = (int)(sourceHeight * nPercent);
            var b          = new Bitmap(destWidth, destHeight);
            var g          = Graphics.FromImage(b);

            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode      = SmoothingMode.HighQuality;
            g.InterpolationMode  = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            b.Save(dest + "/" + album + ".jpg", imgToResize.RawFormat);
            g.Dispose();
            b.Dispose();
            imgToResize.Dispose();
            return(album + ".jpg");
        }