Exemplo n.º 1
0
        private static MemoryStream SaveDrawingImage(ImageSource drawingImage)
        {
            var image = new Image
            {
                Source = drawingImage
            };

            image.Arrange(new Rect(0, 0, drawingImage.Width, drawingImage.Height));

            var settings = Settings.Default;

            var renderTargetBitmap = new RenderTargetBitmap((int)drawingImage.Width, (int)drawingImage.Height, settings.ImageDPI, settings.ImageDPI, PixelFormats.Default);
            renderTargetBitmap.Render(image);

            Settings.Default.Save();

            var encoder = new JpegBitmapEncoder
            {
                QualityLevel = settings.JpgQuality
            };

            encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

            var memoryStream = new MemoryStream();

            encoder.Save(memoryStream);

            return memoryStream;
        }
Exemplo n.º 2
0
        void CheckImage(BitmapImage img)
        {
            if(img.IsDownloading)
            {
                Dispatcher.InvokeAsync(() => CheckImage(img), DispatcherPriority.Background);
            }
            else
            {
                var el = new System.Windows.Controls.Image()
                {
                    Source = img,
                    SnapsToDevicePixels = true,
                    UseLayoutRounding = true,
                    MinWidth = LedBadgeLib.BadgeCaps.Width,
                    MinHeight = LedBadgeLib.BadgeCaps.Height,
                    Width = img.Width * ((BitmapSource)img).DpiX / 96,
                    Stretch = Stretch.UniformToFill,
                };
                RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor);
                el.Measure(new Size(LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height));
                el.Arrange(new Rect(0, 0, LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height));

                m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(el, dither : Dither)));
            };
        }
Exemplo n.º 3
0
        void CheckImage(BitmapImage img)
        {
            if (img.IsDownloading)
            {
                Dispatcher.InvokeAsync(() => CheckImage(img), DispatcherPriority.Background);
            }
            else
            {
                var el = new System.Windows.Controls.Image()
                {
                    Source = img,
                    SnapsToDevicePixels = true,
                    UseLayoutRounding   = true,
                    MinWidth            = m_messageQueue.Device.Width,
                    MinHeight           = m_messageQueue.Device.Height,
                    Width   = img.Width * ((BitmapSource)img).DpiX / 96,
                    Stretch = Stretch.UniformToFill,
                };
                RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor);
                el.Measure(new Size(m_messageQueue.Device.Width, m_messageQueue.Device.Height));
                el.Arrange(new Rect(0, 0, m_messageQueue.Device.Width, m_messageQueue.Device.Height));

                m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(m_messageQueue.Device, el, dither: Dither)));
            };
        }
Exemplo n.º 4
0
 private static UIElement PrintPage(BitmapSource bitmap)
 {
     var bitmapSize = new System.Windows.Size(bitmap.PixelWidth, bitmap.PixelHeight);
     var image = new System.Windows.Controls.Image { Source = bitmap };
     image.Measure(bitmapSize);
     image.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0), bitmapSize));
     return image;
 }
Exemplo n.º 5
0
        private static UIElement PrintPage(BitmapSource bitmap)
        {
            var bitmapSize = new System.Windows.Size(bitmap.PixelWidth, bitmap.PixelHeight);
            var image      = new System.Windows.Controls.Image {
                Source = bitmap
            };

            image.Measure(bitmapSize);
            image.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0), bitmapSize));
            return(image);
        }
Exemplo n.º 6
0
        public void Print(System.Windows.Controls.Image image)
        {
            System.Printing.LocalPrintServer localPrintServer = new System.Printing.LocalPrintServer();
            PrintDialog printDialog = new PrintDialog();

            System.Printing.PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / image.ActualWidth, capabilities.PageImageableArea.ExtentHeight / image.ActualHeight);

            image.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale);
            System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
            image.Measure(sz);
            image.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

            printDialog.PrintVisual(image, "Print Image");
        }
Exemplo n.º 7
0
        void CheckTweetData(string text, List <BitmapImage> images)
        {
            bool ready = true;

            foreach (var img in images)
            {
                if (img.IsDownloading)
                {
                    ready = false;
                    break;
                }
            }

            if (ready)
            {
                Func <ImageSource, Image> makeImg = source =>
                {
                    var el = new System.Windows.Controls.Image()
                    {
                        Source = source,
                        SnapsToDevicePixels = true,
                        UseLayoutRounding   = true,
                        MinWidth            = m_messageQueue.Device.Width,
                        MinHeight           = m_messageQueue.Device.Height,
                        Width   = source.Width * ((BitmapSource)source).DpiX / 96,
                        Stretch = Stretch.UniformToFill,
                    };
                    RenderOptions.SetBitmapScalingMode(source, BitmapScalingMode.NearestNeighbor);
                    el.Measure(new Size(m_messageQueue.Device.Width, m_messageQueue.Device.Height));
                    el.Arrange(new Rect(0, 0, m_messageQueue.Device.Width, m_messageQueue.Device.Height));
                    return(el);
                };

                if (images.Count > 0)
                {
                    m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(m_messageQueue.Device, makeImg(images[0]), dither: Dither)));
                }
                m_messageQueue.Enqueue(LedBadgeLib.WPF.MakeQueuedItem(m_messageQueue.Device, LedBadgeLib.WPF.MakeSingleLineItem(m_messageQueue.Device, text)));
                for (int i = 1; i < images.Count; ++i)
                {
                    m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(m_messageQueue.Device, makeImg(images[i]), dither: Dither)));
                }
            }
            else
            {
                Dispatcher.InvokeAsync(() => CheckTweetData(text, images), DispatcherPriority.Background);
            }
        }
Exemplo n.º 8
0
        public static Bitmap ImageControlToBitmap(System.Windows.Controls.Image image)
        {
            RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)image.ActualWidth, (int)image.ActualHeight, 96.0, 96.0, PixelFormats.Pbgra32);

            image.Measure(new System.Windows.Size((int)image.ActualWidth, (int)image.ActualHeight));
            image.Arrange(new System.Windows.Rect(new System.Windows.Size((int)image.ActualWidth, (int)image.ActualHeight)));

            rtBmp.Render(image);

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            MemoryStream     stream  = new MemoryStream();

            encoder.Frames.Add(BitmapFrame.Create(rtBmp));

            encoder.Save(stream);
            return(new Bitmap(stream));
        }
Exemplo n.º 9
0
        public void SaveDrawingToFile(Drawing drawing, string fileName, double scale)
        {
            var drawingImage = new System.Windows.Controls.Image {
                Source = new DrawingImage(drawing)
            };
            var width  = drawing.Bounds.Width * scale;
            var height = drawing.Bounds.Height * scale;

            drawingImage.Arrange(new Rect(0, 0, width, height));

            var bitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);

            bitmap.Render(drawingImage);

            string Extension = System.IO.Path.GetExtension(fileName).ToLower();


            BitmapEncoder encoder;

            if (Extension == ".gif")
            {
                encoder = new GifBitmapEncoder();
            }
            else if (Extension == ".png")
            {
                encoder = new PngBitmapEncoder();
            }
            else if (Extension == ".jpg")
            {
                encoder = new JpegBitmapEncoder();
            }
            else
            {
                return;
            }

            encoder.Frames.Add(BitmapFrame.Create(bitmap));

            using (var stream = new FileStream(fileName, FileMode.Create))
            {
                encoder.Save(stream);
            }
        }
        /// <summary>
        /// converts a writeable bitmap into a bitmap
        /// make sure that width and height are > 0 to prevent exceptions
        /// </summary>
        /// <param name="drawing"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns>A bitmap instance</returns>
        public System.Drawing.Bitmap WriteableBitmapToBitmap(WriteableBitmap drawing, int width, int height)
        {
            // creates an image with drawing
            System.Windows.Controls.Image drawingImage = new System.Windows.Controls.Image {
                Source = drawing
            };
            drawingImage.Arrange(new System.Windows.Rect(0, 0, width, height));
            // converts into rendertargetbitmap
            RenderTargetBitmap bitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);

            bitmap.Render(drawingImage);
            MemoryStream  stream  = new MemoryStream();
            BitmapEncoder encoder = new BmpBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(bitmap));
            encoder.Save(stream);

            return(new Bitmap(stream));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Save chart image to Bitmap
        /// </summary>
        private System.Drawing.Bitmap ImageToBitmap(System.Windows.Controls.Image img)
        {
            RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)img.Width, (int)img.Height,
                                                              96.0, 96.0, PixelFormats.Pbgra32);

            img.Measure(new System.Windows.Size((int)img.Width, (int)img.Height));
            img.Arrange(new Rect(new System.Windows.Size((int)img.Width, (int)img.Height)));

            rtBmp.Render(img);

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            MemoryStream     stream  = new MemoryStream();

            encoder.Frames.Add(BitmapFrame.Create(rtBmp));

            // Save to memory stream and create Bitmap from stream
            encoder.Save(stream);
            return(new System.Drawing.Bitmap(stream));
        }
Exemplo n.º 12
0
        private void SaveImageToFile(System.Windows.Controls.Image imageIn, string filePath)
        {
            if (imageIn.Source == null)
            {
                return;
            }

            RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)imageIn.Source.Width, (int)imageIn.Source.Height,
                                                              96.0, 96.0, PixelFormats.Pbgra32);

            imageIn.Measure(new System.Windows.Size((int)imageIn.Source.Width, (int)imageIn.Source.Height));
            imageIn.Arrange(new Rect(new System.Windows.Size((int)imageIn.Source.Width, (int)imageIn.Source.Height)));

            rtBmp.Render(imageIn);
            JpegBitmapEncoder jEncoder = new JpegBitmapEncoder();
            MemoryStream      stream   = new MemoryStream();

            jEncoder.Frames.Add(BitmapFrame.Create(rtBmp));

            /*
             * PngBitmapEncoder encoder = new PngBitmapEncoder();
             * MemoryStream stream = new MemoryStream();
             * encoder.Frames.Add(BitmapFrame.Create(rtBmp));
             */
            // Save to memory stream and create Bitamp from stream
            jEncoder.Save(stream);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream);

            // Demonstrate that we can do something with the Bitmap
            bitmap.Save(GetSaveToFilePath(filePath), ImageFormat.Jpeg);



            // Optionally, if we didn't need Bitmap object, but
            // just wanted to render to file, we could:
            //encoder.Save(new FileStream(GetSaveToFilePath(filePath), FileMode.Create));
        }
Exemplo n.º 13
0
        protected override void OnAttached()
        {
            var control = Control as Panel;

            if (control != null)
            {
                System.Windows.Controls.Image image = null;

                var effect = new BlurEffect
                {
                    Radius        = 5,
                    KernelType    = KernelType.Box,
                    RenderingBias = RenderingBias.Performance
                };

                window = System.Windows.Application.Current.MainWindow;

                var dpiX = 96; //(int)typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
                var dpiY = 96; //(int)typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);

                var renderer = new RenderTargetBitmap((int)window.Width, (int)window.Height, dpiX, dpiY, PixelFormats.Pbgra32);

                visibleChanged = (s, e) =>
                {
                    if (control.IsVisible)
                    {
                        control.Children.Remove(image);
                        var children = control.Children.Cast <UIElement>().ToArray();
                        control.Children.Clear();

                        image         = new System.Windows.Controls.Image();
                        image.Effect  = effect;
                        image.Stretch = Stretch.Fill;
                        image.Source  = renderer;
                        renderer.Render(window);

                        control.Children.Add(image);
                        foreach (var c in children)
                        {
                            control.Children.Add(c);
                        }

                        image.Width  = window.Width;
                        image.Height = window.Height;
                        image.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
                        image.Arrange(new Rect(image.DesiredSize));
                    }
                };

                control.IsVisibleChanged += visibleChanged;

                sizeChanged = (s, e) =>
                {
                    if (control.IsVisible && image != null)
                    {
                        image.Width  = window.Width;
                        image.Height = window.Height;
                        image.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));
                        image.Arrange(new Rect(image.DesiredSize));
                    }
                    else
                    {
                        renderer = new RenderTargetBitmap((int)window.Width, (int)window.Height, dpiX, dpiY, PixelFormats.Pbgra32);
                    }
                };

                window.SizeChanged += sizeChanged;
            }
        }
Exemplo n.º 14
0
        private void ApplyCinemagraphButton_Click(object sender, RoutedEventArgs e)
        {
            if (CinemagraphInkCanvas.Strokes.Count == 0)
            {
                ShowWarning(ResMessage("Editor.Cinemagraph.WarningNoDrawing"), MessageIcon.Info);
                return;
            }

            ActionStack.Did(ListFrames);

            var dpi = ListFrames[0].ImageLocation.DpiOf();
            var scaledSize = ListFrames[0].ImageLocation.ScaledSize();

            #region Get the Strokes and Clip the Image

            var image = ListFrames[0].ImageLocation.SourceFrom();
            var rectangle = new RectangleGeometry(new Rect(new System.Windows.Point(0, 0), new System.Windows.Size(image.PixelWidth, image.PixelHeight)));
            Geometry geometry = Geometry.Empty;

            foreach (Stroke stroke in CinemagraphInkCanvas.Strokes)
            {
                geometry = Geometry.Combine(geometry, stroke.GetGeometry(), GeometryCombineMode.Union, null);
            }

            geometry = Geometry.Combine(geometry, rectangle, GeometryCombineMode.Xor, null);

            var clippedImage = new System.Windows.Controls.Image
            {
                Height = image.PixelHeight,
                Width = image.PixelWidth,
                Source = image,
                Clip = geometry
            };
            clippedImage.Measure(scaledSize);
            clippedImage.Arrange(new Rect(scaledSize));

            var imageRender = clippedImage.GetRender(dpi, scaledSize);

            #endregion

            Cursor = Cursors.AppStarting;

            _overlayFramesDel = Overlay;
            _overlayFramesDel.BeginInvoke(imageRender, dpi, true, OverlayCallback, null);

            ClosePanel();
        }
Exemplo n.º 15
0
        void CheckTweetData(string text, List<BitmapImage> images)
        {
            bool ready = true;
            foreach(var img in images)
            {
                if(img.IsDownloading)
                {
                    ready = false;
                    break;
                }
            }

            if(ready)
            {
                Func<ImageSource, Image> makeImg = source =>
                {
                    var el = new System.Windows.Controls.Image()
                    {
                        Source = source,
                        SnapsToDevicePixels = true,
                        UseLayoutRounding = true,
                        MinWidth = LedBadgeLib.BadgeCaps.Width,
                        MinHeight = LedBadgeLib.BadgeCaps.Height,
                        Width = source.Width * ((BitmapSource)source).DpiX / 96,
                        Stretch = Stretch.UniformToFill,
                    };
                    RenderOptions.SetBitmapScalingMode(source, BitmapScalingMode.NearestNeighbor);
                    el.Measure(new Size(LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height));
                    el.Arrange(new Rect(0, 0, LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height));
                    return el;
                };

                if(images.Count > 0)
                {
                    m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(makeImg(images[0]), dither: Dither)));
                }
                m_messageQueue.Enqueue(LedBadgeLib.WPF.MakeQueuedItem(LedBadgeLib.WPF.MakeSingleLineItem(text)));
                for(int i = 1; i < images.Count; ++i)
                {
                    m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(makeImg(images[i]), dither : Dither)));
                }
            }
            else
            {
                Dispatcher.InvokeAsync(() => CheckTweetData(text, images), DispatcherPriority.Background);
            }
        }