Exemple #1
0
        public static string SaveBitmap(Bitmap bitmap, string path, string fileName = null, int quality = 100, bool isResize = true)
        {
            int maxWidth = 2000;

            if (isResize && bitmap.Width > maxWidth)
            {
                double height = (int)(bitmap.AspectRatio() * maxWidth);
                bitmap = Resizer.ResizeImage(bitmap, null, maxWidth.ToString(), height.ToString(), FitMode.Max);
            }
            //int maxHeight = 2000;
            //if (isResize)
            //{
            //    //TODO:
            ////bitmap = Resizer.ResizeImage(bitmap, null, Width.ToString(), Height.ToString(), FitMode.Max);
            //}

            string extension = "jpg";



            if (string.IsNullOrWhiteSpace(fileName))
            {
                fileName = CreaDev.Framework.Core.IO.File.GetRandomFileName();
            }

            if (!path.EndsWith("\\"))
            {
                path = path + "\\";
            }
            /**/


            // Get an ImageCodecInfo object that represents the JPEG codec.
            var myImageCodecInfo = GetEncoderInfo("image/jpeg");
            // for the Quality parameter category.
            var myEncoder = Encoder.Quality;
            // EncoderParameter object in the array.
            var myEncoderParameters = new EncoderParameters(1);
            // Save the bitmap as a JPEG file with quality provided.
            var myEncoderParameter = new EncoderParameter(myEncoder, (long)quality);

            myEncoderParameters.Param[0] = myEncoderParameter;
            bitmap.Save(path + fileName + "." + extension, myImageCodecInfo, myEncoderParameters);

            return(fileName + "." + extension);
        }

        public static string SaveBitmapByExtension(Bitmap bitmap, string path, string originalFileName, int quality = 100, bool isResize = true)
        {
            int    maxWidth  = 500;
            string extension = originalFileName.Substring(originalFileName.LastIndexOf('.'));
            String mime      = _mappings[extension];


            if (isResize && bitmap.Width > maxWidth)
            {
                double height = (int)(bitmap.AspectRatio() * maxWidth);
                bitmap = Resizer.ResizeImage(bitmap, null, maxWidth.ToString(), height.ToString(), FitMode.Max);
            }
            //int maxHeight = 2000;
            //if (isResize)
            //{
            //    //TODO:
            ////bitmap = Resizer.ResizeImage(bitmap, null, Width.ToString(), Height.ToString(), FitMode.Max);
            //}



            string fileName = CreaDev.Framework.Core.IO.File.GetRandomFileName();

            if (!path.EndsWith("\\"))
            {
                path = path + "\\";
            }
            /**/


            // Get an ImageCodecInfo object that represents the JPEG codec.
            ImageCodecInfo myImageCodecInfo = GetEncoderInfo(mime);


            // for the Quality parameter category.
            var myEncoder = Encoder.Quality;
            // EncoderParameter object in the array.
            var myEncoderParameters = new EncoderParameters(1);
            // Save the bitmap as a JPEG file with quality provided.
            var myEncoderParameter = new EncoderParameter(myEncoder, (long)quality);

            myEncoderParameters.Param[0] = myEncoderParameter;
            bitmap.Save(path + fileName + extension, myImageCodecInfo, myEncoderParameters);

            return(fileName + extension);
        }
Exemple #2
0
        public static string SaveBitmapByMimeType(Bitmap bitmap, string path, string mimeType, string originalFileName, int quality = 100, bool isResize = true)
        {
            int    maxWidth  = 2000;
            string extension = originalFileName.Substring(originalFileName.LastIndexOf('.'));

            if (isResize && bitmap.Width > maxWidth)
            {
                double height = (int)(bitmap.AspectRatio() * maxWidth);
                bitmap = Resizer.ResizeImage(bitmap, null, maxWidth.ToString(), height.ToString(), FitMode.Max);
            }
            //int maxHeight = 2000;
            //if (isResize)
            //{
            //    //TODO:
            ////bitmap = Resizer.ResizeImage(bitmap, null, Width.ToString(), Height.ToString(), FitMode.Max);
            //}



            string fileName = CreaDev.Framework.Core.IO.File.GetRandomFileName();

            if (!path.EndsWith("\\"))
            {
                path = path + "\\";
            }
            /**/


            // Get an ImageCodecInfo object that represents the JPEG codec.
            ImageCodecInfo myImageCodecInfo = GetEncoderInfo(mimeType);


            // for the Quality parameter category.
            var myEncoder = Encoder.Quality;
            // EncoderParameter object in the array.
            var myEncoderParameters = new EncoderParameters(1);
            // Save the bitmap as a JPEG file with quality provided.
            var myEncoderParameter = new EncoderParameter(myEncoder, (long)quality);

            myEncoderParameters.Param[0] = myEncoderParameter;
            bitmap.Save(path + fileName + extension, myImageCodecInfo, myEncoderParameters);

            return(fileName + extension);
        }

        public static string SaveImageFromBytes(byte[] bytes, string path, string mimeType, string originalFileName, int quality = 100, bool isResize = true)
        {
            Bitmap       bitmap;
            MemoryStream stream = new MemoryStream();

            stream.Write(bytes, 0, bytes.Length);
            stream.Flush();
            bitmap = new Bitmap(stream);
            int    maxWidth  = 2000;
            string extension = originalFileName.Substring(originalFileName.LastIndexOf('.'));

            if (isResize && bitmap.Width > maxWidth)
            {
                double height = (int)(bitmap.AspectRatio() * maxWidth);
                bitmap = Resizer.ResizeImage(bitmap, null, maxWidth.ToString(), height.ToString(), FitMode.Max);
            }
            //int maxHeight = 2000;
            //if (isResize)
            //{
            //    //TODO:
            ////bitmap = Resizer.ResizeImage(bitmap, null, Width.ToString(), Height.ToString(), FitMode.Max);
            //}



            string fileName = CreaDev.Framework.Core.IO.File.GetRandomFileName();

            if (!path.EndsWith("\\"))
            {
                path = path + "\\";
            }
            /**/


            // Get an ImageCodecInfo object that represents the JPEG codec.
            ImageCodecInfo myImageCodecInfo = GetEncoderInfo(mimeType);


            // for the Quality parameter category.
            var myEncoder = Encoder.Quality;
            // EncoderParameter object in the array.
            var myEncoderParameters = new EncoderParameters(1);
            // Save the bitmap as a JPEG file with quality provided.
            var myEncoderParameter = new EncoderParameter(myEncoder, (long)quality);

            myEncoderParameters.Param[0] = myEncoderParameter;
            bitmap.Save(path + fileName + extension, myImageCodecInfo, myEncoderParameters);

            return(fileName + extension);
        }