Use streetview in windows forms

Topics: Windows Forms
Apr 3, 2011 at 7:42 PM

Hi,

how can i use street view in windows forms with  C# and .Net 4 ?

I add PanoramaViewer to project but who can i  add viewr control to windows forms ?

Apr 3, 2011 at 7:46 PM

it's wpf control, i don't know much about hosting wpf in windows forms

Apr 3, 2011 at 7:49 PM

if i can do this i postet here.

;))

Apr 3, 2011 at 9:00 PM

Can you help me.

i add PanoramaViewer  class to project and then do this.

Please tell me where of this code is wrong ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace StreetViewSample
{
    public partial class Form1 : Form
    {
         StreetViewSample.PanoramaViewer Viewer = new PanoramaViewer();
        
        BackgroundWorker loader = new BackgroundWorker();
        StackPanel buff = new StackPanel();

        public Form1()
        {
            InitializeComponent();

            Viewer.MouseLeftButtonDown += new MouseButtonEventHandler(Viewer_MouseLeftButtonDown);
            Viewer.MouseMove += new System.Windows.Input.MouseEventHandler(Viewer_MouseMove);
            buff.Orientation = System.Windows.Controls.Orientation.Vertical;

            // removes white lines between tiles!
            //DependencyObject.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
            loader.DoWork += new DoWorkEventHandler(loader_DoWork);
            loader.ProgressChanged += new ProgressChangedEventHandler(loader_ProgressChanged);
            loader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(loader_RunWorkerCompleted);
            loader.WorkerReportsProgress = true;
        }
        void loader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            buff.UpdateLayout();

            Canvas canvas = new Canvas();
            canvas.Children.Add(buff);
            canvas.Width = 512 * 13;
            canvas.Height = 512 * 7;

            canvas.UpdateLayout();

            canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
            canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
            int Height = ((int)(canvas.ActualHeight));
            int Width = ((int)(canvas.ActualWidth));

            RenderTargetBitmap _RenderTargetBitmap = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32);
            _RenderTargetBitmap.Render(buff);

            Image img = new Image();
            img.Source = _RenderTargetBitmap;

            Viewer.PanoramaImage = _RenderTargetBitmap;

            this.Text = "Demo.StreetView, enjoy! ;}";
        }

        Vector RotationVector = new Vector();
        Point DownPoint = new Point();
        void Viewer_MouseMove(object sender,System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
                return;
            Vector Offset = Point.Subtract(e.GetPosition(Viewer), DownPoint) * 0.25;

            Viewer.RotationY = RotationVector.Y + Offset.X;
            Viewer.RotationX = RotationVector.X - Offset.Y;
        }

        void Viewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DownPoint = e.GetPosition(Viewer);
            RotationVector.X = Viewer.RotationX;
            RotationVector.Y = Viewer.RotationY;
            this.Cursor = System.Windows.Forms.Cursors.SizeAll;
        }

        private void Viewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.Cursor = System.Windows.Forms.Cursors.Arrow;
        }

        void loader_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 100)
            {
                Pass p = e.UserState as Pass;

                this.Text = "Demo.StreetView, please wait on first time loading: " + p.X + "|" + p.Y + " of 13";
                Image i = new Image();

                i.Source = p.src;
                (buff.Children[buff.Children.Count - 1] as StackPanel).Children.Add(i);
            }
            else if (e.ProgressPercentage == 0)
            {
                this.Text = "Demo.StreetView, please wait on first time loading: zooming...";

                StackPanel ph = new StackPanel();
                ph.Orientation = System.Windows.Controls.Orientation.Horizontal;
                buff.Children.Add(ph);
            }
        }

        void loader_DoWork(object sender, DoWorkEventArgs e)
        {
            string panoId = "rIcDg5NpZyolFR3i98C_3Q";
            int zoom = 4;

            //0, 1
            //1, 2   
            //2, 4
            //3, 7   
            //4, 13  
            //5, 26  

            for (int y = 0; y <= zoom + 1; y++)
            {
                loader.ReportProgress(0);

                for (int x = 0; x < 13; x++)
                {
                    Pass p = new Pass();
                    p.Y = y;
                    p.X = x;

                    string fl = "Tiles\\" + zoom + "\\" + panoId + "\\img_" + x + "_" + y + ".jpg";
                    string dr = System.IO.Path.GetDirectoryName(fl);
                    if (!Directory.Exists(dr))
                    {
                        Directory.CreateDirectory(dr);
                    }
                    if (!File.Exists(fl))
                    {
                        ImageSource src = Get(string.Format("http://cbk{0}.google.com/cbk?output=tile&panoid={1}&zoom={2}&x={3}&y={4}&cb_client=maps_sv", (x + 2 * y) % 3, panoId, zoom, x, y));
                        p.src = src;
                        SaveImg(src, fl);
                    }
                    else
                    {
                        using (Stream s = File.OpenRead(fl))
                        {
                            p.src = FromStream(s);
                        }
                    }

                    loader.ReportProgress(100, p);
                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        void SaveImg(ImageSource src, string file)
        {
            using (Stream s = File.OpenWrite(file))
            {
                JpegBitmapEncoder e = new JpegBitmapEncoder();
                e.Frames.Add(BitmapFrame.Create(src as BitmapSource));
                e.Save(s);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loader.RunWorkerAsync();
        }

        public Stream CopyStream(Stream inputStream)
        {
            const int readSize = 256;
            byte[] buffer = new byte[readSize];
            MemoryStream ms = new MemoryStream();

            using (inputStream)
            {
                int count = inputStream.Read(buffer, 0, readSize);
                while (count > 0)
                {
                    ms.Write(buffer, 0, count);
                    count = inputStream.Read(buffer, 0, readSize);
                }
            }
            buffer = null;
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }

        ImageSource FromStream(Stream stream)
        {
            ImageSource ret = null;
            if (stream != null)
            {
                {
                    // try png decoder
                    try
                    {
                        JpegBitmapDecoder bitmapDecoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                        ImageSource m = bitmapDecoder.Frames[0];

                        if (m != null)
                        {
                            ret = m;
                        }
                    }
                    catch
                    {
                        ret = null;
                    }

                    // try jpeg decoder
                    if (ret == null)
                    {
                        try
                        {
                            stream.Seek(0, SeekOrigin.Begin);

                            PngBitmapDecoder bitmapDecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                            ImageSource m = bitmapDecoder.Frames[0];

                            if (m != null)
                            {
                                ret = m;
                            }
                        }
                        catch
                        {
                            ret = null;
                        }
                    }
                }
            }
            return ret;
        }

        ImageSource Get(string url)
        {
            ImageSource ret = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ServicePoint.ConnectionLimit = 50;
                request.Proxy = WebRequest.DefaultWebProxy;

                request.UserAgent = "Opera/9.62 (Windows NT 5.1; U; en) Presto/2.1.1";
                request.Timeout = 10 * 1000;
                request.ReadWriteTimeout = request.Timeout * 6;
                request.Referer = "http://maps.google.com/";
                request.KeepAlive = true;

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    using (Stream responseStream = CopyStream(response.GetResponseStream()))
                    {
                        ret = FromStream(responseStream);
                    }
                }
            }
            catch (Exception)
            {
                ret = null;
            }
            return ret;
        }
    }

    class Pass
    {
        public ImageSource src;
        public int Y;
        public int X;
    }
}

Apr 4, 2011 at 7:40 AM

please be more concrete ;}

Apr 4, 2011 at 3:45 PM

Hi

i create wpf control for streetview then use in windows application

see this link

http://20uploads.com/files/1390/farvardin/6deb_StreetViewSample.zip

Apr 5, 2011 at 7:28 AM

nice it works, so there is hosting element for wpf, System.Windows.Forms.Integration.ElementHost

Apr 21, 2011 at 5:26 PM

Hi,

I've attemp several ways to incorporate streetview on a vs2008 windows form.

Is there any way to do it or I'm attempting a impossible thing?

I've seen that your wpf control is using FrameWork 4.

Thanks

Apr 22, 2011 at 6:26 AM
Edited Apr 22, 2011 at 6:26 AM

check he's example