/// <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>
		/// <returns></returns>
		public bool Open(Stream stream, Guid previewHandler) {
			UnloadPreviewHandler();

			if (stream == null) {
				ErrorMessage = "No file loaded.";
				return false;
			}

			ErrorMessage = "";

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

						// need to instantiate a different COM type (file format has changed)
						if (mCurrentPreviewHandler != null) Marshal.FinalReleaseComObject(mCurrentPreviewHandler);

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

					if (mCurrentPreviewHandler is IInitializeWithStream) {
						// must wrap the stream to provide compatibility with IStream
						mCurrentPreviewHandlerStream = stream;
						StreamWrapper wrapped = new StreamWrapper(mCurrentPreviewHandlerStream);
						((IInitializeWithStream)mCurrentPreviewHandler).Initialize(wrapped, 0);
					}

					if (mCurrentPreviewHandler is IPreviewHandler) {
						// bind the preview handler to the control's bounds and preview the content
						Rectangle r = ClientRectangle;
						((IPreviewHandler)mCurrentPreviewHandler).SetWindow(Handle, ref r);
						((IPreviewHandler)mCurrentPreviewHandler).DoPreview();

						return true;
					}
				}
				catch (Exception ex) {
					ErrorMessage = "Preview could not be generated.\n" + ex.Message;
				}
			}
			else {
				ErrorMessage = "No preview available.";
			}

			return false;
		}
        /// <summary>
        /// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost.
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool Open(string filename, out Guid?previewGuid)
        {
            UnloadPreviewHandler();


            if (String.IsNullOrEmpty(filename))
            {
                ErrorMessage = "No file loaded.";
                previewGuid  = null;
                return(false);
            }

            // try to get GUID for the preview handler
            Guid guid = GetPreviewHandlerGUID(filename);

            ErrorMessage = "";
            previewGuid  = guid;
            if (guid != Guid.Empty)
            {
                try {
                    if (guid != mCurrentPreviewHandlerGUID)
                    {
                        mCurrentPreviewHandlerGUID = guid;

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

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

                    if (mCurrentPreviewHandler is IInitializeWithFile)
                    {
                        // some handlers accept a filename
                        ((IInitializeWithFile)mCurrentPreviewHandler).Initialize(filename, 0);
                    }
                    else if (mCurrentPreviewHandler is IInitializeWithStream)
                    {
                        if (File.Exists(filename))
                        {
                            // other handlers want an IStream (in this case, a file stream)
                            mCurrentPreviewHandlerStream = File.Open(filename, FileMode.Open, FileAccess.Read);
                            StreamWrapper stream = new StreamWrapper(mCurrentPreviewHandlerStream);
                            ((IInitializeWithStream)mCurrentPreviewHandler).Initialize(stream, 0);
                        }
                        else
                        {
                            ErrorMessage = "File not found.";
                        }
                    }
                    else if (mCurrentPreviewHandler 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)mCurrentPreviewHandler).Initialize(shellItem, 0);
                    }

                    if (mCurrentPreviewHandler is IPreviewHandler)
                    {
                        // bind the preview handler to the control's bounds and preview the content
                        Rectangle r = ClientRectangle;
                        ((IPreviewHandler)mCurrentPreviewHandler).SetWindow(Handle, ref r);
                        ((IPreviewHandler)mCurrentPreviewHandler).DoPreview();

                        return(true);
                    }
                }
                catch (Exception ex) {
                    ErrorMessage = "Preview could not be generated.\n" + ex.Message;
                }
            }
            else
            {
                ErrorMessage = "No preview available.";
            }

            return(false);
        }
		/// <summary>
		/// Opens the specified file using the appropriate preview handler and displays the result in this PreviewHandlerHost.
		/// </summary>
		/// <param name="filename"></param>
		/// <returns></returns>
		public bool Open(string filename, out Guid? previewGuid) {
			UnloadPreviewHandler();


			if (String.IsNullOrEmpty(filename)) {
				ErrorMessage = "No file loaded.";
				previewGuid = null;
				return false;
			}

			// try to get GUID for the preview handler
			Guid guid = GetPreviewHandlerGUID(filename);
			ErrorMessage = "";
			previewGuid = guid;
			if (guid != Guid.Empty) {
				try {
					if (guid != mCurrentPreviewHandlerGUID) {
						mCurrentPreviewHandlerGUID = guid;

						// need to instantiate a different COM type (file format has changed)
						if (mCurrentPreviewHandler != null) Marshal.FinalReleaseComObject(mCurrentPreviewHandler);

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

					if (mCurrentPreviewHandler is IInitializeWithFile) {
						// some handlers accept a filename
						((IInitializeWithFile)mCurrentPreviewHandler).Initialize(filename, 0);
					}
					else if (mCurrentPreviewHandler is IInitializeWithStream) {
						if (File.Exists(filename)) {
							// other handlers want an IStream (in this case, a file stream)
							mCurrentPreviewHandlerStream = File.Open(filename, FileMode.Open, FileAccess.Read);
							StreamWrapper stream = new StreamWrapper(mCurrentPreviewHandlerStream);
							((IInitializeWithStream)mCurrentPreviewHandler).Initialize(stream, 0);
						}
						else {
							ErrorMessage = "File not found.";
						}
					}
					else if (mCurrentPreviewHandler 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)mCurrentPreviewHandler).Initialize(shellItem, 0);
					}

					if (mCurrentPreviewHandler is IPreviewHandler) {
						// bind the preview handler to the control's bounds and preview the content
						Rectangle r = ClientRectangle;
						((IPreviewHandler)mCurrentPreviewHandler).SetWindow(Handle, ref r);
						((IPreviewHandler)mCurrentPreviewHandler).DoPreview();

						return true;
					}
				}
				catch (Exception ex) {
					ErrorMessage = "Preview could not be generated.\n" + ex.Message;
				}
			}
			else {
				ErrorMessage = "No preview available.";
			}

			return false;
		}
Example #4
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>
        /// <returns></returns>
        public bool Open(Stream stream, Guid previewHandler)
        {
            UnloadPreviewHandler();

            if (stream == null)
            {
                ErrorMessage = "No file loaded.";
                return(false);
            }

            ErrorMessage = "";

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

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

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

                    if (mCurrentPreviewHandler is IInitializeWithStream)
                    {
                        // must wrap the stream to provide compatibility with IStream
                        mCurrentPreviewHandlerStream = stream;
                        StreamWrapper wrapped = new StreamWrapper(mCurrentPreviewHandlerStream);
                        ((IInitializeWithStream)mCurrentPreviewHandler).Initialize(wrapped, 0);
                    }

                    if (mCurrentPreviewHandler is IPreviewHandler)
                    {
                        // bind the preview handler to the control's bounds and preview the content
                        Rectangle r = ClientRectangle;
                        ((IPreviewHandler)mCurrentPreviewHandler).SetWindow(Handle, ref r);
                        ((IPreviewHandler)mCurrentPreviewHandler).DoPreview();

                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    ErrorMessage = "Preview could not be generated.\n" + ex.Message;
                }
            }
            else
            {
                ErrorMessage = "No preview available.";
            }

            return(false);
        }