Bloom.ImageProcessing.ImageUtils.AppearsToBeJpeg C# (CSharp) Method

AppearsToBeJpeg() public static method

public static AppearsToBeJpeg ( SIL.Windows.Forms.ImageToolbox.PalasoImage imageInfo ) : bool
imageInfo SIL.Windows.Forms.ImageToolbox.PalasoImage
return bool
        public static bool AppearsToBeJpeg(PalasoImage imageInfo)
        {
            // A user experienced a crash due to a null object in this section of the code.
            // I've added a couple of checks to prevent that kind of crash here.
            if (imageInfo == null || imageInfo.Image == null)
                return false;
            /*
             * Note, each guid is VERY SIMILAR. The difference is only in the last 2 digits of the 1st group.
               Undefined  B96B3CA9
                MemoryBMP  B96B3CAA
                BMP    B96B3CAB
                EMF    B96B3CAC
                WMF    B96B3CAD
                JPEG    B96B3CAE
                PNG    B96B3CAF
                GIF    B96B3CB0
                TIFF    B96B3CB1
                EXIF    B96B3CB2
                Icon    B96B3CB5
             */
            if(imageInfo.Image.RawFormat != null && ImageFormat.Jpeg.Guid == imageInfo.Image.RawFormat.Guid)
                return true;

            if(ImageFormat.Jpeg.Equals(imageInfo.Image.PixelFormat))//review
                return true;

            if(string.IsNullOrEmpty(imageInfo.FileName))
                return false;

            return new[] { ".jpg", ".jpeg" }.Contains(Path.GetExtension(imageInfo.FileName).ToLowerInvariant());
        }

Usage Example

Example #1
0
        // Make a thumbnail of the input image. newWidth and newHeight are both limits; the image will not
        // be larger than original, but if necessary will be shrunk to fit within the indicated rectangle.
        // If parameter 'backColor' is not Empty, we fill the background of the thumbnail with that color.
        public static bool GenerateThumbnail(string originalPath, string pathToProcessedImage, int newWidth, Color backColor)
        {
            using (var originalImage = PalasoImage.FromFileRobustly(originalPath))
            {
                // check if it needs resized
                if (originalImage.Image.Width <= newWidth)
                {
                    return(false);
                }

                // calculate dimensions
                var newW = (originalImage.Image.Width > newWidth) ? newWidth : originalImage.Image.Width;
                // allow for proper rounding from the division
                var newH = (newW * originalImage.Image.Height + (originalImage.Image.Width / 2)) / originalImage.Image.Width;

                var thumbnail = new Bitmap(newW, newH);

                var g = Graphics.FromImage(thumbnail);
                if (backColor != Color.Empty)
                {
                    using (var brush = new SolidBrush(backColor))
                    {
                        g.FillRectangle(brush, new Rectangle(0, 0, newW, newH));
                    }
                }
                Image imageToDraw      = originalImage.Image;
                bool  useOriginalImage = ImageUtils.AppearsToBeJpeg(originalImage);
                if (!useOriginalImage)
                {
                    imageToDraw = MakePngBackgroundTransparent(originalImage);
                }
                var destRect = new Rectangle(0, 0, newW, newH);
                // Note the image size may change when the background is made transparent.
                // See https://silbloom.myjetbrains.com/youtrack/issue/BL-5632.
                g.DrawImage(imageToDraw, destRect, new Rectangle(0, 0, imageToDraw.Width, imageToDraw.Height), GraphicsUnit.Pixel);
                if (!useOriginalImage)
                {
                    imageToDraw.Dispose();
                }
                RobustImageIO.SaveImage(thumbnail, pathToProcessedImage);
            }

            return(true);
        }
All Usage Examples Of Bloom.ImageProcessing.ImageUtils::AppearsToBeJpeg