Ejemplo n.º 1
0
        // Receive an event containing new image processing parameters.
        // Store these parameters and then try to update the image
        public async void AdjustImage_EventHandler(object sender, ImageAdjusterEventArgs e)
        {
            if (e == null)
            {
                // Shouldn't happen, but...
                return;
            }

            string path = DataEntryHandler.TryGetFilePathFromGlobalDataHandler();

            if (String.IsNullOrEmpty(path))
            {
                // The file cannot be opened or is not displayable.
                // Signal change in image state, which essentially says there is no displayable image to adjust (consumed by ImageAdjuster)
                this.OnImageStateChanged(new ImageStateEventArgs(false)); //  Signal change in image state (consumed by ImageAdjuster)
                return;
            }

            if (e.OpenExternalViewer)
            {
                // The event says to open an external photo viewer. Try to do so.
                // Note that we don't do any image processing on this event if if this is the case.
                if (ProcessExecution.TryProcessStart(path) == false)
                {
                    // Can't open the image file with an external view. Note that file must exist at this point as we checked for that above.
                    Dialogs.MarkableCanvasCantOpenExternalPhotoViewerDialog(Util.GlobalReferences.MainWindow, Path.GetExtension(path));
                }
                return;
            }

            // Process the image based on the current image processing arguments.
            if (e.ForceUpdate == false && (e.Contrast == this.lastContrast && e.Brightness == this.lastBrightness && e.DetectEdges == this.lastDetectEdges && e.Sharpen == this.lastSharpen && e.UseGamma == this.lastUseGamma && e.GammaValue == this.lastGammaValue))
            {
                // If there is no change from the last time we processed an image, abort as it would not make any difference to what the user sees
                return;
            }
            this.contrast    = e.Contrast;
            this.brightness  = e.Brightness;
            this.detectEdges = e.DetectEdges;
            this.sharpen     = e.Sharpen;
            this.useGamma    = e.UseGamma;
            this.gammaValue  = e.GammaValue;
            this.timerImageProcessingUpdate.Start();
            await UpdateAndProcessImage().ConfigureAwait(true);
        }
Ejemplo n.º 2
0
        // Update the image according to the image processing parameters.
        private async Task UpdateAndProcessImage()
        {
            // If its processing the image, try again later (via the time),
            if (this.Processing)
            {
                return;
            }
            try
            {
                string path = DataEntryHandler.TryGetFilePathFromGlobalDataHandler();;
                if (String.IsNullOrEmpty(path))
                {
                    // If we cannot get a valid file, there is no image to manipulate.
                    // So abort and signal a change in image state that says there is no displayable image to adjust (consumed by ImageAdjuster)
                    this.OnImageStateChanged(new ImageStateEventArgs(false));
                }

                // Set the state to Processing is used to indicate that other attempts to process the image should be aborted util this is done.
                this.Processing = true;
                using (MemoryStream imageStream = new MemoryStream(File.ReadAllBytes(path)))
                {
                    // Remember the currently selected image processing states, so we can compare them later for changes
                    this.lastBrightness  = this.brightness;
                    this.lastContrast    = this.contrast;
                    this.lastSharpen     = this.sharpen;
                    this.lastDetectEdges = this.detectEdges;
                    this.lastUseGamma    = this.useGamma;
                    this.lastGammaValue  = this.gammaValue;
                    BitmapFrame bf = await ImageProcess.StreamToImageProcessedBitmap(imageStream, this.brightness, this.contrast, this.sharpen, this.detectEdges, this.useGamma, this.gammaValue).ConfigureAwait(true);

                    if (bf != null)
                    {
                        this.ImageToDisplay.Source = await ImageProcess.StreamToImageProcessedBitmap(imageStream, this.brightness, this.contrast, this.sharpen, this.detectEdges, this.useGamma, this.gammaValue).ConfigureAwait(true);
                    }
                }
            }
            catch
            {
                // We failed on this image. To avoid this happening again,
                // Signal change in image state, which essentially says there is no adjustable image (consumed by ImageAdjuster)
                this.OnImageStateChanged(new ImageStateEventArgs(false));
            }
            this.Processing = false;
        }