IService1.cs

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Runtime.Serialization;

5 using System.ServiceModel;

6 using System.ServiceModel.Web;

7 using System.Text;

8 using System.IO;

9

10 namespace PictureService2

11 {

12

13 [ServiceContract]

14 public interface IService1

15 {

16 [OperationContract]

17 [WebGet(UriTemplate="/GetFiles")]

18 List<string> GetFilesName();

19

20

21 [OperationContract]

22 [WebGet(UriTemplate="/GetImage/{imageName}")]

23 Stream GetImage(string imageName);

24

25

26 }

27

28

29

30 }

31


Service1.svc.cs

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Runtime.Serialization;

5 using System.ServiceModel;

6 using System.ServiceModel.Web;

7 using System.Text;

8 using System.IO;

9

10 namespace PictureService2

11 {

12

13 public class Service1 : IService1

14 {

15

16 public List<string> GetFilesName()

17 {

18 DirectoryInfo info = new DirectoryInfo(@"D:\Images");

19 var res = from r in info.GetFiles() select r.Name.Split(".".ToCharArray())[0];

20 return res.ToList();

21 }

22 public Stream GetImage(string imageName)

23 {

24 FileStream fs = null;

25 string name = string.Format(@"D:\Images\{0}.jpg", imageName);

26 fs = File.OpenRead(name);

27 WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

28 return fs;

29 }

30

31 }

32 }

33

Windows 7 phone Application

MainPage.xaml


1 <phoneNavigation:PhoneApplicationPage

2 x:Class="PictureGallery.MainPage"

3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

5 xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation"

6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

8 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"

9 FontFamily="{StaticResource PhoneFontFamilyNormal}"

10 FontSize="{StaticResource PhoneFontSizeNormal}"

11 Foreground="{StaticResource PhoneForegroundBrush}">

12

13 <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">

14 <Grid.RowDefinitions>

15 <RowDefinition Height="Auto"/>

16 <RowDefinition Height="*"/>

17 </Grid.RowDefinitions>

18

19 <!--TitleGrid is the name of the application and page title-->

20 <Grid x:Name="TitleGrid" Grid.Row="0">

21 <TextBlock Text="MY APPLICATION" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/>

22 <TextBlock Text="page title" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/>

23 </Grid>

24

25 <!--ContentGrid is empty. Place new content here-->

26 <Grid x:Name="ContentGrid" Grid.Row="1">

27 <Grid.RowDefinitions>

28 <RowDefinition Height="*"/>

29 <RowDefinition Height="3*"/>

30 </Grid.RowDefinitions>

31 <StackPanel Orientation="Horizontal" Grid.Row="0">

32 <Button x:Name="myButton" Content="GetImagesName" Height="50" />

33 <ComboBox x:Name="cmbName" Height="50" Width="211" Foreground="Black" />

34 </StackPanel>

35 <Image Grid.Row="1" x:Name="myImage" Height="Auto" Width="Auto" />

36 </Grid>

37 </Grid>

38

39 </phoneNavigation:PhoneApplicationPage>

MainPage.xaml.cs

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Net;

5 using System.Windows;

6 using System.Windows.Controls;

7 using System.Windows.Documents;

8 using System.Windows.Input;

9 using System.Windows.Media;

10 using System.Windows.Media.Animation;

11 using System.Windows.Shapes;

12 using Microsoft.Phone.Controls;

13 using System.Runtime.Serialization;

14 using System.Windows.Media.Imaging;

15

16

17 namespace PictureGallery

18 {

19 public partial class MainPage : PhoneApplicationPage

20 {

21 public MainPage()

22 {

23 InitializeComponent();

24

25 SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

26 myButton.Click += new RoutedEventHandler(myButton_Click);

27 cmbName.SelectionChanged += new SelectionChangedEventHandler(cmbName_SelectionChanged);

28

29 }

30

31 void cmbName_SelectionChanged(object sender, SelectionChangedEventArgs e)

32 {

33 if (cmbName.SelectedIndex != -1)

34 {

35 string serviceURI = string.Format("http://localhost:19612/Service1.svc/GetImage/{0}", cmbName.SelectedItem.ToString());

36 WebClient proxy1 = new WebClient();

37 proxy1.OpenReadCompleted += new OpenReadCompletedEventHandler(GetImage);

38 proxy1.OpenReadAsync(new Uri(serviceURI));

39 }

40 }

41

42 void GetImage(object sender, OpenReadCompletedEventArgs e)

43 {

44

45 BitmapImage img = new BitmapImage();

46 img.SetSource(e.Result);

47 myImage.Source = img;

48

49 }

50

51 void myButton_Click(object sender, RoutedEventArgs e)

52 {

53 cmbName.Items.Clear();

54 cmbName.SelectedIndex = -1;

55 WebClient proxy = new WebClient();

56 proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(GetFileName);

57 proxy.OpenReadAsync(new Uri("http://localhost:19612/Service1.svc/GetFIles"));

58

59 }

60

61 void GetFileName(object sender, OpenReadCompletedEventArgs e)

62 {

63

64 DataContractSerializer serz = new DataContractSerializer(typeof(List<string>));

65 List<string > fileNames  = (List<string>) serz.ReadObject(e.Result);

66 cmbName.ItemsSource = fileNames;

67 }

68

69

70

71

72 }

73 }

74

75

76

77

Advertisements

One thought on “Video on Picture Gallery for Windows 7 Phone using WCF REST Service

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s