示例#1
0
        internal void CopyToDrawingLayer(DrawingLayer drawingLayer, Rectangle destRect)
        {
            IntPtr pBuffer;
            int bufferSize;
            int stride = 0;
            int hr = 0;

            var rect = new WICRect
            {
                X = 0,
                Y = 0,
                Width = Width,
                Height = Height
            };

            IWICBitmapLock imageLock;

            hr = m_internalBitmap.Lock(ref rect,
                                       WICBitmapLockFlags.WICBitmapLockRead,
                                       out imageLock);

            if(hr != 0)
                throw new Exception("Could not lock the image");

            hr = imageLock.GetDataPointer(out bufferSize, out pBuffer);

            imageLock.GetStride(out stride);

            if(destRect.IsEmpty)
                drawingLayer.D2DRenderTarget.InternalBitmap.FromMemory(pBuffer, stride);
            else
                drawingLayer.D2DRenderTarget.InternalBitmap.FromMemory(pBuffer, 
                                                                       stride, 
                                                                       destRect.GetInternalRect());

            Marshal.ReleaseComObject(imageLock);
        }
示例#2
0
        public ImageData Lock(Rectangle lockRectangle, ImageLock lockType)
        {
            IWICBitmapLock imageLock;
            WICBitmapLockFlags flags;

            var rect = new WICRect
            {
                X = lockRectangle.X,
                Y = lockRectangle.Y,
                Width = lockRectangle.Width,
                Height = lockRectangle.Height
            };
           
            switch(lockType)
            {
                case ImageLock.Read:
                    flags = WICBitmapLockFlags.WICBitmapLockRead;
                    break;
                case ImageLock.Write:
                    flags = WICBitmapLockFlags.WICBitmapLockWrite;
                    break;
                case ImageLock.ReadWrite:
                    flags = WICBitmapLockFlags.WICBitmapLockRead | WICBitmapLockFlags.WICBitmapLockWrite;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("lockType");
            }

            int hr = m_internalBitmap.Lock(ref rect,
                                          flags,
                                          out imageLock);

            if (hr != 0)
                throw new Exception("Could not lock the image");

            return new ImageData(imageLock);
        }
示例#3
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;
        }