Posts Tagged ‘C#’
21
Sep

Convert color string to rgb value

Post a comment » Popularity: 1%

This following codes convert an ordinary color string (eg: #FF0000) into RGB value.

Convert color string to rgb value

convert hex color string to rgb:
public static Color CovnertToColor(string colorString)
{
            byte a = 255;
            byte r = (byte)(Convert.ToUInt32(colorString.Substring(1, 2), 16));
            byte g = (byte)(Convert.ToUInt32(colorString.Substring(3, 2), 16));
            byte b = (byte)(Convert.ToUInt32(colorString.Substring(5, 2), 16));
            return Color.FromArgb(a, r, g, b);
}

// or

public static Color ParseColor(string value)
{
    value = value.Replace("#", "");
    Int32 v = Int32.Parse(value, System.Globalization.NumberStyles.HexNumber);
    return new Color()
    {
        A = 255,
        R = Convert.ToByte((v >> 16) & 255),
        G = Convert.ToByte((v >> 8) & 255),
        B = Convert.ToByte((v >> 0) & 255)
    };
}

// Create random color
Random r = new Random();
byte red = (byte)(255 * r.NextDouble());
byte green = (byte)(255 * r.NextDouble());
byte blue = (byte)(255 * r.NextDouble());
Color color = Color.FromArgb(255, red, green, blue);  
19
Sep

Create Storyboard using C# and XAML

Post a comment » Popularity: 1%

Kris Meeusen demonstrated how to create Storyboard animation identically using C# or XAML. A good article for you to reference how to create your first animation.

Storyboard

View Demo: New Window
19
Sep

HTTP Post Request

Post a comment » Popularity: 1%

This coding below demonstrated how you can POST data to the target remote server. Terence Tsang also provided a simple sample letting you to search the feed items of your blog (Only works for Wordpress).

 

If you are new to Silverlight networking,  remember to check out Network Security Access Restrictions in Silverlight.

HTTP Post Request

View Demo: New Window
Sending Post Data to Server:
// Create a request object
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(POST_ADDRESS, UriKind.Absolute));
request.Method = "POST";
// don't miss out this
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);  

// Sumbit the Post Data
void RequestReady(IAsyncResult asyncResult)
{
    HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
    Stream stream = request.EndGetRequestStream(asyncResult);  

    // Hack for solving multi-threading problem
    this.Dispatcher.BeginInvoke(delegate()
    {
        // Send the post variables
        StreamWriter writer = new StreamWriter(stream);
        writer.WriteLine("key1=value1");
        writer.WriteLine("key2=value2");
        writer.Flush();
        writer.Close();  

        request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
    });
}  

// Get the result  from the server
void ResponseReady(IAsyncResult asyncResult)
{
    HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);  

    this.Dispatcher.BeginInvoke(delegate()
    {
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        // get the result text
        string result = reader.ReadToEnd();
    });
}  
17
Sep

Animated 3D Text

Post a comment » Popularity: 1%

Charles Petzold created a rotating text sample using perspective 3D. It’s easy to be achieved by code. However, he achieved the implementation without writing any C#.

Animated-3D-Text

View Demo: New Window
Creating 3D Rotate Text using XAML:


    
        
            
            
        

        
            
                
                    
                
            
        
    

    
        
            
                
                    
                
            
        
    

15
Sep

Simulate Double Click

Post a comment » Popularity: 1%

There is no native support for double click in Silverlight. Mike has a workaround to solve this problem. This is achieved by using a combination of DispatcherTimer and MouseDown Event.

 double-click

View Demo: New Window
How to simulate double click in C#:
DispatcherTimer _doubleClickTimer;

public Page()
{
    InitializeComponent();
    _doubleClickTimer = new DispatcherTimer();
    _doubleClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
    _doubleClickTimer.Tick += new EventHandler(DoubleClick_Timer);
    this.MouseLeftButtonDown += new MouseButtonEventHandler(Page_MouseLeftButtonDown);
}

// too much time has passed for it to be a double click.
void DoubleClick_Timer(object sender, EventArgs e)
{
    _doubleClickTimer.Stop();
}

void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (_doubleClickTimer.IsEnabled)
    {
        // a double click has occured
        _doubleClickTimer.Stop();
    }
    else
    {
        _doubleClickTimer.Start();
    }
}
11
Sep

Switching to Full Screen

Post a comment » Popularity: 1%

Switch to full screen can be achieved by just one line of coding. However, please note that the method can be called under certain user interaction, such as Mouse Click.

Switching to Full Screen

View Demo: New Window
Switch to full screen in C#:
private void FullScreen_Click(object sender, RoutedEventArgs e)
{
    // This can be called upon a mouse event
    Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
}