Exemple #1
0
        /// <summary>
        /// Opens the specified stream using the preview handler COM type with the provided GUID and displays the result in this PreviewHandlerHost.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="previewHandler"></param>
        /// <param name="clientRectangle"> </param>
        /// <param name="handle"> </param>
        /// <returns></returns>
        public string Open(Stream stream, Guid previewHandler, Rectangle clientRectangle, IntPtr handle)
        {
            UnloadPreviewHandler();

            if (stream == null)
            {
                return "The file could not be previewed.";
            }

            if (previewHandler != Guid.Empty)
            {
                try
                {
                    if (previewHandler != _currentPreviewHandlerGUID)
                    {
                        _currentPreviewHandlerGUID = previewHandler;

                        // need to instantiate a different COM type (file format has changed)
                        if (_currentPreviewHandler != null)
                        {
                            Marshal.FinalReleaseComObject(_currentPreviewHandler);
                            GC.Collect();
                        }

                        // use reflection to instantiate the preview handler type
                        Type comType = Type.GetTypeFromCLSID(_currentPreviewHandlerGUID);
                        _currentPreviewHandler = Activator.CreateInstance(comType);
                    }

                    var currentPreviewHandler = _currentPreviewHandler as IInitializeWithStream;
                    if (currentPreviewHandler != null)
                    {
                        // must wrap the stream to provide compatibility with IStream
                        _currentPreviewHandlerStream = stream;
                        var wrapped = new StreamWrapper(_currentPreviewHandlerStream);
                        (currentPreviewHandler).Initialize(wrapped, 0);
                    }

                    var handler = _currentPreviewHandler as IPreviewHandler;
                    if (handler != null)
                    {
                        // bind the preview handler to the control's bounds and preview the content
                        Rectangle r = clientRectangle;
                        (handler).SetWindow(handle, ref r);
                        (handler).DoPreview();

                        return "success";
                    }
                }
                catch (Exception ex)
                {
                    return "The file could not be previewed.\n" + ex.Message;
                }
            }
            else
            {
                return "No preview is registered for this type of file.";
            }

            return "failure";
        }
Exemple #2
0
        /// <summary>
        /// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="clientRectangle"> </param>
        /// <param name="handle"> </param>
        /// <returns></returns>
        public string Open(string filename,Rectangle clientRectangle, IntPtr handle)
        {
            _status = HandlerStatus.None;
            UnloadPreviewHandler();

            if (String.IsNullOrEmpty(filename))
            {
                return "No file loaded";
            }
            if (!File.Exists(filename))
            {
                return "The file was not found";
            }

            // try to get GUID for the preview handler
            Guid guid = GetPreviewHandlerGUID(filename);
            if (guid != Guid.Empty)
            {
                _status = HandlerStatus.FoundGuid;
                try
                {
                    if (guid != _currentPreviewHandlerGUID)
                    {
                        _currentPreviewHandlerGUID = guid;

                        // need to instantiate a different COM type (file format has changed)
                        if (_currentPreviewHandler != null)
                        {
                            Marshal.FinalReleaseComObject(_currentPreviewHandler);
                            GC.Collect();
                        }
                        // use reflection to instantiate the preview handler type
                        Type comType = Type.GetTypeFromCLSID(_currentPreviewHandlerGUID);
                        _currentPreviewHandler = Activator.CreateInstance(comType);
                    }
                    _status = HandlerStatus.Instantiated;
                    //try stream first
                    if (_currentPreviewHandler is IInitializeWithStream)
                    {
                        // some handlers want an IStream (in this case, a file stream)
                        _currentPreviewHandlerStream = new MemoryStream();
                        using (var fs = File.Open(filename, FileMode.Open, FileAccess.Read))
                        {
                            fs.CopyTo(_currentPreviewHandlerStream);
                        }
                        var stream = new StreamWrapper(_currentPreviewHandlerStream);
                        ((IInitializeWithStream)_currentPreviewHandler).Initialize(stream, 0);
                    }
                    else if (_currentPreviewHandler is IInitializeWithFile)
                    {
                        // some handlers accept a filename
                        ((IInitializeWithFile)_currentPreviewHandler).Initialize(filename, 0);
                    }
                    else if (_currentPreviewHandler is IInitializeWithItem)
                    {
                        // a third category exists, must be initialised with a shell item
                        IShellItem shellItem;
                        SHCreateItemFromParsingName(filename, IntPtr.Zero, new Guid(GUID_ISHELLITEM), out shellItem);
                        ((IInitializeWithItem)_currentPreviewHandler).Initialize(shellItem, 0);
                    }
                    _status = HandlerStatus.Initialized;
                    var currentPreviewHandler = _currentPreviewHandler as IPreviewHandler;
                    if (currentPreviewHandler != null)
                    {
                        // bind the preview handler to the control's bounds and preview the content
                        Rectangle r = clientRectangle;
                        (currentPreviewHandler).SetWindow(handle, ref r);
                        Application.DoEvents();
                        (currentPreviewHandler).DoPreview();

                        return "success";
                    }
                }
                catch (Exception ex)
                {
                    _status = HandlerStatus.Failed;
                    return "The file could not be previewed\n" + ex.Message;
                }
            }
            else
            {
                return "No previewer is registered for this type of file";
            }
            return "Unable to preview file";
        }