Exemple #1
0
 private void DrawQuestion6()
 {
     try
     {
         AnswerVariant_7.Visibility = AnswerVariant_8.Visibility = Visibility.Hidden;
         AnswerMarginLeft.Width     = 350;
         BitmapImage bitmap = new BitmapImage();
         bitmap.BeginInit();
         bitmap.UriSource = new Uri($"{_TEST_.DirPath}{_TEST_.CurrentQuestionNum}\\Q.png");
         bitmap.EndInit();
         CroppedBitmap cb = new CroppedBitmap(bitmap, new Int32Rect(0, 0, 340, 218));
         imgQuestion.Source = cb;
         string imgName = "AnswerVariant_";
         for (int i = 1, j = 4, f = 1, l = 2; i < 4; i++, j++, f += 2, l += 2)
         {
             int t = 149;
             if (bitmap.Width < 429)
             {
                 t = 148;
             }
             (FindName(imgName + f) as Image).Source = new CroppedBitmap(bitmap, new Int32Rect((i - 1) * 140, 245, t, 80));
             (FindName(imgName + l) as Image).Source = new CroppedBitmap(bitmap, new Int32Rect((i - 1) * 140, 358, t, 80));
         }
         beforeSelection.BorderBrush = null;
     }
     catch (Exception ex)
     {
         ExceptionForMail.ExceptionList.Add(new myException(this.GetType().Name, 305, ex.Message, System.Reflection.MethodInfo.GetCurrentMethod().Name));
     }
 }
Exemple #2
0
        private void Go_Click(object sender, RoutedEventArgs e)
        {
            if (image1.Source != null)
            {
                Rect rect1       = new Rect(Canvas.GetLeft(selectionRectangle), Canvas.GetTop(selectionRectangle), selectionRectangle.Width, selectionRectangle.Height);
                var  img         = image1.Source as BitmapSource;
                var  scaleWidth  = (img.PixelWidth) / (image1.ActualWidth);
                var  scaleHeight = (img.PixelHeight) / (image1.ActualHeight);

                var rcFrom = new Int32Rect()
                {
                    X      = (int)(rect1.X * scaleWidth),
                    Y      = (int)(rect1.Y * scaleHeight),
                    Width  = (int)(rect1.Width * scaleWidth),
                    Height = (int)(rect1.Height * scaleHeight)
                };


                BitmapSource bs = new CroppedBitmap(image1.Source as BitmapSource, rcFrom);

                var cropped = new CroppedBitmap(inputImage, rcFrom);

                croppedImage  = GetJpgImage(bs);
                image2.Source = croppedImage;
            }
        }
Exemple #3
0
        public override object CropBitmap(object handle, int srcX, int srcY, int w, int h)
        {
            var oldImg = (SWMI.BitmapSource)DataConverter.AsImageSource(handle);
            var bmp    = new CroppedBitmap(oldImg, new Int32Rect(srcX, srcY, w, h));

            return(new WpfImage(bmp));
        }
Exemple #4
0
        /// <summary>Creates the screenshot of entire plotter element</summary>
        /// <returns></returns>
        internal static BitmapSource CreateScreenshot(UIElement uiElement, Int32Rect screenshotSource)
        {
            Window window = Window.GetWindow(uiElement);

            if (window == null)
            {
                return(CreateElementScreenshot(uiElement));
            }
            Size size = window.RenderSize;

            //double dpiCoeff = 32 / SystemParameters.CursorWidth;
            //int dpi = (int)(dpiCoeff * 96);
            double dpiCoeff = 1;
            int    dpi      = 96;

            RenderTargetBitmap bmp = new RenderTargetBitmap(
                (int)(size.Width * dpiCoeff), (int)(size.Height * dpiCoeff),
                dpi, dpi, PixelFormats.Default);

            // white background
            Rectangle whiteRect = new Rectangle {
                Width = size.Width, Height = size.Height, Fill = Brushes.White
            };

            whiteRect.Measure(size);
            whiteRect.Arrange(new Rect(size));
            bmp.Render(whiteRect);
            // the very element
            bmp.Render(uiElement);

            CroppedBitmap croppedBmp = new CroppedBitmap(bmp, screenshotSource);

            return(croppedBmp);
        }
        public static ImageSource GetImage(int statusId, bool use2x)
        {
            if (statusId == 0)
            {
                return(null);
            }

            var dic = use2x ? IconCollection2x : IconCollection;
            var img = use2x ? IconBitmap2x : IconBitmap;
            var pos = use2x ? IconPosition2x : IconPosition;

            lock (dic)
            {
                if (dic.ContainsKey(statusId))
                {
                    return(dic[statusId]);
                }
                else
                {
                    try
                    {
                        var image = new CroppedBitmap(img, pos[statusId]);
                        dic.Add(statusId, image);

                        return(image);
                    }
                    catch
                    {
                        return(null);
                    }
                }
            }
        }
Exemple #6
0
        public BitmapSource Convert(Bitmap bitmap)
        {
            var bitmapData   = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
            var bitmapSource = BitmapSource.Create(bitmapData.Width, bitmapData.Height, 96, 96, PixelFormats.Bgr32, null, bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);

            bitmap.UnlockBits(bitmapData);
            bitmapSource.Freeze();             // make it readable on any thread

            // crop border
            var cropLeft   = Math.Max(0, CropLeft);
            var cropTop    = Math.Max(0, CropTop);
            var cropRight  = Math.Max(0, CropRight);
            var cropBottom = Math.Max(0, CropBottom);

            if (bitmapSource.PixelWidth - cropLeft - cropRight <= 0)
            {
                throw new CropRectangleOutOfRangeException("With a width of " + bitmapSource.PixelWidth + ", left crop of " + cropLeft + " and right crop of " + cropRight + ", there is no surface left to grab.");
            }
            if (bitmapSource.PixelHeight - cropTop - cropBottom <= 0)
            {
                throw new CropRectangleOutOfRangeException("With a height of " + bitmapSource.PixelHeight + ", top crop of " + cropTop + " and bottom crop of " + cropBottom + ", there is no surface left to grab.");
            }
            var rect = new Int32Rect(cropLeft, cropTop, bitmapSource.PixelWidth - cropLeft - cropRight, bitmapSource.PixelHeight - cropTop - cropBottom);

            var img = new CroppedBitmap(bitmapSource, rect);

            img.Freeze();
            return(img);
        }
        public List <CroppedBitmap> CutImages()
        {
            List <CroppedBitmap> ReturnList = new List <CroppedBitmap>();

            for (int y = 0; y < RowsCount; y++)
            {
                for (int x = 0; x < ColumnsCount; x++)
                {
                    Vector CropStartPos = new Vector(StartPos.X + (x * WidthPerColumn), StartPos.Y + (y * HeightPerRow));
                    Vector CropEndPos   = new Vector(StartPos.X + ((x + 1) * WidthPerColumn), StartPos.Y + ((y + 1) * HeightPerRow));

                    CroppedBitmap CropBitmap = new CroppedBitmap(CurrentSpritesheet,
                                                                 new Int32Rect(
                                                                     Convert.ToInt32(CropStartPos.X),
                                                                     Convert.ToInt32(CropStartPos.Y),
                                                                     Convert.ToInt32(CropEndPos.X - CropStartPos.X),
                                                                     Convert.ToInt32(CropEndPos.Y - CropStartPos.Y)
                                                                     ));

                    ReturnList.Add(CropBitmap);
                }
            }

            return(ReturnList);
        }
        Color GetColorFromImage(Point p)
        {
            try
            {
                Rect bounds            = VisualTreeHelper.GetDescendantBounds(this);
                RenderTargetBitmap rtb = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Default);
                rtb.Render(this);

                byte[]           arr;
                PngBitmapEncoder png = new PngBitmapEncoder();
                png.Frames.Add(BitmapFrame.Create(rtb));
                using (var stream = new System.IO.MemoryStream())
                {
                    png.Save(stream);
                    arr = stream.ToArray();
                }

                BitmapSource bitmap = BitmapFrame.Create(new System.IO.MemoryStream(arr));

                byte[]        pixels = new byte[4];
                CroppedBitmap cb     = new CroppedBitmap(bitmap, new Int32Rect((int)p.X, (int)p.Y, 1, 1));
                cb.CopyPixels(pixels, 4, 0);
                return(Color.FromArgb(pixels[3], pixels[2], pixels[1], pixels[0]));
            }
            catch (Exception)
            {
                return(this.ColorBox.Color);
            }
        }
Exemple #9
0
        /// <summary>
        /// Gets the color of the pixel using the custom PixelColor .
        /// </summary>
        /// <param name="bitmapSource">The bitmap source.</param>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns>PixelColor</returns>
        /// <exception cref="PixelPicker.Helpers.OutOfBoundsException"></exception>

        public static PixelColor GetPixelColorUsingPixelColor(this BitmapImage bitmapSource, int x, int y)
        {
            if (bitmapSource == null)
            {
                throw new Exception(PixelPicker.BitmapImageException);
            }

            if (x < 0 || x > bitmapSource.PixelWidth - 1 || y < 0 || y > bitmapSource.PixelHeight - 1)
            {
                throw new OutOfBoundsException();
            }

            var cb = new CroppedBitmap(bitmapSource, new Int32Rect(x, y, 1, 1));

            byte[] pixels = new byte[4];
            cb.CopyPixels(pixels, 4, 0);

            cb = null;

            var a = pixels[3];
            var r = pixels[2];
            var g = pixels[1];
            var b = pixels[0];

            return(new PixelColor(a, r, g, b));
        }
Exemple #10
0
        public static byte[] GetCroppedImageData(RegionCapture regionCapture)
        {
            BitmapSource bitmapSource = new CroppedBitmap(regionCapture.ScreenCapture, new Int32Rect((int)regionCapture.Position.X, (int)regionCapture.Position.Y,
                                                                                                     (int)regionCapture.SelectedRegion.Width, (int)regionCapture.SelectedRegion.Height));

            return(Helper.GetMemoryFromBitmapSource(bitmapSource));
        }
Exemple #11
0
        private void PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.SystemKey == Key.PrintScreen &&
                e.KeyboardDevice.Modifiers == ModifierKeys.Alt &&
                (sender as Window).WindowState != WindowState.Maximized)
            {
                try
                {
                    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(
                        (int)(sender as Window).ActualWidth,
                        (int)(sender as Window).ActualHeight,
                        96, 96, System.Windows.Media.PixelFormats.Pbgra32);

                    renderTargetBitmap.Render(sender as Window);

                    CroppedBitmap crop = new CroppedBitmap(renderTargetBitmap,
                                                           new Int32Rect(7, 7,
                                                                         (int)(sender as Window).ActualWidth - 14,
                                                                         (int)(sender as Window).ActualHeight - 14));

                    Clipboard.SetImage(crop);
                }
                catch (Exception) { }

                GC.Collect();
                e.Handled = true;
            }
        }
Exemple #12
0
        private static WriteableBitmap CropFrame(ref Rectangle srcRect, BitmapSource source)
        {
            var x = Math.Min(srcRect.X, source.PixelWidth - 1);
            var y = Math.Min(srcRect.Y, source.PixelHeight - 1);

            x = Math.Max(x, 0);
            y = Math.Max(y, 0);

            var right = srcRect.X + srcRect.Width;

            right = Math.Min(right, source.PixelWidth);
            right = Math.Max(right, 0);

            var bottom = srcRect.Y + srcRect.Height;

            bottom = Math.Min(bottom, source.PixelHeight);
            bottom = Math.Max(bottom, 0);

            var crop = new CroppedBitmap(source, new Int32Rect(x, y, right - x, bottom - y));

            crop.Freeze();

            var bmp = BitmapFactory.ConvertToPbgra32Format(crop);

            return(bmp);
        }
Exemple #13
0
        private void Crop(object obj)
        {
            System.Windows.Controls.Image croppedImage = new System.Windows.Controls.Image();
            croppedImage.Width  = 200;
            croppedImage.Margin = new Thickness(5);
            CroppedBitmap cb = new CroppedBitmap();

            if (CropSelected.Contains("First"))
            {
                cb = new CroppedBitmap(ImagePath, new Int32Rect(0, 0, 50, 50));
            }
            else if (CropSelected.Contains("Second"))
            {
                cb = new CroppedBitmap(ImagePath, new Int32Rect(0, 0, 100, 100));
            }
            else
            {
                cb = new CroppedBitmap(ImagePath, new Int32Rect(0, 0, 200, 200));
            }
            croppedImage.Source = cb;
            string path = Path.GetTempPath() + "temp.jpeg";

            saveCroppedBitmap(cb, path);
            BitmapImage bitmap = new BitmapImage();

            bitmap.BeginInit();
            bitmap.UriSource = new Uri(path);
            bitmap.EndInit();
            ImagePath = bitmap;
        }
        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            if (imageBox1.Source != null)
            {
                var ratioX = imageBox1.Source.Width / imageBox1.ActualWidth;
                var ratioY = imageBox1.Source.Height / imageBox1.ActualHeight;

                Int32Rect rcFrom = new Int32Rect
                {
                    X      = (int)(Canvas.GetLeft(rect1) * ratioX),
                    Y      = (int)(Canvas.GetTop(rect1) * ratioY),
                    Width  = (int)(rect1.Width * ratioX),
                    Height = (int)(rect1.Height * ratioY)
                };

                var crop = new CroppedBitmap(imageBox1.Source as BitmapSource, rcFrom);
                imageBox2.Source = crop;
                BitmapEncoder pngEncoder = new PngBitmapEncoder();
                pngEncoder.Frames.Add(BitmapFrame.Create(crop));

                using (var fs = System.IO.File.OpenWrite("crop.jpg"))
                {
                    pngEncoder.Save(fs);
                }
            }
        }
        private static BitmapSource[][] SplitFrame(BitmapSource rawframe, ConvertOptions options)
        {
            var scalex = (double)options.ImageWidth.Value / rawframe.PixelWidth;
            var scaley = (double)options.ImageHeight.Value / rawframe.PixelHeight;
            var frame  = new TransformedBitmap(rawframe, new ScaleTransform(scalex, scaley));

            Debug.Assert(frame.PixelWidth == options.ImageWidth.Value);
            Debug.Assert(frame.PixelHeight == options.ImageHeight.Value);

            var xcount = (options.ImageWidth.Value + options.TileWidth - 1) / options.TileWidth;
            var ycount = (options.ImageHeight.Value + options.TileHeight - 1) / options.TileHeight;

            var ret = new BitmapSource[ycount][];

            for (var y = 0; y < ycount; y++)
            {
                ret[y] = new BitmapSource[xcount];
                for (var x = 0; x < xcount; x++)
                {
                    var rect = new Int32Rect(x * options.TileWidth, y * options.TileHeight, options.TileWidth, options.TileHeight);
                    if (rect.X + rect.Width > frame.PixelWidth)
                    {
                        rect.Width = frame.PixelWidth - rect.X;
                    }
                    if (rect.Y + rect.Height > frame.PixelHeight)
                    {
                        rect.Height = frame.PixelHeight - rect.Y;
                    }
                    var cropped = new CroppedBitmap(frame, rect);
                    ret[y][x] = cropped;
                }
            }

            return(ret);
        }
        private void CanvasPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isMouseDown)
            {
                var x = Mouse.GetPosition(CanvasPanel).X;
                var y = Mouse.GetPosition(CanvasPanel).Y;

                if (x >= _ellipseHalfWidth && x <= CanvasImage.Width - _ellipseHalfWidth - 1 && y >= _ellipseHalfHeight && y <= CanvasImage.Height - _ellipseHalfHeight - 1)
                {
                    try
                    {
                        var croppedBitmap = new CroppedBitmap(ColorImage.Source as BitmapSource, new Int32Rect(Convert.ToInt32(x), Convert.ToInt32(y), 1, 1));
                        var pixels        = new byte[4];
                        croppedBitmap.CopyPixels(pixels, 4, 0);
                        SelectedColor = Color.FromArgb(255, pixels[2], pixels[1], pixels[0]);
                        MoveEllipse(x, y, SelectedColor);
                        UpdateTextBox();
                        ColorRectangle.Fill = new SolidColorBrush(SelectedColor);
                    }
                    catch (Exception)
                    {
                        _isMouseDown = false;
                        SetColor(Colors.White);
                    }
                    finally
                    {
                        CanvasImage.InvalidateVisual();
                    }
                }
                else
                {
                    _isMouseDown = false;
                }
            }
        }
Exemple #17
0
        private IEnumerable <Color> GeneratePalette()
        {
            if (Bitmap == null)
            {
                return(Enumerable.Empty <Color>());
            }
            byte[] bgrData = null;
            switch (Width)
            {
            case 512:
            {
                var crop = new CroppedBitmap(Bitmap, new Int32Rect(0, 0, 412, 240));
                bgrData = crop.GetBgr24Data();
                break;
            }

            case 1024:
            {
                // It Has a 920 Version, but the Max data is 1008, bear with a little gray
                var crop = new CroppedBitmap(Bitmap, new Int32Rect(0, 0, 1008, 240));
                bgrData = crop.GetBgr24Data();
                break;
            }

            default:
            {
                bgrData = Bitmap.GetBgr24Data();
                break;
            }
            }

            return(GraphicUtils.PaletteGen(bgrData, 20).Select(Extensions.ToMediaColor).ToList());
        }
        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source),
                                           new Int32Rect((int)Mouse.GetPosition(e.Source as Image).X,
                                                         (int)Mouse.GetPosition(e.Source as Image).Y, 1, 1));
                _pixels = new byte[4];
                try
                {
                    cb.CopyPixels(_pixels, 4, 0);
                    SetColor(Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]));
                    UpdateMarkerPosition();

                    if (OnColorSelected != null)
                    {
                        OnColorSelected(SelectedColor);
                    }
                }
                catch
                {
                    // not logged
                }
                UpdateSlider();
            }
            catch (Exception)
            {
                // not logged
            }
        }
Exemple #19
0
        private CroppedBitmap GetSnappedImage(Point mouse_up_point, Point mouse_down_point)
        {
            CroppedBitmap cropped_image_page = null;

            PngBitmapDecoder decoder    = new PngBitmapDecoder(new MemoryStream(pdf_renderer_control_stats.pdf_document.PDFRenderer.GetPageByDPIAsImage(page, 150)), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            BitmapSource     image_page = decoder.Frames[0];

            if (null != image_page)
            {
                double left   = Math.Min(mouse_up_point.X, mouse_down_point.X) * image_page.PixelWidth / this.ActualWidth;
                double top    = Math.Min(mouse_up_point.Y, mouse_down_point.Y) * image_page.PixelHeight / this.ActualHeight;
                double width  = Math.Abs(mouse_up_point.X - mouse_down_point.X) * image_page.PixelWidth / this.ActualWidth;
                double height = Math.Abs(mouse_up_point.Y - mouse_down_point.Y) * image_page.PixelHeight / this.ActualHeight;

                left   = Math.Max(left, 0);
                top    = Math.Max(top, 0);
                width  = Math.Min(width, image_page.PixelWidth - left);
                height = Math.Min(height, image_page.PixelHeight - top);

                if (0 < width && 0 < height)
                {
                    cropped_image_page = new CroppedBitmap(image_page, new Int32Rect((int)left, (int)top, (int)width, (int)height));
                }
            }

            return(cropped_image_page);
        }
        private void Image_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var AssociatedObject = this;
            // Retrieve the coordinate of the mouse position in relation to the supplied image.
            Point point = e.GetPosition(AssociatedObject);

            // Use RenderTargetBitmap to get the visual, in case the image has been transformed.
            var renderTargetBitmap = new RenderTargetBitmap((int)AssociatedObject.ActualWidth,
                                                            (int)AssociatedObject.ActualHeight,
                                                            96, 96, PixelFormats.Default);

            renderTargetBitmap.Render(AssociatedObject);

            // Make sure that the point is within the dimensions of the image.
            if ((point.X <= renderTargetBitmap.PixelWidth) && (point.Y <= renderTargetBitmap.PixelHeight))
            {
                // Create a cropped image at the supplied point coordinates.
                var croppedBitmap = new CroppedBitmap(renderTargetBitmap,
                                                      new Int32Rect((int)point.X, (int)point.Y, 1, 1));

                // Copy the sampled pixel to a byte array.
                var pixels = new byte[4];
                croppedBitmap.CopyPixels(pixels, 4, 0);

                // Assign the sampled color to a SolidColorBrush and return as conversion.
                var SelectedColor = Color.FromArgb(255, pixels[2], pixels[1], pixels[0]);
                TextBox.Text     = "#" + SelectedColor.ToString().Substring(3);
                Label.Background = new SolidColorBrush(SelectedColor);
            }
        }
        private void CropMiniMap(int posX, int posY)
        {
            posX -= (MinimapZoom / 2);
            posY -= (MinimapZoom / 2);

            if (posX < 0)
            {
                posX = 0;
            }
            if (posY < 0)
            {
                posY = 0;
            }

            MiniMapCropped = new CroppedBitmap((BitmapSource)MiniMapImage, new Int32Rect(posX, posY, MinimapZoom, MinimapZoom));
            var target = new RenderTargetBitmap(MiniMapCropped.PixelWidth, MiniMapCropped.PixelHeight, MiniMapCropped.DpiX, MiniMapCropped.DpiY, PixelFormats.Pbgra32);
            var visual = new DrawingVisual();

            using (var r = visual.RenderOpen())
            {
                r.DrawLine(new Pen(Brushes.Red, 10.0), new Point(0, 0), new Point(MiniMapCropped.Width, MiniMapCropped.Height));
            }

            target.Render(visual);

            MiniMapCropped.Source = target;
        }
        private BitmapSource?imageForEnchantmentLevel(int level)
        {
            var enchantmentLevelImage = fullImageForEnchantmentLevel(level);
            var croppedImage          = new CroppedBitmap(enchantmentLevelImage, new Int32Rect(0, 0, 976, 959));

            return(croppedImage);
        }
Exemple #23
0
        /// <summary>
        /// Simply grab a 1*1 pixel from the current color image, and
        /// use that and copy the new 1*1 image pixels to a byte array and
        /// then construct a Color from that.
        /// </summary>
        private void CanvImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (!IsMouseDown)
            {
                return;
            }

            try{
                CroppedBitmap cb = new CroppedBitmap(ColorImage.Source as BitmapSource,
                                                     new Int32Rect((int)Mouse.GetPosition(CanvImage).X,
                                                                   (int)Mouse.GetPosition(CanvImage).Y, 1, 1));

                byte[] pixels = new byte[4];

                //    try{
                cb.CopyPixels(pixels, 4, 0);
                //    }
                //    catch (Exception ex){
                //Ooops
                //    }

                //Ok now, so update the mouse cursor position and the SelectedColor
                ellipsePixel.SetValue(Canvas.LeftProperty, (double)(Mouse.GetPosition(CanvImage).X - 5));
                ellipsePixel.SetValue(Canvas.TopProperty, (double)(Mouse.GetPosition(CanvImage).Y - 5));
                CanvImage.InvalidateVisual();
                SelectedColor = Color.FromArgb((byte)AlphaSlider.Value, pixels[2], pixels[1], pixels[0]);
            }
            catch (Exception exc) {
                WriteLine(exc.Message);
                WriteLine(exc.StackTrace);
                //not much we can do
            }
        }
Exemple #24
0
        private ImageEditor(string filename, Size size, bool allowNoCrop, Rect?startRect)
        {
            _model = new CropModel {
                TargetWidth  = size.Width,
                TargetHeight = size.Height
            };

            DataContext = this;
            InitializeComponent();

            Buttons = new[] {
                allowNoCrop?CreateExtraDialogButton(AppStrings.CropImage_Skip, new DelegateCommand(() => {
                    Result = (BitmapSource)_image.ImageSource;
                    ResultRect = new Int32Rect(0, 0, _image.Width, _image.Height);
                    ResultRectRelative = new Rect(0d, 0d, 1d, 1d);
                    Close();
                }, () => !_image.IsBroken)) : null,
                    CreateCloseDialogButton(UiStrings.Ok, true, false, MessageBoxResult.OK, new DelegateCommand(() => {
                    var rect = new Int32Rect((int)_model.OffsetX, (int)_model.OffsetY, (int)_model.ImageWidth, (int)_model.ImageHeight);

                    ResultRect         = rect;
                    ResultRectRelative = new Rect(
                        (double)rect.X / _image.Width, (double)rect.Y / _image.Height,
                        (double)rect.Width / _image.Width, (double)rect.Height / _image.Height);

                    var cropped = new CroppedBitmap((BitmapSource)_image.ImageSource, rect);
                    Result      = cropped.Resize((int)_model.TargetWidth, (int)_model.TargetHeight);
                }, () => !_image.IsBroken)),
                    CancelButton
            };

            LoadImage(filename, startRect).Forget();
            UpdateControlsToScale();
        }
Exemple #25
0
        private void SaveImgBtn_Click(object sender, RoutedEventArgs e)
        {
            string path = FileManager.GetSavePathFromDialog(Enums.Extension.PNG);

            if (path == null)
            {
                MessageBox.Show(Properties.Strings.FileNotFound);
            }
            else
            {
                var rect = myCanvas.Children[0] as Rectangle;
                RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Width + 5,
                                                                (int)rect.Height + 5, 96d, 96d, PixelFormats.Default);
                rtb.Render(myCanvas);

                var crop = new CroppedBitmap(rtb, new Int32Rect(0, 0, (int)rect.Width, (int)rect.Height));

                BitmapEncoder pngEncoder = new PngBitmapEncoder();
                pngEncoder.Frames.Add(BitmapFrame.Create(crop));

                using (var fs = System.IO.File.OpenWrite(FileManager.GetFullPath(path)))
                {
                    pngEncoder.Save(fs);
                }
            }
        }
Exemple #26
0
        private ImageWidgetViewModel MakeCopyImageWidget(BitmapSource source, Size orignalsize, Rect rectangleIntersect, Rect rectRotate)
        {
            var widget = this._pageEditorViewModel.PageEditorModel.AddWidgetItem2Dom
                             (Naver.Compass.Service.Document.WidgetType.Image,
                             Naver.Compass.Service.Document.ShapeType.None,
                             rectangleIntersect.Left,
                             rectangleIntersect.Top,
                             Convert.ToInt32(rectangleIntersect.Width),
                             Convert.ToInt32(rectangleIntersect.Height));
            var vmItem = new ImageWidgetViewModel(widget);

            vmItem.IsSelected = false;
            var cb = new CroppedBitmap(
                source,
                new Int32Rect(
                    Convert.ToInt32((rectangleIntersect.Left - rectRotate.Left) * orignalsize.Width / rectRotate.Width),
                    Convert.ToInt32((rectangleIntersect.Top - rectRotate.Top) * orignalsize.Height / rectRotate.Height),
                    Convert.ToInt32(rectangleIntersect.Width * orignalsize.Width / rectRotate.Width),
                    Convert.ToInt32(rectangleIntersect.Height * orignalsize.Height / rectRotate.Height)));
            var ms      = new MemoryStream();
            var encoder = new PngBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(cb));
            encoder.Save(ms);
            ms.Seek(0, SeekOrigin.Begin);
            vmItem.ImportImgSeekLater(ms, false);

            return(vmItem);
        }
Exemple #27
0
        /// <summary>Creates the screenshot of entire plotter element</summary>
        /// <returns></returns>
        internal static BitmapSource CreateScreenshot(UIElement uiElement, Int32Rect screenshotSource)
        {
            Window window = Window.GetWindow(uiElement);

            if (window == null)
            {
                return(CreateElementScreenshot(uiElement));
            }
            Size size = window.RenderSize;

            //double dpiCoeff = 32 / SystemParameters.CursorWidth;
            //int dpi = (int)(dpiCoeff * 96);
            double dpiCoeff = 1;
            int    dpi      = 96;

            RenderTargetBitmap bmp = new RenderTargetBitmap(
                (int)(size.Width * dpiCoeff), (int)(size.Height * dpiCoeff),
                dpi, dpi, PixelFormats.Default);

            bmp.Render(uiElement);

            CroppedBitmap croppedBmp = new CroppedBitmap(bmp, screenshotSource);

            return(croppedBmp);
        }
        private CroppedBitmap GenerateCrop()
        {
            /*
             * BitmapSource bSource = new BitmapImage(new Uri(@"C:\Users\Gabriel\Pictures\demo.jpeg"));
             * Int32Rect cropRect = new Int32Rect(0,0,200,200);
             * var bmpCropped = new CroppedBitmap(bSource,cropRect);
             */


            double width  = border.ActualWidth;
            double height = border.ActualHeight;

            RenderTargetBitmap bmpCopied = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), 0, 0, PixelFormats.Default);
            DrawingVisual      dv        = new DrawingVisual();

            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(border);
                vb.Stretch = Stretch.None;
                dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
            }

            bmpCopied.Render(dv);

            var cropRectWidth  = (int)(Math.Round(Canvas.GetLeft(rectangle)) + rectangle.StrokeThickness);
            var cropRectHeight = (int)(Math.Round(Canvas.GetTop(rectangle)) + rectangle.StrokeThickness);
            var cropRectX      = (int)(rectangle.ActualWidth - rectangle.StrokeThickness * 2);
            var cropRectY      = (int)(rectangle.ActualHeight - rectangle.StrokeThickness * 2);

            Int32Rect     cropRect   = new Int32Rect(cropRectWidth, cropRectHeight, cropRectX, cropRectY);
            CroppedBitmap bmpCropped = new CroppedBitmap(bmpCopied, cropRect);


            return(bmpCropped);
        }
Exemple #29
0
        private Grid MapGrid(CroppedBitmap bitmap, int x, int y)
        {
            var grid = new Grid {
                RenderTransform = new RotateTransform(0, 123, 123)
            };

            grid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(82)
            });
            grid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(82)
            });
            grid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = new GridLength(82)
            });
            grid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(82)
            });
            grid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(82)
            });
            grid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(82)
            });
            var img = new Image {
                Source = bitmap
            };

            grid.Children.Add(img);
            Grid.SetColumn(img, x);
            Grid.SetRow(img, y);
            grid.Width  = 82 * 3;
            grid.Height = 82 * 3;
            return(grid);
        }
Exemple #30
0
        protected override void DecodeData()
        {
            Dictionary <String, BitmapSource> ImagesBoard = ReadImages();

            NumberToKey.Clear();
            foreach (SummonerSpellDto spell in SummonerSpellList.data.Values)
            {
                try
                {
                    ImageDto image = spell.image;
                    NumberToKey.Add(spell.id, spell.key);
                    string imagePath = DirectoryPath + spell.image.sprite;
                    if (!ImagesBoard.ContainsKey(imagePath))
                    {
                        String url = String.Format("http://ddragon.leagueoflegends.com/cdn/{0}/img/sprite/{1}", SummonerSpellList.version, spell.image.sprite);
                        File.Delete(imagePath);
                        Utilities.DownloadFile(url, imagePath);
                        ImagesBoard.Add(imagePath, Utilities.GetBitmapImage(imagePath));
                    }
                    ImageSource cropped = new CroppedBitmap(ImagesBoard[imagePath], new Int32Rect(image.x, image.y, image.w, image.h));
                    cropped.Freeze();   //一定要Freeze,因為跨Thread
                    if (Images.ContainsKey(spell.key))
                    {
                        Images.Remove(spell.key);
                    }
                    Images.Add(spell.key, cropped);
                }
                catch (Exception)
                {
                }
            }
        }