Thursday, January 3, 2013

Silverlight : Ger ByteArry from Image File and Convert ByteArray to ImageSource

 Ger ByteArry from Image File

To Read ByteArray from FileInfo using OpenFileDialog, Steps Are :

1. First Create Method that takes Stream as input and return Byte[].


 public static byte[] ReadBytesFromStream(Stream stream)
 {
 byte[] size = new byte[16 * 1024];
 using (MemoryStream memoryStream = new MemoryStream())
 {
  int readCount;
  while ((readCount = stream.Read(size, 0, size.Length)) > 0)
  {
   memoryStream.Write(size, 0, readCount);
  }
  return memoryStream.ToArray();
 }
} 
 
  
2. Create Button for Upload Image, write following code on Button Click event.

private void UploadImageButton_Click(object sender, RoutedEventArgs e)
{
 OpenFileDialog filedlg = new OpenFileDialog();
 dlg.Multiselect = false;
    dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png|JPEG Images(*.jpeg)|*.jpeg";
 
 bool? retval = dlg.ShowDialog();
 
 if (retval != null && retval == true)
 {
  byte[] getArray = ReadBytesFromStream(dlg.File.OpenRead());  
 }
}


 Get Image From ByteArry


your ByteArray  : byte[] getArray

 
using (MemoryStream memoryStream = new MemoryStream(getArray, 0,
     imageBytes.Length))
{
 BitmapImage image = new BitmapImage();
 image.SetSource(memoryStream );
}

Change ScrollViewer Style of ListBox in Silverlight

To change Silverlight ListBox ScrollViewer default style, you have to change its ControlTemplate.

Please follow these steps to change Default style of ListBox Scrollviewer.

1.  Let's say your ScrollViewer Style : MyScrollViewerStyle

2.  Change ControlTemplate, like this :

    <ListBox.Template>
 <ControlTemplate TargetType="ItemsControl">
      <ScrollViewer Style="{StaticResource MyScrollViewerStyle}">
  <ItemsPresenter />
      </ScrollViewer>
 </ControlTemplate>
     </ListBox.Template>
   
here in ControlTemplate add ScrollViewer and set your style, and don't forget to add <ItemPresenter/> within ScrollViewer.
because without ItemPresenter your DataItems is not visible in ListBox.

3. Full Source.

 <ListBox ItemsSource="{Binding EmployeeCollection}" 
                 ScrollViewer.VerticalScrollBarVisibility="Auto">
 <ListBox.ItemContainerStyle>
  <Style TargetType="ListBoxItem">
   <Setter Property="HorizontalContentAlignment" 
                                Value="Stretch" />
  </Style>
 </ListBox.ItemContainerStyle>
 <ListBox.Template>
  <ControlTemplate TargetType="ItemsControl">
       <ScrollViewer Style="{StaticResource MyScrollViewerStyle}">
      <ItemsPresenter />
        </ScrollViewer>
  </ControlTemplate>
 </ListBox.Template>
 <ListBox.ItemTemplate>
  <DataTemplate> 
                    .......
                     Your Data to Format
                    .......        
                </DataTemplate>
        </ListBox.ItemTemplate> 
 </ListBox>   
 


Tuesday, January 1, 2013

Get Host Name and IP Address in Silverlight

1. Open aspx page in which xap file is hosted.

2. Write following code in Page_Load Event.


<script runat="server">  
   public string HostName {get; set;}
   public string UserIp {get; set;} 
  protected void Page_Load(object sender, EventArgs e)
  {
 //Code for get system IP
  
 HostName=System.Net.Dns.GetHostName();
 System.Net.IPHostEntryipEntry=System.Net.Dns.GetHostEntry(strHostName);
 System.Net.IPAddress[]addr=ipEntry.AddressList;
 foreach(System.Net.IPAddress ip in addr)
 {
  if(ip.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork)
  {
   UserIp=ip.ToString();     
   break;
  }
 }  
  }

</script>
 
3. Now you can pass IP and Host Name to Silverlight application
using parameter in initParams tag with key-value pair.

 <param name="initParams"  
    value="<%= string.Format("HostName={0},HostIP={1}", HostName, UserIp) %>" /> 


4. Get passed Parameter values in Application_Startup event  in App.xaml.cs file in Silverlight Application. 

private void Application_Startup(object sender, StartupEventArgs e) 

      if (e.InitParams.ContainsKey("HostName")) 
      { 
            TerminalModel.Instance.HostName = e.InitParams["HostName"]; 
      } 
      if (e.InitParams.ContainsKey("HostIP")) 
      { 
            TerminalModel.Instance.HostIP = e.InitParams["HostIP"]; 
      }
}