コード例 #1
0
ファイル: LightResize.cs プロジェクト: imazen/lightresize
        /// <summary>
        /// Loads the bitmap from stream, processes, and renders, sending the result Bitmap to the 'consumer' callback for encoding or usage. 
        /// </summary>
        /// <param name="s"></param>
        /// <param name="consumer"></param>
        /// <param name="options"></param>
        protected void Build(Stream s, BitmapConsumer consumer, JobOptions options)
        {
            var leaveSourceStreamOpen = ((options & JobOptions.LeaveSourceStreamOpen) != 0 ||
                                 (options & JobOptions.RewindSourceStream) != 0);

            var bufferSource = ((options & JobOptions.BufferEntireSourceStream) != 0);
            var originalPosition = ((options & JobOptions.RewindSourceStream) != 0) ? s.Position : -1;
            var preserveTemp = ((options & JobOptions.PreserveTargetBitmap) != 0);

            try
            {
                try
                {
                    //Buffer source stream if requested
                    UnderlyingStream = bufferSource ? StreamUtils.CopyToMemoryStream(s,false,0x1000) : s;

                    //Allow early disposal (enables same-file edits)
                    if (bufferSource && !leaveSourceStreamOpen)
                    {
                        s.Dispose();
                        s = null;
                    }
                    //Load bitmap
                    Source = new Bitmap(UnderlyingStream, !IgnoreIccProfile);
                    //Use size
                    OriginalSize = Source.Size;

                    //Do math
                    Layout();
                    //Render to 'Dest'
                    Render();
                }
                finally
                {
                    try
                    {
                        //Dispose loaded bitmap instance
                        if (Source != null) Source.Dispose();
                    }
                    finally
                    {
                        Source = null; //Ensure reference is null
                        try
                        {
                            //Dispose buffer
                            if (UnderlyingStream != null && s != UnderlyingStream) UnderlyingStream.Dispose();
                        }
                        finally
                        {
                            UnderlyingStream = null; //Ensure reference is null
                            //Dispose source stream or restore its position
                            if (!leaveSourceStreamOpen && s != null) s.Dispose();
                            else if (originalPosition > -1 && s != null && s.CanSeek) s.Position = originalPosition;
                        }
                    }
                }
                //Fire callback to write to disk or use Bitmap instance directly
                consumer(Dest,options);
            }
            finally
            {
                //Temporary bitmap must  be disposed
                if (!preserveTemp && Dest != null) {
                    Dest.Dispose();
                    Dest = null;
                }
            }
        }
コード例 #2
0
ファイル: LightResize.cs プロジェクト: lajjne/lightresize
        /// <summary>
        /// Loads the bitmap from stream, processes, and renders, sending the result Bitmap to the 'consumer' callback for encoding or usage.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="consumer"></param>
        /// <param name="options"></param>
        protected void Build(Stream s, BitmapConsumer consumer, JobOptions options)
        {
            var leaveSourceStreamOpen = ((options & JobOptions.LeaveSourceStreamOpen) != 0 ||
                                         (options & JobOptions.RewindSourceStream) != 0);

            var bufferSource     = ((options & JobOptions.BufferEntireSourceStream) != 0);
            var originalPosition = ((options & JobOptions.RewindSourceStream) != 0) ? s.Position : -1;
            var preserveTemp     = ((options & JobOptions.PreserveTargetBitmap) != 0);

            try
            {
                try
                {
                    //Buffer source stream if requested
                    UnderlyingStream = bufferSource ? StreamUtils.CopyToMemoryStream(s, false, 0x1000) : s;

                    //Allow early disposal (enables same-file edits)
                    if (bufferSource && !leaveSourceStreamOpen)
                    {
                        s.Dispose();
                        s = null;
                    }
                    //Load bitmap
                    Source = new Bitmap(UnderlyingStream, !IgnoreIccProfile);
                    //Use size
                    OriginalSize = Source.Size;

                    //Do math
                    Layout();
                    //Render to 'Dest'
                    Render();
                }
                finally
                {
                    try
                    {
                        //Dispose loaded bitmap instance
                        if (Source != null)
                        {
                            Source.Dispose();
                        }
                    }
                    finally
                    {
                        Source = null; //Ensure reference is null
                        try
                        {
                            //Dispose buffer
                            if (UnderlyingStream != null && s != UnderlyingStream)
                            {
                                UnderlyingStream.Dispose();
                            }
                        }
                        finally
                        {
                            UnderlyingStream = null; //Ensure reference is null
                            //Dispose source stream or restore its position
                            if (!leaveSourceStreamOpen && s != null)
                            {
                                s.Dispose();
                            }
                            else if (originalPosition > -1 && s != null && s.CanSeek)
                            {
                                s.Position = originalPosition;
                            }
                        }
                    }
                }
                //Fire callback to write to disk or use Bitmap instance directly
                consumer(Dest, options);
            }
            finally
            {
                //Temporary bitmap must  be disposed
                if (!preserveTemp && Dest != null)
                {
                    Dest.Dispose();
                    Dest = null;
                }
            }
        }