Archive for Agosto 5th, 2008

Get Image From Resource (WPF)

CodeKeep C# Feed Agosto 5th, 2008

Description: This describe how to get image from resource file and shows usage

Link: http://www.codekeep.net/snippets/b4cafe76-0ca7-4548-a70c-2c181d9e4167.aspx

  public static T loadResource<T>(string path)
        {
            T c = default(T);
            StreamResourceInfo sri = Application.GetResourceStream(new Uri(path, UriKind.Relative));

            if (sri.ContentType == "application/xaml+xml")
                c = (T)XamlReader.Load(sri.Stream);
            else if (sri.ContentType.IndexOf("image") >= 0)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = sri.Stream;
                bi.EndInit();

                if (typeof(T) == typeof(ImageSource))
                    c = (T)((object)bi);
                else if (typeof(T) == typeof(Image))
                {
                    Image img = new Image();
                    img.Source = bi;
                    c = (T)((object)img);
                }
            }
            sri.Stream.Close();
            sri.Stream.Dispose();
            return c;
        }

Sample Property

CodeKeep C# Feed Agosto 5th, 2008

Description: Nothing exciting, just a sample.

Link: http://www.codekeep.net/snippets/39020ddd-5179-4903-8198-b4f98c81b923.aspx

		public DateTime FileCreated
		{
			get { return fileCreated; }
			set { fileCreated = value; }
		}

check the time elapsed for code

CodeKeep C# Feed Agosto 5th, 2008

Description: use Stopwatch to check how many time has been elapsed for some code

Link: http://www.codekeep.net/snippets/0ad83535-2af7-4830-a77d-e8f4dac7dbf9.aspx

Stopwatch sw;
//start watch
sw = Stopwatch.StartNew();

//do something here
//...

//check the elapsed
Console.WriteLine("{0}: Safe2DimArrayAccess", sw.Elapsed); 

Get and check the key from console

CodeKeep C# Feed Agosto 5th, 2008

Description: Get and check the keys input from console

Link: http://www.codekeep.net/snippets/bdafc009-9d54-41ce-994a-1c6e86bcff73.aspx

			Console.Write("Please enter password: "); 
			while (true) 
			{ 
				ConsoleKeyInfo cki = Console.ReadKey(true); 
				if (cki.Key == ConsoleKey.Enter) break; 
				// Append password characters into the SecureString 
				stringBuilder.Append(cki.KeyChar);
				Console.Write("*");
			}