Archive for January 2012

Convert time in seconds to HH:MM:SS

## Convert time in seconds to hours:minutes:seconds
# @param sec Time in seconds
# @return The time in hh:mm:ss format
def SecToTime(Sec): 
  H = int(Sec / 3600)
  M = int(Sec / 60 - H * 60)
  S = int(Sec - (H * 3600 + M * 60))

  if len(str(H)) == 1: time = "0" + str(H) + ":"
  else: time = str(H) + ":"
 
  if len(str(M)) == 1: time = time + "0" + str(M) + ":"
  else: time = time + str(M) + ":"

  if len(str(S)) == 1: time = time + "0" + str(S) 
  else: time = time + str(S) 
  
  return time
  pass

Get Web Page Content as a String with C#

This function dowloads the html content of a webpage and returns the content as a string.

public static string GetWebPageAsString(string url) {
    HttpWebRequest httpWebRequest =(HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    Stream stream = httpWebResponse.GetResponseStream();
    StreamReader streamReader = new StreamReader(stream, Encoding.ASCII);
    return streamReader.ReadToEnd();
}

Recursive delete files and folders using C#

This function will Recursively delete all files and folders.

private void DeleteAllFiles(string sPath)
{
    foreach(string strFile in Directory.GetFiles(sPath))
    {
        File.Delete(strFile);
    }
    foreach (string strDir in Directory.GetDirectories(sPath))
    {
        DeleteAllFiles(strDir);
    }
    Directory.Delete(sPath);
}

Create a autocompleting textbox using C#

The code example below creates an autocompleting textbox, with the possible values Andy, Andrew, Dylan and Kyle.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace autoComplete
{
    public partial class Form1 : Form
    {
        AutoCompleteStringCollection nameCollection = new AutoCompleteStringCollection();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            nameCollection.Add("Andy");
            nameCollection.Add("Andrew");
            nameCollection.Add("Dylan");
            nameCollection.Add("Kyle");

            txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtName.AutoCompleteCustomSource = nameCollection;
        }
    }
}

Create arraylist

Create an ArrayList and add three elements.

using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList list = new ArrayList();
        list.Add("One");
        list.Add("Two");
        list.Add("Three");
    }
}

Wood Cells

Analysis of wood cells using halcon.

read_image (Woodcells1, 'C:\\Program Files\\MVTec\\HALCON\\images\\woodcell.png')
X := 20
threshold (Woodcells1, CellBoarder, 0, 120)
get_image_pointer1 (Woodcells1, Pointer, Type, Width, Height)
open_file ('wood_cells.dat', 'output', FileHandle)
for i := 0 to Width-X-1 by 1
    clip_region (CellBoarder, Part, 0, i, Height-1, i+X)
    area_center (Part, Area, Row, Col)
    fwrite_string (FileHandle, i + ' ' + (Area * 100.0 / (X * Height)))
    fnew_line (FileHandle)
endfor
close_file (FileHandle)