/// <summary>
        /// Gets the file bytes.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="uploadedFile">The uploaded file.</param>
        /// <returns></returns>
        public override Stream GetFileContentStream( HttpContext context, HttpPostedFile uploadedFile )
        {
            if ( uploadedFile.ContentType == "image/svg+xml" )
            {
                return base.GetFileContentStream( context, uploadedFile );
            }
            else
            {
                Bitmap bmp = new Bitmap( uploadedFile.InputStream );

                // Check to see if we should flip the image.
                var exif = new EXIFextractor( ref bmp, "\n" );
                if ( exif["Orientation"] != null )
                {
                    RotateFlipType flip = OrientationToFlipType( exif["Orientation"].ToString() );

                    // don't flip if orientation is correct
                    if ( flip != RotateFlipType.RotateNoneFlipNone )
                    {
                        bmp.RotateFlip( flip );
                        exif.setTag( 0x112, "1" ); // reset orientation tag
                    }
                }

                if ( context.Request.QueryString["enableResize"] != null )
                {
                    Bitmap resizedBmp = RoughResize( bmp, 1024, 768 );
                    bmp = resizedBmp;
                }

                var stream = new MemoryStream();
                bmp.Save( stream, ContentTypeToImageFormat( uploadedFile.ContentType ) );
                return stream;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the file bytes.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="uploadedFile">The uploaded file.</param>
        /// <returns></returns>
        public static Stream GetFileContentStream( HttpPostedFile uploadedFile, bool enableResize, bool errorIfNotImage = false )
        {
            if ( uploadedFile.ContentType == "image/svg+xml" || uploadedFile.ContentType == "image/tiff" )
            {
                return uploadedFile.InputStream;
            }
            else
            {
                try
                {
                    Bitmap bmp = new Bitmap( uploadedFile.InputStream );

                    // Check to see if we should flip the image.
                    var exif = new EXIFextractor( ref bmp, "\n" );
                    if ( exif["Orientation"] != null )
                    {
                        RotateFlipType flip = OrientationToFlipType( exif["Orientation"].ToString() );

                        // don't flip if orientation is correct
                        if ( flip != RotateFlipType.RotateNoneFlipNone )
                        {
                            bmp.RotateFlip( flip );
                            exif.setTag( 0x112, "1" ); // reset orientation tag
                        }
                    }

                    if ( enableResize )
                    {
                        Bitmap resizedBmp = RoughResize( bmp, 1024, 768 );
                        bmp = resizedBmp;
                    }

                    var stream = new MemoryStream();
                    bmp.Save( stream, ContentTypeToImageFormat( uploadedFile.ContentType ) );
                    return stream;
                }
                catch
                {
                    if ( errorIfNotImage )
                    {
                        throw new InvalidDataException( "The file is not a valid image type" );
                    }

                    // if it couldn't be converted to a bitmap or if the exif or resize thing failed, just return the original stream
                    return uploadedFile.InputStream;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the file bytes.
        /// </summary>
        /// <param name="uploadedFile">The uploaded file.</param>
        /// <param name="resizeIfImage">if set to <c>true</c> [resize if image].</param>
        /// <returns></returns>
        public static Stream GetFileContentStream( HttpPostedFile uploadedFile, bool resizeIfImage = true )
        {
            if ( uploadedFile.ContentType == "image/svg+xml" || uploadedFile.ContentType == "image/tiff" || !uploadedFile.ContentType.StartsWith( "image/" ) )
            {
                return uploadedFile.InputStream;
            }

            try
            {
                var bmp = new Bitmap( uploadedFile.InputStream );

                // Check to see if we should flip the image.
                var exif = new EXIFextractor( ref bmp, "\n" );
                if ( exif["Orientation"] != null )
                {
                    var flip = OrientationToFlipType( exif["Orientation"].ToString() );

                    // don't flip if orientation is correct
                    if ( flip != RotateFlipType.RotateNoneFlipNone )
                    {
                        bmp.RotateFlip( flip );
                        exif.setTag( 0x112, "1" ); // reset orientation tag
                    }
                }

                if ( resizeIfImage )
                {
                    bmp = RoughResize( bmp, 1024, 768 );
                }

                var stream = new MemoryStream();
                bmp.Save( stream, ContentTypeToImageFormat( uploadedFile.ContentType ) );
                return stream;
            }
            catch
            {
                // if it couldn't be converted to a bitmap or if the exif or resize thing failed, just return the original stream
                return uploadedFile.InputStream;
            }
        }