Example #1
0
        public PSFilterHost(BitmapSource sourceImage, System.Windows.Media.Color primary, System.Windows.Media.Color secondary, Region selectedRegion, IntPtr parentWindowHandle)
#endif
        {
            if (sourceImage == null)
            {
                throw new ArgumentNullException(nameof(sourceImage));
            }

            int imageWidth  = 0;
            int imageHeight = 0;

#if GDIPLUS
            imageWidth  = sourceImage.Width;
            imageHeight = sourceImage.Height;
#else
            imageWidth  = sourceImage.PixelWidth;
            imageHeight = sourceImage.PixelHeight;
#endif

            if (imageWidth > 32000 || imageHeight > 32000)
            {
                string message = string.Empty;
                if (imageWidth > 32000 && imageHeight > 32000)
                {
                    message = Resources.ImageSizeTooLarge;
                }
                else
                {
                    if (imageWidth > 32000)
                    {
                        message = Resources.ImageWidthTooLarge;
                    }
                    else
                    {
                        message = Resources.ImageHeightTooLarge;
                    }
                }

                throw new ImageSizeTooLargeException(message);
            }

#if GDIPLUS
            source = (Bitmap)sourceImage.Clone();
#else
            source = sourceImage.Clone();
#endif
            disposed         = false;
            dest             = null;
            filterParameters = null;
            primaryColor     = primary;
            secondaryColor   = secondary;
            if (selectedRegion != null)
            {
                this.selectedRegion = selectedRegion.Clone();
            }
            else
            {
                this.selectedRegion = null;
            }
            owner             = parentWindowHandle;
            pseudoResources   = null;
            abortFunc         = null;
            hostInfo          = null;
            hostColorProfiles = null;
            sessionSettings   = null;
        }
Example #2
0
        public bool RunFilter(PluginData pluginData, bool showUI)
        {
            if (pluginData == null)
            {
                throw new ArgumentNullException(nameof(pluginData));
            }

            if (disposed)
            {
                throw new ObjectDisposedException(nameof(PSFilterHost));
            }

            if (!showUI && filterParameters == null)
            {
                throw new InvalidOperationException(Resources.ParametersNotSet);
            }

            bool result = false;

            using (LoadPsFilter lps = new LoadPsFilter(source, primaryColor, secondaryColor, selectedRegion, owner))
            {
                if (abortFunc != null)
                {
                    lps.SetAbortFunc(abortFunc);
                }

                if (pickColor != null)
                {
                    lps.SetPickColor(pickColor);
                }

                if (UpdateProgress != null)
                {
                    lps.SetProgressFunc(new ProgressProc(OnFilterReportProgress));
                }

                if (filterParameters != null)
                {
                    lps.ParameterData = filterParameters;
                    lps.ShowUI        = showUI;
                }

                if (pseudoResources != null)
                {
                    lps.PseudoResources = pseudoResources;
                }

                if (hostInfo != null)
                {
                    lps.HostInformation = hostInfo;
                }

                if (hostColorProfiles != null)
                {
                    lps.SetColorProfiles(hostColorProfiles);
                }

                if (sessionSettings != null)
                {
                    lps.SetPluginSettings(sessionSettings);
                }

                try
                {
                    result = lps.RunPlugin(pluginData);
                }
                catch (FileNotFoundException)
                {
                    throw;
                }
                catch (SecurityException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (ex is OutOfMemoryException || ex is StackOverflowException || ex is ThreadAbortException)
                    {
                        throw;
                    }

                    throw new FilterRunException(ex.Message, ex);
                }

                if (result)
                {
#if GDIPLUS
                    dest = lps.Dest.ToGdipBitmap();
#else
                    dest = lps.Dest.ToBitmapSource();
                    dest.Freeze();
#endif

                    filterParameters = lps.ParameterData;
                    pseudoResources  = lps.PseudoResources;
                    hostInfo         = lps.HostInformation;
                    sessionSettings  = lps.GetPluginSettings();
                }
                else if (!string.IsNullOrEmpty(lps.ErrorMessage))
                {
                    throw new FilterRunException(lps.ErrorMessage);
                }
            }

            return(result);
        }