private void HandleBeginRequest(object sender, EventArgs e)
        {
            var application = (HttpApplication)sender;
            var context = application.Context;
            var relativeFilePath = context.Request.AppRelativeCurrentExecutionFilePath.TrimStart('~');

            // Check whether the current request URL contains information about a preview mode
            if (VirtualContext.HandleVirtualContext(ref relativeFilePath))
            {
                // Validate integrity of preview information (including the unique identifier generated by Kentico) in the current request URL
                if (!VirtualContext.ValidatePreviewHash(relativeFilePath) || !ValidatePreviewGuid(VirtualContext.GetItem(VirtualContext.PARAM_WF_GUID)))
                {
                    VirtualContext.Reset();
                    throw new HttpException(404, "The preview link is not valid.");
                }

                // Disable same origin policy for a preview mode as Kentico displays preview in a frame
                AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

                // Do not cache response in a preview mode, it has to contain current data
                context.Response.Cache.SetNoServerCaching();
                context.Response.Cache.SetNoStore();

                // Remove preview mode information from the request URL
                context.RewritePath("~" + relativeFilePath, context.Request.PathInfo, context.Request.Url.Query.TrimStart('?'));
            }
            else
            {
                // Add validation callback for the output cache item to ignore it in a preview mode
                context.Response.Cache.AddValidationCallback(ValidateCacheItem, null);
            }

            var previewFeature = new PreviewFeature();
            context.Kentico().SetFeature<IPreviewFeature>(previewFeature);
        }
Beispiel #2
0
        private void RefreshThumbnail(Scan scan)
        {
            PreviewType    previewType = PreviewType.FullFrame;
            PreviewFeature feature     = null;

            if (boxPreviewFeature.Created)
            {
                boxPreviewFeature.Invoke((MethodInvoker) delegate
                {
                    previewType = _ActivePreviewType;
                    if (boxPreviewFeature.SelectedIndex > 0)
                    {
                        feature = (PreviewFeature)boxPreviewFeature.SelectedItem;
                    }
                });
            }
            RefreshThumbnailAsync(scan, previewType, feature);
        }
Beispiel #3
0
 private void FillboxPreviewFeature()
 {
     boxPreviewFeature.Items.Add("<None>");
     if (_CompiledFeatures != null)
     {
         foreach (var wz in _CompiledFeatures.CWatchZones)
         {
             foreach (var w in wz.CWatches)
             {
                 foreach (var wi in w.CWatchImages)
                 {
                     var name = wz.Name + "/" + w.Name + " - " + wi.Name;
                     var pf   = new PreviewFeature(name, wz, w, wi);
                     boxPreviewFeature.Items.Add(pf);
                 }
             }
         }
     }
     boxPreviewFeature.SelectedIndex = 0;
 }
Beispiel #4
0
        // @TODO: Break into separate methods?
        private async void RefreshThumbnailAsync(Scan scan, PreviewType previewType, PreviewFeature feature)
        {
            await Task.Delay(0);

            try
            {
                Geometry    minGeo;
                MagickImage mi;
                string      confidence = string.Empty;
                Geometry    cropGeo    = _CropGeometry;

                if (previewType == PreviewType.FullFrame)
                {
                    minGeo = Geometry.Min(_VideoGeometry, GetScaledGeometry(_VideoGeometry));
                    mi     = new MagickImage(scan.CurrentFrame.Bitmap);

                    if (!_Selecting)
                    {
                        var roundGeo = cropGeo.Round();
                        mi.Composite(_ScreenOverlay, (int)roundGeo.X, (int)roundGeo.Y, CompositeOperator.Over);
                    }
                    else
                    {
                        var numRect = NumGeometry.ToRectangle();
                        using (var overlay = new MagickImage(SCREEN_COLOR, numRect.Width, numRect.Height))
                        {
                            mi.Composite(overlay, numRect.X, numRect.Y, CompositeOperator.Over);
                        }
                    }
                }
                else if (previewType == PreviewType.FrameCrop)
                {
                    minGeo = Geometry.Min(cropGeo, GetScaledGeometry(cropGeo));
                    if (!_VideoGeometry.Contains(cropGeo))
                    {
                        mi = new MagickImage(scan.CurrentFrame.Bitmap);
                        mi.Extent(cropGeo.ToMagick(), STANDARD_GRAVITY, EXTENT_COLOR);
                    }
                    else
                    {
                        mi = new MagickImage(scan.CurrentFrame.Bitmap.Clone(cropGeo.ToRectangle(), PixelFormat.Format24bppRgb));
                    }

                    if (!_Selecting)
                    {
                        var roundGeo     = cropGeo.Round();
                        var roundTrueGeo = _TrueCropGeometry.Round();
                        int xOffset      = (int)(roundTrueGeo.X - cropGeo.X);
                        int yOffset      = (int)(roundTrueGeo.Y - cropGeo.Y);
                        mi.Composite(_WatchZoneOverlay, xOffset, yOffset, CompositeOperator.Over);
                    }
                }
                else if (previewType == PreviewType.Features && feature == null)
                {
                    var trueGeo = _TrueCropGeometry;

                    minGeo = Geometry.Min(trueGeo, GetScaledGeometry(trueGeo));
                    if (!_VideoGeometry.Contains(trueGeo))
                    {
                        mi = new MagickImage(scan.CurrentFrame.Bitmap);
                        mi.Extent(trueGeo.ToMagick(), STANDARD_GRAVITY, EXTENT_COLOR);
                    }
                    else
                    {
                        mi = new MagickImage(scan.CurrentFrame.Bitmap.Clone(trueGeo.ToRectangle(), PixelFormat.Format24bppRgb));
                    }

                    if (!_Selecting)
                    {
                        mi.Composite(_WatchZoneOverlay, CompositeOperator.Over);
                    }
                }
                else if (previewType == PreviewType.Features && feature != null)
                {
                    var wzGeo = feature.WatchZone.Geometry;

                    if (!ckbShowComparison.Checked)
                    {
                        var baseMGeo = new MagickGeometry(64, 64, (int)Math.Round(wzGeo.Width), (int)Math.Round(wzGeo.Height));

                        wzGeo.Adjust(-64, -64, 128, 128);

                        minGeo = Geometry.Min(wzGeo, GetScaledGeometry(wzGeo));
                        if (!_VideoGeometry.Contains(wzGeo))
                        {
                            mi = new MagickImage(scan.CurrentFrame.Bitmap);
                            mi.Extent(wzGeo.ToMagick(), STANDARD_GRAVITY, EXTENT_COLOR);
                        }
                        else
                        {
                            mi = new MagickImage(scan.CurrentFrame.Bitmap.Clone(wzGeo.ToRectangle(), PixelFormat.Format24bppRgb));
                        }

                        using (var baseM = new MagickImage(
                                   MagickColors.Transparent,
                                   baseMGeo.Width,
                                   baseMGeo.Height))
                            using (var overlay = new MagickImage(
                                       PREVIEW_EXTENT_COLOR,
                                       baseMGeo.Width + 128,
                                       baseMGeo.Height + 128))
                            {
                                overlay.Composite(baseM, new PointD(baseMGeo.X, baseMGeo.Y), CompositeOperator.Alpha);
                                mi.Composite(overlay, CompositeOperator.Atop);
                            }
                    }
                    else
                    {
                        minGeo = Geometry.Min(wzGeo, GetScaledGeometry(wzGeo));
                        if (!_VideoGeometry.Contains(wzGeo))
                        {
                            mi = new MagickImage(scan.CurrentFrame.Bitmap);
                            mi.Extent(wzGeo.ToMagick(), STANDARD_GRAVITY, EXTENT_COLOR);
                        }
                        else
                        {
                            mi = new MagickImage(scan.CurrentFrame.Bitmap.Clone(wzGeo.ToRectangle(), PixelFormat.Format24bppRgb));
                        }

                        // @TODO: Add previous frame stuff
                        using (var deltaImage = feature.WatchImage.MagickImage.Clone())
                        {
                            mi.ColorSpace         = feature.Watcher.ColorSpace;
                            deltaImage.ColorSpace = feature.Watcher.ColorSpace;
                            if (feature.WatchImage.HasAlpha)
                            {
                                mi.Composite(feature.WatchImage.AlphaChannel, CompositeOperator.Over);
                            }
                            if (feature.Watcher.Equalize)
                            {
                                mi.Equalize();
                            }
                            confidence = mi.Compare(deltaImage, feature.Watcher.ErrorMetric).ToString("F4");
                            mi.Composite(deltaImage, CompositeOperator.Difference);
                        }
                    }
                }
                else
                {
                    throw new InvalidEnumArgumentException("PreviewType out of bounds? This shouldn't happen.");
                }

                var drawingSize = minGeo.Size.ToDrawing();
                if (mi.Width > drawingSize.Width || mi.Height > drawingSize.Height)
                {
                    var mGeo = minGeo.ToMagick();
                    mGeo.IgnoreAspectRatio = false;
                    //mi.ColorSpace = ColorSpace.HCL;
                    mi.FilterType = DEFAULT_SCALE_FILTER;
                    mi.Resize(mGeo);
                }
                UpdatepictureBox(drawingSize, mi.ToBitmap(), confidence);
            }
            catch (Exception e)
            {
                Log.Error(e, "Thumbnail failed to render for the Scan Region.");
                scan.Dispose();
                _RenderingFrame = false;
            }
        }