Exemple #1
0
        /// <summary>
        /// This function convert the Canvas control to the Bitmap Image.
        /// </summary>
        /// <param name="surface">
        /// Canvas control Object want to convert.
        /// </param>
        /// <returns>
        /// Bitmap Object containing Canvas control Image.
        /// </returns>
        public static System.Drawing.Bitmap CanvasToBitmap(System.Windows.Controls.Canvas surface)
        {
            System.Drawing.Bitmap BmpOut = null;
            try
            {
                try
                {
                    File.Delete("C:\\Images\\Img.jpg");
                }
                catch (Exception)
                { }
                Uri path = new Uri("C:\\Images\\Img.jpg");
                // Save current canvas transform
                Transform transform = surface.LayoutTransform;
                // reset current transform (in case it is scaled or rotated)
                surface.LayoutTransform = null;

                // Get the size of canvas
                Size size = new Size(surface.ActualWidth, surface.ActualHeight);
                // Measure and arrange the surface
                // VERY IMPORTANT
                surface.Measure(size);
                surface.Arrange(new Rect(size));

                // Create a render bitmap and push the surface to it
                RenderTargetBitmap renderBitmap =
                    new RenderTargetBitmap(
                        (int)size.Width,
                        (int)size.Height,
                        96d,
                        96d,
                        PixelFormats.Pbgra32);
                renderBitmap.Render(surface);

                // Create a file stream for saving image
                using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
                {
                    // Use png encoder for our data
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    // push the rendered bitmap to it
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    // save the data to the stream
                    encoder.Save(outStream);
                }

                // Restore previously saved layout
                surface.LayoutTransform = transform;
                Thread.Sleep(200);

                BmpOut = new System.Drawing.Bitmap("C:\\Images\\Img.jpg");
            }
            catch (Exception)
            { }
            return(BmpOut);
        }
        // renders a bitmap image and save as a png file
        public static void RenderImage(int width, int height, string path, System.Windows.Controls.Canvas canvas)
        {
            canvas.Measure(new Size(width, height));
            canvas.Arrange(new Rect(new Size(width, height)));

            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);

            renderBitmap.Render(canvas);
            PngBitmapEncoder encoder = new PngBitmapEncoder();

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

            using (FileStream file = File.Create(path))
            {
                encoder.Save(file);
            }
        }
Exemple #3
0
        /// <summary>
        /// Avi : 14 Feb ; Kraus RGA Project.
        /// Export the canvas to the Images withe the file name.
        /// used in the camera screen to capture the images.
        /// </summary>
        /// <param name="path">
        /// String pathe of the images to be save with the image extension.
        /// </param>
        /// <param name="surface">
        /// Canvas control to be converted in to image.
        /// </param>
        public static void CanvasExportToPng(Uri path, System.Windows.Controls.Canvas surface)
        {
            if (path == null)
            {
                return;
            }

            // Save current canvas transform
            Transform transform = surface.LayoutTransform;

            // reset current transform (in case it is scaled or rotated)
            surface.LayoutTransform = null;

            // Get the size of canvas
            Size size = new Size(surface.ActualWidth, surface.ActualHeight);

            // Measure and arrange the surface
            // VERY IMPORTANT
            surface.Measure(size);
            surface.Arrange(new Rect(size));

            // Create a render bitmap and push the surface to it
            RenderTargetBitmap renderBitmap =
                new RenderTargetBitmap(
                    (int)size.Width,
                    (int)size.Height,
                    96d,
                    96d,
                    PixelFormats.Pbgra32);

            renderBitmap.Render(surface);

            // Create a file stream for saving image
            using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
            {
                // Use png encoder for our data
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                // push the rendered bitmap to it
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                // save the data to the stream
                encoder.Save(outStream);
            }

            // Restore previously saved layout
            surface.LayoutTransform = transform;
        }
Exemple #4
0
        public static void CreateRemappedRasterFromVector(string xamlFile, System.Windows.Media.Color newColor, System.IO.Stream stream, double height, double width, string dynamicText)
        {
            string remapBase = "ReMapMe_";
            int    max       = 3;
            int    index     = 0;
            int    missed    = 0;
            Object temp      = null;
            string dynamText = "DynamicText";

            System.Windows.Controls.Canvas theVisual = System.Windows.Markup.XamlReader.Load(new System.IO.FileStream(xamlFile, System.IO.FileMode.Open)) as System.Windows.Controls.Canvas;

            if (null == theVisual)
            {
                return;
            }

            System.Windows.Size size = new System.Windows.Size(width, height);

            System.Windows.Media.DrawingVisual drawingVisual = new System.Windows.Media.DrawingVisual();

            System.Windows.Media.VisualBrush visualBrush = new System.Windows.Media.VisualBrush(theVisual);

            System.Windows.Rect rect = new System.Windows.Rect(new System.Windows.Point(), size);

            while (missed < max)
            {
                string elementid = remapBase + index;

                temp = theVisual.FindName(elementid);

                if (null == temp)
                {
                    missed++;
                    index++;
                    continue;
                }

                System.Windows.Media.SolidColorBrush scb = ((temp as System.Windows.Shapes.Path).Fill as System.Windows.Media.SolidColorBrush);

                System.Windows.Media.Color c = scb.Color;
                c.B = newColor.B;
                c.R = newColor.R;
                c.G = newColor.G;

                scb.Color = c;
                index++;
            }

            theVisual.Arrange(rect);
            theVisual.UpdateLayout();

            using (System.Windows.Media.DrawingContext dc = drawingVisual.RenderOpen())
            {
                dc.DrawRectangle(visualBrush, null, rect);
            }

            if (!dynamText.IsNullOrWhitespace())
            {
                temp = theVisual.FindName(dynamText);
                if (null != temp)
                {
                    //do dynamic text shit
                }
            }

            System.Windows.Media.Imaging.RenderTargetBitmap render = new System.Windows.Media.Imaging.RenderTargetBitmap((int)height, (int)width, 96, 96, System.Windows.Media.PixelFormats.Pbgra32);
            render.Render(drawingVisual);

            System.Windows.Media.Imaging.PngBitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();

            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(render));

            encoder.Save(stream);

            stream.Flush();
            stream.Close();

            visualBrush   = null;
            drawingVisual = null;
            theVisual     = null;
            render        = null;
            encoder       = null;
        }
Exemple #5
0
        public void GenRangeBrush()
        {
            //处理点集
            for (int i = 0; i < drawObjects.Count; i++)
            {
                StruDrawObjDesc objdesc    = drawObjects[i];
                string[]        ss         = objdesc.strPoints.Split(' ');
                int             pointcount = ss.Length;
                List <Point>    points     = new List <Point>();
                Point           pnt        = new Point(0, 0);
                for (int j = 0; j < pointcount; j++)
                {
                    pnt = Point.Parse(ss[j]);
                    points.Add(pnt);
                }
                objdesc.listpnts = points;
            }


            //miny = dots.Min(p => p.location.X); maxy = dots.Max(p => p.location.X);  //将经度换为X坐标, 纬度换为Y坐标
            //minx = dots.Min(p => p.location.Y); maxx = dots.Max(p => p.location.Y);
            miny = drawObjects.Min(p1 => p1.listpnts.Min(p2 => p2.X)); maxy = drawObjects.Max(p1 => p1.listpnts.Max(p2 => p2.X));
            minx = drawObjects.Min(p1 => p1.listpnts.Min(p2 => p2.Y)); maxx = drawObjects.Max(p1 => p1.listpnts.Max(p2 => p2.Y));
            double w = maxx - minx; double h = maxy - miny;

            //minx = minx - w * 0.2; maxx = maxx + w * 0.2;
            //miny = miny - h * 0.2; maxy = maxy + h * 0.2;

            minx = minx - 0.01; maxx = maxx + 0.01; //改为扩展固定大小
            miny = miny - 0.01; maxy = maxy + 0.01;

            w = maxx - minx; h = maxy - miny;
            //经纬换为屏幕坐标
            int    size     = 1024;
            double ww       = w > h ? size : size * w / h;
            double hh       = w > h ? size * h / w : size;
            double scalerad = size / Math.Max(w, h) / 10000;
            Size   canvSize = new Size(ww, hh);

            for (int i = 0; i < drawObjects.Count; i++)
            {
                StruDrawObjDesc objdesc    = drawObjects[i];
                List <Point>    lp         = objdesc.listpnts;
                PointCollection pc         = new PointCollection();
                int             pointcount = lp.Count;
                foreach (Point pp in lp)
                {
                    Point tmp = new Point((pp.Y - minx) / w * canvSize.Width, (maxy - pp.X) / h * canvSize.Height);  //重新赋与新的平面点位置, 注,纬度取反,仅适用北半球
                    pc.Add(tmp);
                }

                objdesc.pc       = pc;
                objdesc.listpnts = null;
            }


            //绘制
            canv.Children.Clear();
            canv.Children.Add(new Rectangle()
            {
                Width = canvSize.Width, Height = canvSize.Height, StrokeThickness = 10, Stroke = Brushes.Transparent
            });


            //生成geo
            PathGeometry pg = new PathGeometry();

            foreach (StruDrawObjDesc objdesc in drawObjects)
            {
                PathFigure pf = new PathFigure();
                pg.Figures.Add(pf);
                pf.Segments.Add(new PolyLineSegment());
                pf.StartPoint = objdesc.pc[0];
                (pf.Segments[0] as PolyLineSegment).Points = objdesc.pc;
            }


            for (int i = layerCount - 1; i >= 0; i--)
            {
                Path path = new Path();
                path.Data               = pg;
                path.Stroke             = brush;
                path.StrokeStartLineCap = path.StrokeEndLineCap = PenLineCap.Round;
                path.StrokeLineJoin     = PenLineJoin.Round;
                path.StrokeThickness    = rad * scalerad * (i + 1) / layerCount;
                //path.Opacity = ((1.0f*layerCount - i ) / layerCount);
                path.Fill = Brushes.Transparent;

                path.Effect = new BlurEffect()
                {
                    Radius = blueRadius * scalerad
                };

                canv.Children.Add(path);
            }
            canv.Effect = new BlurEffect()
            {
                Radius = Math.Max(canvSize.Width, canvSize.Height) / blurFactor
            };

            RangeBrush        = new VisualBrush();
            RangeBrush.Visual = canv;

            canv.Measure(canvSize);
            canv.Arrange(new System.Windows.Rect(0, 0, canvSize.Width, canvSize.Height));

            canv.Measure(canvSize);
            canv.Arrange(new System.Windows.Rect(0, 0, canvSize.Width, canvSize.Height));
        }