Generate() публичный статический Метод

Generates a thumbnail of the source image based on parameters.

Thumbnail width or height must be greater than 0. Otherwise an empty image is generated.

When ImageScaleMode.PreserveRatioNoFill used and both dimensions are set, if aspect ratio of source image and thumbnail doesn't match, thumbnail's horizontal or vertical size may be different than requested one. In, PreserveRatioWithFill mode thumbnail size will be at requested size but empty parts are filled with a solid color.

public static Generate ( Image image, int thumbWidth, int thumbHeight, ImageScaleMode mode, Color backgroundColor, float xDPI, float yDPI ) : Image
image Image /// Image object to generate thumbnail for (required)
thumbWidth int /// Thumbnail width. If 0, width is calculated by source aspect ratio. Only one of /// width or height can be zero.
thumbHeight int /// Thumbnail height. If 0, height is calculated by source aspect ratio. Only one of /// width or height can be zero.
mode ImageScaleMode /// Thumbnail generation mode. It is only important when both dimensions are specified and /// source aspect ratio is different than thumbnail (see ).
backgroundColor Color /// Specifies fill color for PreserveRatioWithFill mode.
xDPI float
yDPI float
Результат Image
Пример #1
0
        private bool ProcessImageStream(Stream fileContent, ITextLocalizer localizer)
        {
            var imageChecker = new ImageChecker();

            CheckResult = imageChecker.CheckStream(fileContent, true, out Image image, Logger);
            try
            {
                FileSize    = imageChecker.DataSize;
                ImageWidth  = imageChecker.Width;
                ImageHeight = imageChecker.Height;

                if (CheckResult != ImageCheckResult.JPEGImage &&
                    CheckResult != ImageCheckResult.GIFImage &&
                    CheckResult != ImageCheckResult.PNGImage)
                {
                    ErrorMessage = imageChecker.FormatErrorMessage(CheckResult, localizer);
                    return(false);
                }
                else
                {
                    IsImage = true;

                    var extension = CheckResult == ImageCheckResult.PNGImage ? ".png" :
                                    (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg");

                    storage.PurgeTemporaryFiles();

                    if (ThumbWidth > 0 || ThumbHeight > 0)
                    {
                        using Image thumbImage =
                                  ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor);
                        var thumbFile = UploadPathHelper.GetThumbnailName(TemporaryFile);

                        using (var ms = new MemoryStream())
                        {
                            if (ThumbQuality != 0)
                            {
                                var p = new EncoderParameters(1);
                                p.Param[0] = new EncoderParameter(Encoder.Quality, ThumbQuality);

                                ImageCodecInfo   jpegCodec = null;
                                ImageCodecInfo[] codecs    = ImageCodecInfo.GetImageEncoders();
                                // Find the correct image codec
                                for (int i = 0; i < codecs.Length; i++)
                                {
                                    if (codecs[i].MimeType == "image/jpeg")
                                    {
                                        jpegCodec = codecs[i];
                                    }
                                }

                                thumbImage.Save(ms, jpegCodec, p);
                            }
                            else
                            {
                                thumbImage.Save(ms, ImageFormat.Jpeg);
                            }
                            ms.Seek(0, SeekOrigin.Begin);
                            ThumbFile = storage.WriteFile(thumbFile, ms, autoRename: false);
                        }
                        ThumbHeight = thumbImage.Width;
                        ThumbWidth  = thumbImage.Height;
                    }

                    return(true);
                }
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }
Пример #2
0
        private bool ProcessImageStream(Stream fileContent, string extension)
        {
            Image image        = null;
            var   imageChecker = new ImageChecker();

            CheckResult = imageChecker.CheckStream(fileContent, true, out image);
            try
            {
                FileSize    = imageChecker.DataSize;
                ImageWidth  = imageChecker.Width;
                ImageHeight = imageChecker.Height;

                if (CheckResult != ImageCheckResult.JPEGImage &&
                    CheckResult != ImageCheckResult.GIFImage &&
                    CheckResult != ImageCheckResult.PNGImage)
                {
                    ErrorMessage = imageChecker.FormatErrorMessage(CheckResult);
                    return(false);
                }
                else
                {
                    IsImage = true;

                    extension = (CheckResult == ImageCheckResult.PNGImage ? ".png" :
                                 (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg"));

                    var temporaryPath = UploadHelper.TemporaryPath;
                    Directory.CreateDirectory(temporaryPath);
                    TemporaryFileHelper.PurgeDirectoryDefault(temporaryPath);

                    string baseFileName = System.IO.Path.Combine(temporaryPath, Guid.NewGuid().ToString("N"));

                    FilePath = baseFileName + extension;
                    fileContent.Seek(0, SeekOrigin.Begin);
                    using (FileStream fs = new FileStream(FilePath, FileMode.Create))
                        fileContent.CopyTo(fs);

                    if (ThumbWidth > 0 || ThumbHeight > 0)
                    {
                        using (System.Drawing.Image thumbImage =
                                   ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor))
                        {
                            ThumbFile = baseFileName + "_t.jpg";

                            if (ThumbQuality != 0)
                            {
                                var p = new System.Drawing.Imaging.EncoderParameters(1);
                                p.Param[0] = new EncoderParameter(Encoder.Quality, ThumbQuality);

                                ImageCodecInfo   jpegCodec = null;
                                ImageCodecInfo[] codecs    = ImageCodecInfo.GetImageEncoders();
                                // Find the correct image codec
                                for (int i = 0; i < codecs.Length; i++)
                                {
                                    if (codecs[i].MimeType == "image/jpeg")
                                    {
                                        jpegCodec = codecs[i];
                                    }
                                }

                                thumbImage.Save(ThumbFile, jpegCodec, p);
                            }
                            else
                            {
                                thumbImage.Save(ThumbFile, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }

                            ThumbHeight = thumbImage.Width;
                            ThumbWidth  = thumbImage.Height;
                        }
                    }

                    return(true);
                }
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                    image = null;
                }
            }
        }
Пример #3
0
        private bool ProcessImageStream(Stream fileContent, ITextLocalizer localizer)
        {
            var imageChecker = new ImageChecker();

            CheckResult = imageChecker.CheckStream(fileContent, true, out Image image, Logger);
            try
            {
                FileSize    = imageChecker.DataSize;
                ImageWidth  = imageChecker.Width;
                ImageHeight = imageChecker.Height;

                if (CheckResult != ImageCheckResult.JPEGImage &&
                    CheckResult != ImageCheckResult.GIFImage &&
                    CheckResult != ImageCheckResult.PNGImage)
                {
                    ErrorMessage = imageChecker.FormatErrorMessage(CheckResult, localizer);
                    return(false);
                }
                else
                {
                    IsImage = true;

                    var extension = CheckResult == ImageCheckResult.PNGImage ? ".png" :
                                    (CheckResult == ImageCheckResult.GIFImage ? ".gif" : ".jpg");

                    storage.PurgeTemporaryFiles();

                    if (ThumbWidth > 0 || ThumbHeight > 0)
                    {
                        ThumbnailGenerator.Generate(image, ThumbWidth, ThumbHeight, ThumbScaleMode, ThumbBackColor,
                                                    inplace: true);
                        var thumbFile = UploadPathHelper.GetThumbnailName(TemporaryFile);

                        using (var ms = new MemoryStream())
                        {
                            if (ThumbQuality != 0)
                            {
                                image.Save(ms, new JpegEncoder {
                                    Quality = ThumbQuality
                                });
                            }
                            else
                            {
                                image.Save(ms, new JpegEncoder());
                            }
                            ms.Seek(0, SeekOrigin.Begin);
                            ThumbFile = storage.WriteFile(thumbFile, ms, autoRename: false);
                        }
                        ThumbHeight = image.Width;
                        ThumbWidth  = image.Height;
                    }

                    return(true);
                }
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
            }
        }