示例#1
0
        internal BitmapFrameEncode(
            BitmapSource source,
            BitmapSource thumbnail,
            BitmapMetadata metadata,
            ReadOnlyCollection <ColorContext> colorContexts
            )
            : base(true)
        {
            _bitmapInit.BeginInit();

            Debug.Assert(source != null);
            _source                = source;
            WicSourceHandle        = _source.WicSourceHandle;
            IsSourceCached         = _source.IsSourceCached;
            _isColorCorrected      = _source._isColorCorrected;
            _thumbnail             = thumbnail;
            _readOnlycolorContexts = colorContexts;
            InternalMetadata       = metadata;
            _syncObject            = source.SyncObject;
            _bitmapInit.EndInit();

            FinalizeCreation();
        }
示例#2
0
        internal void Init(object value)
        {
            if (value == null)
            {
                varType = (ushort)VarEnum.VT_EMPTY;
            }
            else if (value is Array)
            {
                Type type = value.GetType();

                if (type == typeof(sbyte[]))
                {
                    InitVector(value as Array, typeof(sbyte), VarEnum.VT_I1);
                }
                else if (type == typeof(byte[]))
                {
                    InitVector(value as Array, typeof(byte), VarEnum.VT_UI1);
                }
                else if (value is char[])
                {
                    varType = (ushort)VarEnum.VT_LPSTR;
                    pszVal  = Marshal.StringToCoTaskMemAnsi(new String(value as char[]));
                }
                else if (value is char[][])
                {
                    char[][] charArray = value as char[][];

                    String[] strArray = new String[charArray.GetLength(0)];

                    for (int i = 0; i < charArray.Length; i++)
                    {
                        strArray[i] = new String(charArray[i] as char[]);
                    }

                    Init(strArray, true);
                }
                else if (type == typeof(short[]))
                {
                    InitVector(value as Array, typeof(short), VarEnum.VT_I2);
                }
                else if (type == typeof(ushort[]))
                {
                    InitVector(value as Array, typeof(ushort), VarEnum.VT_UI2);
                }
                else if (type == typeof(int[]))
                {
                    InitVector(value as Array, typeof(int), VarEnum.VT_I4);
                }
                else if (type == typeof(uint[]))
                {
                    InitVector(value as Array, typeof(uint), VarEnum.VT_UI4);
                }
                else if (type == typeof(Int64[]))
                {
                    InitVector(value as Array, typeof(Int64), VarEnum.VT_I8);
                }
                else if (type == typeof(UInt64[]))
                {
                    InitVector(value as Array, typeof(UInt64), VarEnum.VT_UI8);
                }
                else if (value is float[])
                {
                    InitVector(value as Array, typeof(float), VarEnum.VT_R4);
                }
                else if (value is double[])
                {
                    InitVector(value as Array, typeof(double), VarEnum.VT_R8);
                }
                else if (value is Guid[])
                {
                    InitVector(value as Array, typeof(Guid), VarEnum.VT_CLSID);
                }
                else if (value is String[])
                {
                    Init(value as String[], false);
                }
                else if (value is bool[])
                {
                    bool[]  boolArray = value as bool[];
                    short[] array     = new short[boolArray.Length];

                    for (int i = 0; i < boolArray.Length; i++)
                    {
                        array[i] = (short)(boolArray[i] ? -1 : 0);
                    }

                    InitVector(array, typeof(short), VarEnum.VT_BOOL);
                }
                else
                {
                    throw new System.InvalidOperationException(SR.Get(SRID.Image_PropertyNotSupported));
                }
            }
            else
            {
                Type type = value.GetType();

                if (value is String)
                {
                    varType = (ushort)VarEnum.VT_LPWSTR;
                    pwszVal = Marshal.StringToCoTaskMemUni(value as String);
                }
                else if (type == typeof(sbyte))
                {
                    varType = (ushort)VarEnum.VT_I1;
                    cVal    = (sbyte)value;
                }
                else if (type == typeof(byte))
                {
                    varType = (ushort)VarEnum.VT_UI1;
                    bVal    = (byte)value;
                }
                else if (type == typeof(System.Runtime.InteropServices.ComTypes.FILETIME))
                {
                    varType  = (ushort)VarEnum.VT_FILETIME;
                    filetime = (System.Runtime.InteropServices.ComTypes.FILETIME)value;
                }
                else if (value is char)
                {
                    varType = (ushort)VarEnum.VT_LPSTR;
                    pszVal  = Marshal.StringToCoTaskMemAnsi(new String(new char[] { (char)value }));
                }
                else if (type == typeof(short))
                {
                    varType = (ushort)VarEnum.VT_I2;
                    iVal    = (short)value;
                }
                else if (type == typeof(ushort))
                {
                    varType = (ushort)VarEnum.VT_UI2;
                    uiVal   = (ushort)value;
                }
                else if (type == typeof(int))
                {
                    varType = (ushort)VarEnum.VT_I4;
                    intVal  = (int)value;
                }
                else if (type == typeof(uint))
                {
                    varType = (ushort)VarEnum.VT_UI4;
                    uintVal = (uint)value;
                }
                else if (type == typeof(Int64))
                {
                    varType = (ushort)VarEnum.VT_I8;
                    lVal    = (Int64)value;
                }
                else if (type == typeof(UInt64))
                {
                    varType = (ushort)VarEnum.VT_UI8;
                    ulVal   = (UInt64)value;
                }
                else if (value is float)
                {
                    varType = (ushort)VarEnum.VT_R4;
                    fltVal  = (float)value;
                }
                else if (value is double)
                {
                    varType = (ushort)VarEnum.VT_R8;
                    dblVal  = (double)value;
                }
                else if (value is Guid)
                {
                    byte[] guid = ((Guid)value).ToByteArray();
                    varType   = (ushort)VarEnum.VT_CLSID;
                    pclsidVal = Marshal.AllocCoTaskMem(guid.Length);
                    Marshal.Copy(guid, 0, pclsidVal, guid.Length);
                }
                else if (value is bool)
                {
                    varType = (ushort)VarEnum.VT_BOOL;
                    boolVal = (short)(((bool)value) ? -1 : 0);
                }
                else if (value is BitmapMetadataBlob)
                {
                    Init((value as BitmapMetadataBlob).InternalGetBlobValue(), typeof(byte), VarEnum.VT_BLOB);
                }
                else if (value is BitmapMetadata)
                {
                    IntPtr         punkTemp = IntPtr.Zero;
                    BitmapMetadata metadata = value as BitmapMetadata;

                    SafeMILHandle metadataHandle = metadata.InternalMetadataHandle;

                    if (metadataHandle == null || metadataHandle.IsInvalid)
                    {
                        throw new NotImplementedException();
                    }

                    Guid wicMetadataQueryReader = MILGuidData.IID_IWICMetadataQueryReader;
                    HRESULT.Check(UnsafeNativeMethods.MILUnknown.QueryInterface(
                                      metadataHandle,
                                      ref wicMetadataQueryReader,
                                      out punkTemp));

                    varType = (ushort)VarEnum.VT_UNKNOWN;
                    punkVal = punkTemp;
                }
                else
                {
                    throw new System.InvalidOperationException(SR.Get(SRID.Image_PropertyNotSupported));
                }
            }
        }
        private void SaveFrame(SafeMILHandle frameEncodeHandle, SafeMILHandle encoderOptions, BitmapFrame frame)
        {
            SetupFrame(frameEncodeHandle, encoderOptions);

            // Helpful for debugging stress and remote dumps
            _encodeState = EncodeState.FrameEncodeInitialized;

            // Set the size
            HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.SetSize(
                              frameEncodeHandle,
                              frame.PixelWidth,
                              frame.PixelHeight
                              ));

            // Helpful for debugging stress and remote dumps
            _encodeState = EncodeState.FrameEncodeSizeSet;

            // Set the resolution
            double dpiX = frame.DpiX;
            double dpiY = frame.DpiY;

            if (dpiX <= 0)
            {
                dpiX = 96;
            }
            if (dpiY <= 0)
            {
                dpiY = 96;
            }

            HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.SetResolution(
                              frameEncodeHandle,
                              dpiX,
                              dpiY
                              ));

            // Helpful for debugging stress and remote dumps
            _encodeState = EncodeState.FrameEncodeResolutionSet;

            if (_supportsFrameThumbnails)
            {
                // Set the thumbnail.
                BitmapSource thumbnail = frame.Thumbnail;

                if (thumbnail != null)
                {
                    SafeMILHandle thumbnailHandle = thumbnail.WicSourceHandle;

                    lock (thumbnail.SyncObject)
                    {
                        HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.SetThumbnail(
                                          frameEncodeHandle,
                                          thumbnailHandle
                                          ));

                        // Helpful for debugging stress and remote dumps
                        _encodeState = EncodeState.FrameEncodeThumbnailSet;
                    }
                }
            }

            // if the source has been color corrected, we want to use a corresponding color profile
            if (frame._isColorCorrected)
            {
                ColorContext colorContext     = new ColorContext(frame.Format);
                IntPtr[]     colorContextPtrs = new IntPtr[1] {
                    colorContext.ColorContextHandle.DangerousGetHandle()
                };

                int hr = UnsafeNativeMethods.WICBitmapFrameEncode.SetColorContexts(
                    frameEncodeHandle,
                    1,
                    colorContextPtrs
                    );

                // It's possible that some encoders may not support color contexts so don't check hr
                if (hr == HRESULT.S_OK)
                {
                    // Helpful for debugging stress and remote dumps
                    _encodeState = EncodeState.FrameEncodeColorContextsSet;
                }
            }
            // if the caller has explicitly provided color contexts, add them to the encoder
            else
            {
                IList <ColorContext> colorContexts = frame.ColorContexts;
                if (colorContexts != null && colorContexts.Count > 0)
                {
                    int count = colorContexts.Count;

                    // Marshal can't convert SafeMILHandle[] so we must
                    {
                        IntPtr[] colorContextPtrs = new IntPtr[count];
                        for (int i = 0; i < count; ++i)
                        {
                            colorContextPtrs[i] = colorContexts[i].ColorContextHandle.DangerousGetHandle();
                        }

                        int hr = UnsafeNativeMethods.WICBitmapFrameEncode.SetColorContexts(
                            frameEncodeHandle,
                            (uint)count,
                            colorContextPtrs
                            );

                        // It's possible that some encoders may not support color contexts so don't check hr
                        if (hr == HRESULT.S_OK)
                        {
                            // Helpful for debugging stress and remote dumps
                            _encodeState = EncodeState.FrameEncodeColorContextsSet;
                        }
                    }
                }
            }

            // Set the pixel format and palette

            lock (frame.SyncObject)
            {
                SafeMILHandle outSourceHandle    = new SafeMILHandle();
                SafeMILHandle bitmapSourceHandle = frame.WicSourceHandle;
                SafeMILHandle paletteHandle      = new SafeMILHandle();

                // Set the pixel format and palette of the bitmap.
                // This could (but hopefully won't) introduce a format converter.
                HRESULT.Check(UnsafeNativeMethods.WICCodec.WICSetEncoderFormat(
                                  bitmapSourceHandle,
                                  paletteHandle,
                                  frameEncodeHandle,
                                  out outSourceHandle
                                  ));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.FrameEncodeFormatSet;
                _writeSourceHandles.Add(outSourceHandle);

                // Set the metadata
                if (_supportsFrameMetadata)
                {
                    BitmapMetadata metadata = frame.Metadata as BitmapMetadata;

                    // If the frame has metadata associated with a different container format, then we ignore it.
                    if (metadata != null && metadata.GuidFormat == ContainerFormat)
                    {
                        SafeMILHandle /* IWICMetadataQueryWriter */ metadataHandle = new SafeMILHandle();

                        HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.GetMetadataQueryWriter(
                                          frameEncodeHandle,
                                          out metadataHandle
                                          ));

                        PROPVARIANT propVar = new PROPVARIANT();

                        try
                        {
                            propVar.Init(metadata);

                            lock (metadata.SyncObject)
                            {
                                HRESULT.Check(UnsafeNativeMethods.WICMetadataQueryWriter.SetMetadataByName(
                                                  metadataHandle,
                                                  "/",
                                                  ref propVar
                                                  ));

                                // Helpful for debugging stress and remote dumps
                                _encodeState = EncodeState.FrameEncodeMetadataSet;
                            }
                        }
                        finally
                        {
                            propVar.Clear();
                        }
                    }
                }

                Int32Rect r = new Int32Rect();
                HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.WriteSource(
                                  frameEncodeHandle,
                                  outSourceHandle,
                                  ref r
                                  ));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.FrameEncodeSourceWritten;

                HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.Commit(
                                  frameEncodeHandle
                                  ));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.FrameEncodeCommitted;
            }
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        public void SetQuery(String query, object value)
        {
            WritePreamble();

            if (query == null)
            {
                throw new System.ArgumentNullException("query");
            }

            if (value == null)
            {
                throw new System.ArgumentNullException("value");
            }

            if (_readOnly)
            {
                throw new System.InvalidOperationException(SR.Get(SRID.Image_MetadataReadOnly));
            }

            // Store these for debugging stress failures.
            _setQueryString = query;
            _setQueryValue  = value;

            EnsureBitmapMetadata();

            PROPVARIANT propVar = new PROPVARIANT();

            try
            {
                propVar.Init(value);

                if (propVar.RequiresSyncObject)
                {
                    BitmapMetadata metadata = value as BitmapMetadata;
                    Invariant.Assert(metadata != null);

                    #pragma warning suppress 6506 // Invariant.Assert(metadata != null);
                    metadata.VerifyAccess();

                    #pragma warning suppress 6506 // Invariant.Assert(metadata != null);
                    lock (metadata._syncObject)
                    {
                        lock (_syncObject)
                        {
                            HRESULT.Check(UnsafeNativeMethods.WICMetadataQueryWriter.SetMetadataByName(
                                              _metadataHandle,
                                              query,
                                              ref propVar
                                              ));
                        }
                    }
                }
                else
                {
                    lock (_syncObject)
                    {
                        HRESULT.Check(UnsafeNativeMethods.WICMetadataQueryWriter.SetMetadataByName(
                                          _metadataHandle,
                                          query,
                                          ref propVar
                                          ));
                    }
                }
            }
            finally
            {
                propVar.Clear();
            }

            WritePostscript();
        }
 public static System.Windows.Media.Imaging.BitmapFrame Create(BitmapSource source, BitmapSource thumbnail, BitmapMetadata metadata, System.Collections.ObjectModel.ReadOnlyCollection <System.Windows.Media.ColorContext> colorContexts)
 {
     return(default(System.Windows.Media.Imaging.BitmapFrame));
 }