Category Archives: C#

C#: How to (parse the text content)/ read from Microsoft Word Document doc

Get the Text out of a Microsoft word file. Read from MS word.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices.ComTypes;
 
namespace readDOC
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object miss = System.Reflection.Missing.Value;
            object path = @"C:\DOC\myDocument.docx";
            object readOnly = true;
            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            string totaltext = "";
            for (int i = 0; i < docs.Paragraphs.Count; i++)
            {
                   totaltext += " \r\n "+ docs.Paragraphs[i+1].Range.Text.ToString();
            }
            Console.WriteLine(totaltext);
            docs.Close();
            word.Quit();
        }
    }
}

C#: How to programmatically resize images or create thumbnails from a variety of differently sized images using ffmpeg

ImageMagick should be used to do this,  ffmpeg is for AV.

But to do this using ffmpeg and c#, follow these steps….

You can download ffmpeg for free at http://ffmpeg.org/download.html

My images exists in C:\image_directory and my ffmpeg exists in C:\ffmpeg.

 

I will be using the following ffmpeg command to resize each image (create a thumbnail):

C:\ffmpeg\bin>ffmpeg -i c:\image_directory\1.png -s 80×80 c:\image_directory\t\t_1.png


The above command will take an input image 1.png, resize it to 80px by 80px, save it in a sub-folder named \t\,
and append “t_” to the begining of the file name.

Here is how you would loop through every image in the folder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
 
namespace ImgResize
{
    class Program
    {
        static void Main(string[] args)
        {
            //get an array of all the image files//
            string[] imgPaths = Directory.GetFiles(@"c:\image_directory\");
 
            //loop through each image//
            for (int i = 0; i &lt; imgPaths.Length; i++)
            {
                string file_path = imgPaths[i];
                //get the file name out of full path//
                string filename = Regex.Match(imgPaths[i], @".*\\([^\\]+$)").Groups[1].Value;
                //output the filename about to be converted
                Console.WriteLine("converting : "+filename);
                //make arguements string
                string write2string = " -i " + file_path + " -s 80x80 "+@"c:\\image_directory\\t\\t_" + filename;
                //create a process
                Process myProcess = new Process();
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.RedirectStandardOutput = true;
                //point ffmpeg location
                myProcess.StartInfo.FileName = @"c:\\ffmpeg\\bin\\ffmpeg";
                //set arguements
                myProcess.StartInfo.Arguments = write2string;
                myProcess.Start();
                myProcess.WaitForExit();
            }
        }
    }
}

C#: Scrabble Player; Part 2 – Find all permutations of a string, parse a dictionary, cheat at Scrabble.

This an extension of a post from before.

C#: Find all permutations of a string, parse a dictionary, cheat at Scrabble.

ScrabblePlayer is a combination of my own joyous curiosity and an accumulation of my previous posts.  It is not built for performance.

This program accepts an input of English Letters from the user.  It then, figures out all possible permutations of the characters(letters), as well as, all permutations of sub-combinations down to a length of two.

Example: User inputs the word “abcd” ;

sub-combinations and -> permutations of input “abcd

each set has its own permutations recurviely.

1.) abcd : 4 characters, 24 permutations; (1*2*3*4)
2.) abc  : 3 characters, 6  permutations; (1*2*3)
3.) ab   : 2 characters, 2  permutations; (1*2)

Next, ScrabblePlayer parses a set of XML files which contain Websters Dictionary content.  The parsing populates both a LinkedList and a HashTable.  LinkedList<string> is set to the word, HashTable<string, string> is set to word as Key, and definition as Value.

After a LinkedList of both the accumulated permutations and dictionary words is created, ScrabblePlayer compares them.  If a permutation of the User’s input is found in the dictionary word list, it is displayed along with its definition.

The dictionary can be downloaded at http://www.ibiblio.org/webster/. I have renamed all the files ( A.xml, B.xml, C.xml, and so on ) .

Continue reading

C#: Find all permutations of a string, parse a dictionary, cheat at Scrabble.

This program has been expanded in a later post:
C#: Scrabble Player; Part 2 – Find all permutations of a string, parse a dictionary, cheat at Scrabble.

Find all possible permutations of a string.  Parse an XML dictionary http://www.ibiblio.org/webster/.  Compare permutations with dictionary and display results.  Ofcourse, the following program will only find words the same length as letters inputted.

Continue reading