24 Mart 2016 Perşembe

picturebox image download c#

PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(New Uri(link))))

14 Mart 2016 Pazartesi

c# ip ülke kodu

WebClient wc = new WebClient();
var strJson = wc.DownloadString(“http://ipinfo.io/” + Request.ServerVariables[“REMOTE_ADDR”]);
var ip = Newtonsoft.Json.Linq.JObject.Parse(strJson);
var country = ip[“country”].ToString();

C#.net - Get MAC Address

C#.net - Get MAC Address 


Sometime in our application we need to fetch MAC Address of network adapter card.In this article i will show you how to get a MAC Address.


Step 1
Create a Console Application and give the solution name as  ConMACAddress.


Step 2
Add System.Management assembly reference to the project from solution explorer,it is look like this





Click on image for better view


Step 3
Write a static method for get MAC Address,it is look like this


  1. public static String GetMACAddress()  
  2.        {  
  3.            #region Get MAC Address  
  4.            try  
  5.            {  
  6.                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");  
  7.                ManagementObjectCollection moc = mc.GetInstances();  
  8.                string MACAddress = String.Empty;  
  9.                foreach (ManagementObject mo in moc)  
  10.                {  
  11.                    if (MACAddress == String.Empty) // only return MAC Address from first card  
  12.                    {  
  13.                        if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();  
  14.                    }  
  15.                    mo.Dispose();  
  16.                }  
  17.   
  18.                MACAddress = MACAddress.Replace(":""");  
  19.                return MACAddress;  
  20.            }  
  21.            catch (Exception ex)  
  22.            {  
  23.                throw new Exception(ex.Message);  
  24.            }  
  25.            #endregion  
  26.        }  

Step 4
Call above function in main method,it is look like this

  1. static void Main(string[] args)  
  2.         {  
  3.             try  
  4.             {  
  5.                 System.Console.WriteLine("MAC Address\t:\t" + GetMACAddress());    
  6.             }  
  7.             catch (Exception ex)  
  8.             {  
  9.                 System.Console.WriteLine(ex.Message);     
  10.             }  
  11.         }  

Run the Project.


Full Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Management;  
  6.   
  7. namespace ConMACAddress  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             try  
  14.             {  
  15.                 System.Console.WriteLine("MAC Address\t:\t" + GetMACAddress());    
  16.             }  
  17.             catch (Exception ex)  
  18.             {  
  19.                 System.Console.WriteLine(ex.Message);     
  20.             }  
  21.         }  
  22.   
  23.         public static String GetMACAddress()  
  24.         {  
  25.             #region Get MAC Address  
  26.             try  
  27.             {  
  28.                 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");  
  29.                 ManagementObjectCollection moc = mc.GetInstances();  
  30.                 string MACAddress = String.Empty;  
  31.                 foreach (ManagementObject mo in moc)  
  32.                 {  
  33.                     if (MACAddress == String.Empty) // only return MAC Address from first card  
  34.                     {  
  35.                         if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();  
  36.                     }  
  37.                     mo.Dispose();  
  38.                 }  
  39.   
  40.                 MACAddress = MACAddress.Replace(":""");  
  41.                 return MACAddress;  
  42.             }  
  43.             catch (Exception ex)  
  44.             {  
  45.                 throw new Exception(ex.Message);  
  46.             }  
  47.             #endregion  
  48.         }  
  49.     }  
  50. }  

Download
Download Source Code

WPF - Image Gallery in WPF

In this article i will show you how to create a Dynamic Image Gallery in WPF application by using Listbox and UniformGrid Panel.

Step 1
Create a WPF Application and give solution name as WpfImageGallery.

Step 2
Create a New Folder in the Solution and give folder name as Images,it is look like this



Click on Image for Better View

Step 3
Create a XML file in Application BaseDirectory and give file name asImageData.xml.(./bin/debug/ImageData.xml)

Step 4
The XML in the following example defines an XML document with a root node called Images. This root node contains one or more nodes called Image that include elements called
ImageName.,it is look like this






<?xml version="1.0" encoding="utf-8" ?>
<Images>

<Image>
    <ImagePath>../Images/Gaara.png</ImagePath>
  </Image>

  <Image>
    <ImagePath>../Images/Choji.png</ImagePath>
  </Image>

  <Image>
    <ImagePath>../Images/Jiraiya.jpg</ImagePath>
  </Image>

  <Image>
    <ImagePath>../Images/Kakashi.png</ImagePath>
  </Image>

<Image>
    <ImagePath>../Images/Kiba.png</ImagePath>
  </Image>
<Image>
    <ImagePath>../Images/Naruto.jpg</ImagePath>
  </Image>

<Image>
    <ImagePath>../Images/Neji.jpg</ImagePath>
  </Image>

<Image>
    <ImagePath>../Images/RockLee.jpg</ImagePath>
  </Image>

<Image>
    <ImagePath>../Images/Sai.jpg</ImagePath>
  </Image>

<Image>
    <ImagePath>../Images/Shikamaru.jpg</ImagePath>
  </Image>

<Image>
    <ImagePath>../Images/Shino.jpg</ImagePath>
  </Image>

<Image>
    <ImagePath>../Images/Yamato.jpg</ImagePath>
  </Image>

</Images>

Step 5
Create a ImageEntity Class in the solution.it is an Entity class,it is look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfImageGallery
{
    public class ImageEntity
    {
        #region Property

      
        public String ImagePath
        {
            get;
            set;
        }

        #endregion
    }
}
Step 6
Create a ImageView Static Class in a solution for retriving Image Path from XML Document,it is look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace WpfImageGallery
{
    public static class ImageView
    {
        #region Methods

        public static List<ImageEntity> GetAllImagesData()
        {
            try
            {
                // Load Xml Document
                XDocument XDoc = XDocument.Load("ImageData.xml");

                // Query for retriving all Images data from XML
                var Query = from Q in XDoc.Descendants("Image")
                            select new ImageEntity
                            {
                                ImagePath = Q.Element("ImagePath").Value
                            };

                // return images data
                return Query.ToList<ImageEntity>();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion
    }
}
Step 7
Now design Image gallery on window.Drag and drop ListBox Control from Toolbox and
set a Background Color,it is look like this
<ListBox x:Name="LsImageGallery">
            <ListBox.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FF1E2A2F" Offset="1"/>
                </LinearGradientBrush>
            </ListBox.Background>
        </ListBox>




Step 8
Create a DataTemplate and ItemPanelTemplate to a Listbox control,it is look like this
<Window.Resources>

        
        <DataTemplate x:Key="ImageGalleryDataTemplate">
            <Grid>
                <Border BorderBrush="#FFFF9800" BorderThickness="3"  Width="120" Height="120" Padding="10" Margin="15" CornerRadius="10">
                    <!--Bind Image Path in Image Control-->
                    <Image Source="{Binding ImagePath}" Stretch="Fill"  HorizontalAlignment="Center">
                            <!--View Large Image on Image Control Tooltip-->
                            <Image.ToolTip>
                                <Grid>
                                    <Image Source="{Binding ImagePath}" Stretch="Fill" HorizontalAlignment="Center" Height="200" Width="200"></Image>
                                </Grid>    
                            </Image.ToolTip>                
                            
                    </Image>

                </Border>
            </Grid>
        </DataTemplate>
        
        <ItemsPanelTemplate x:Key="ImageGalleryItemsPanelTemplate">
            
            <!--Display Images on UniformGrid Panel-->
            <UniformGrid Columns="4" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
            
        </ItemsPanelTemplate>

        
    </Window.Resources>
Step 9
Apply DataTemplate and ItemPanelTemplate on ListBox Control,it is look like this
<ListBox x:Name="LsImageGallery"  ItemsSource="{Binding}" ItemTemplate="{DynamicResource ImageGalleryDataTemplate}" ItemsPanel="{DynamicResource ImageGalleryItemsPanelTemplate}">
            <ListBox.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FF1E2A2F" Offset="1"/>
                </LinearGradientBrush>
            </ListBox.Background>
        </ListBox>
ItemSource Property - Gets or sets a collection used to generate the content of the ItemsControl.
ItemTemplate Property - Gets or sets the DataTemplate used to display each item.
ItemPanel Property -  Gets or sets the template that defines the panel that controls the layout of items.
Step 10
Now Bind the data in ListBox Control in Code Behind,it is look like this
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfImageGallery
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();

            try
            {
                BindImages(); // Bind Image in Constructor
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);  
            }
        }

        /// <summary>
        /// Bind Image in List Box Control
        /// </summary>
        private void BindImages()
        {
            try
            {
                // Store Data in List Object
                List<ImageEntity> ListImageObj = ImageView.GetAllImagesData();

                // Check List Object Count
                if (ListImageObj.Count > 0)
                {
                    // Bind Data in List Box Control.
                    LsImageGallery.DataContext = ListImageObj;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
Run The Project.
Output


















Move the Mouse Pointer to the Image




















Full XAML Code

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    x:Class="WpfImageGallery.MainWindow"
    x:Name="Window"
    Title="Image Gallery"
    Width="640" Height="500">
    
    <Window.Resources>

        
        <DataTemplate x:Key="ImageGalleryDataTemplate">
            <Grid>
                <Border BorderBrush="#FFFF9800" BorderThickness="3"  Width="120" Height="120" Padding="10" Margin="15" CornerRadius="10">
                    <!--Bind Image Path in Image Control-->
                    <Image Source="{Binding ImagePath}" Stretch="Fill"  HorizontalAlignment="Center">
                            <!--View Large Image on Image Control Tooltip-->
                            <Image.ToolTip>
                                <Grid>
                                    <Image Source="{Binding ImagePath}" Stretch="Fill" HorizontalAlignment="Center" Height="200" Width="200"></Image>
                                </Grid>    
                            </Image.ToolTip>                
                            
                    </Image>

                </Border>
            </Grid>
        </DataTemplate>
        
        <ItemsPanelTemplate x:Key="ImageGalleryItemsPanelTemplate">
            
            <!--Display Images on UniformGrid Panel-->
            <UniformGrid Columns="4" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
            
        </ItemsPanelTemplate>

        
    </Window.Resources>

    <Grid x:Name="LayoutRoot">
        
        <ListBox x:Name="LsImageGallery"  ItemsSource="{Binding}" ItemTemplate="{DynamicResource ImageGalleryDataTemplate}" ItemsPanel="{DynamicResource ImageGalleryItemsPanelTemplate}">
            <ListBox.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black"/>
                    <GradientStop Color="#FF1E2A2F" Offset="1"/>
                </LinearGradientBrush>
            </ListBox.Background>
        </ListBox>
        
    </Grid>
</Window>


Download
Download Source Code 

I had created another Image Gallery.Please Check it.I hope you Like it.
Download Source Code from the following Link
 
Downalod Source Code - ImageGallery_2

Output