Esempio n. 1
0
        internal Image(Stream stream, DirectCanvasFactory directCanvasFactory)
        {
            m_directCanvasFactory = directCanvasFactory;
          
            var factory = m_directCanvasFactory.WicImagingFactory;

            var nativeStream = new NativeStream(stream);

            var vendorGuid = Guid.Empty;
            IWICBitmapDecoder decoder;

            int hr = factory.CreateDecoderFromStream(nativeStream,
                                                     ref vendorGuid,
                                                     WICDecodeOptions.WICDecodeMetadataCacheOnLoad,
                                                     out decoder);

            if (hr != 0)
               throw new Exception(string.Format("Could not create the decoder from stream {0}", hr));

            Initialize(decoder);
        }
Esempio n. 2
0
        /// <summary>
        /// Saves an image to a stream
        /// </summary>
        /// <param name="stream">The stream to write to</param>
        /// <param name="format">The image format to use</param>
        public void Save(Stream stream, ImageFormat format)
        {
            /* Get the wic factory from our direct canvas factory */
            var factory = m_directCanvasFactory.WicImagingFactory;

            /* The WIC encoder interface */
            IWICBitmapEncoder encoder = null;
            IWICBitmapFrameEncode frame = null;
            Exception exception = null;

            /* The area of the image we are going to encode */
            var rect = new WICRect() { X = 0, Y = 0, Width = Width, Height = Height };

            /* We don't prefer any vendor */
            Guid vendor = Guid.Empty;

            /* Get the WIC Guid for the format we want to use */
            Guid wicContainerFormat = ImageFormatToWicContainerFormat(format);

            /* Create our encoder */
            int hr = factory.CreateEncoder(ref wicContainerFormat, 
                                           ref vendor, 
                                           out encoder);

            if (hr != 0)
            {
                exception = new Exception("Could not create the encoder");
                goto cleanup;
            }

            /* Here we wrap our .NET stream with a COM IStream */
            var nativeStream = new NativeStream(stream);

            /* This intializes the encoder */
            hr = encoder.Initialize(nativeStream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);
            if(hr != 0)
            {
                exception = new Exception("Could not initialize the encoder");
                goto cleanup;
            }

            IntPtr options = IntPtr.Zero;

            /* Creates a new frame to be encoded */
            hr = encoder.CreateNewFrame(out frame, ref options);

            if (hr != 0)
            {
                exception = new Exception("Could not create a new frame for the encoder");
                goto cleanup;
            }

            /* This intializes with the defaults */
            hr = frame.Initialize(IntPtr.Zero);

            if (hr != 0)
            {
                exception = new Exception("Could not initialize the encoded frame");
                goto cleanup;
            }

            /* Sets the size of the frame */
            hr = frame.SetSize(Width, Height);

            if (hr != 0)
            {
                exception = new Exception("Could not set the size of the encode frame");
                goto cleanup;
            }

            hr = frame.SetPixelFormat(ref WICFormats.WICPixelFormatDontCare);

            /* Write our WIC bitmap source */
            hr = frame.WriteSource(m_internalBitmap, ref rect);

            if (hr != 0)
            {
                exception = new Exception("Could not write the bitmap source to the frame");
                goto cleanup;
            }

            /* Commits all our changes to the frame  */
            hr = frame.Commit();
            if (hr != 0)
            {
                exception = new Exception("Could not commit the frame");
                goto cleanup;
            }

            /* Actually does all the encoding */
            hr = encoder.Commit();

            if (hr != 0)
            {
                exception = new Exception("Could not commit the encoder");
                goto cleanup;
            }

        cleanup:

            if(frame != null)
                Marshal.ReleaseComObject(frame);    
            
            if(encoder != null)
                Marshal.ReleaseComObject(encoder);    

            if (exception != null)
                throw exception;
        }