Thursday, August 15, 2013

The project type is not supported by this installation!!

It is very common to see this error when you try to open a .net project in visual studio. Normally this happens because of different versions of Visual Studio. To open such projects in your VS environment, open .proj file in note pad and remove <ProjectTypeGuids> and save it. Try reloading the project.

Friday, October 5, 2012

Split mXm 2 dimensional Square array into N – Two dimensional Sub Square arrays



Today while working on a relatively complex project I stumbled for couple of hours to split a square matrix into multiple subsets of square arrays. As usual I Googled and Binged for few minutes and did not find any articles or source code on how to tackle the problem, the result is this small article.

Here is my 2 – dimensional array for a Telugu letter మౌ (pronounced as mou).





This is  partially normalized character for 32X32 pixels. To compute Zonal/rotational characteristics of this letter I wanted to split this letter into 4 horizontal and 4 vertical tracks, so that each subset contains 8X8 pixels with total of 16 squares. In other words I wanted to split 32X32 Square into 16 equally sized squares.




Following function returns a jagged array. Jagged array is an array of arrays. In this case I wanted 16, 8X8 sub squares and the result for the above matrix is
  



Function :


 public static int[][,] SplitSquareMatrix(int[,] characterMatrix, int horizintalTrackCountForSubsquares,
            int verticalTrackCountForSubsquares)
        {
            int cwidth = characterMatrix.GetLength(0);
            int cheight = characterMatrix.GetLength(1);
            int xstep = cwidth / verticalTrackCountForSubsquares;
            int ystep = cheight / horizintalTrackCountForSubsquares;
            int xincrement = xstep, yincriment = ystep;          
            int xcor = 0,ycor = 0;

            int loopCount=0;

            int ycounter=0,xcounter=0,j=0,k=0;
            // Declare a jagged array of Squares. Squares is an array of 2 dimensional with
            // (x,y) == (row,column).

            int[][,] Squares = new int[verticalTrackCountForSubsquares * horizintalTrackCountForSubsquares][,];
          
            while (j < horizintalTrackCountForSubsquares)
            {

                while(k<verticalTrackCountForSubsquares)
                {
                    Squares[loopCount]=new int[xstep, ystep];
                    for (int x = xcor; x < xincrement; x++)
                    {
                        if (x >= cwidth) break;
                        for (int y = ycor; y < yincriment; y++)
                        {
                            if (y >= cheight) break;
                            Squares[loopCount][xcounter, ycounter]  = characterMatrix[x, y];
                            ycounter++;
                        }
                        ycounter = 0;
                        xcounter++;
                    }
                    loopCount++;
                    xcounter = 0;
                    xcor = xincrement;
                    xincrement = xincrement + xstep;
                    k++;                   
                }
                // reset variables to next y step.  
                k = 0;
                xcor = 0;
                xincrement = xstep;
                ycor = yincriment;
                yincriment = yincriment + ystep;
                j++;
               
            }
            return Squares;
        }

Tuesday, September 18, 2012

How to draw an alphabet as a bitmap image using C#

The following procedure centers a specified alphabet inside a square. Change Color.FromArgb parameters to draw a coloured text.

usage :  DrawAlphabetImage("లు", 28,"Goutami", 32, 32, @"C:\Taa.bmp");


procedure

 /// <summary>
        ///
        /// </summary>
        /// <param name="alphabet">Alphabet as a string</param>
        /// <param name="fontSize">Size Of the font</param>
        /// <param name="width">Image width</param>
        /// <param name="height">Image Height</param>
        /// <param name="fileName">File name to save bmp image</param>


        private void DrawAlphabetImage(string alphabet,int fontSize,string fontFamily, int width,int height,string fileName)
        {
          
           
                    Bitmap bitmap = new Bitmap(1, 1);
                    Font font = new Font(fontFamily, fontSize, FontStyle.Bold, GraphicsUnit.Pixel);
                    Graphics graphics = Graphics.FromImage(bitmap);
                    bitmap = new Bitmap(bitmap, new Size(width, height));
                    graphics = Graphics.FromImage(bitmap);
                    graphics.Clear(Color.White);
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
                    using (var format = new StringFormat()
                    {
                        Alignment = StringAlignment.Center,
                        LineAlignment = StringAlignment.Center,
                    })
                    {
                        graphics.DrawString(alphabet, font, new SolidBrush(Color.FromArgb(0, 0, 0)), new Rectangle(0, 0, width, height), format);
                    }

                    graphics.Flush();
                    graphics.Dispose();
                    bitmap.Save(fileName);
        }



Do not forget to add the following using statements

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text; 

Tuesday, August 7, 2012

Notes on Android - 1

Following table is the summary of different Android versions and major improvements in a specific version. Although developers has choice of choosing the best version, most of the time the decision to support a certain target should normally not be based on which API you want to use, but instead on what market you are trying to reach.








Here is a snapshot of each version and its market share from google.







if our target is primarily tablets and not cell phones, then we should target
Honeycomb. Honeycomb represents only about 2.4% as of this post date, and not all tablets support Honeycomb.

Here is some more information Barnes & Noble’s Nook uses android 2.2, Amazon kindle fire uses Android 2.3, so supporting older versions makes sence, because when we see the pie, more than 60% of devices still use Android 2.3.3

And finally here is the screen resolution chart.



Saturday, July 28, 2012

Caesar Cipher - RotN


Caesar Cipher, which is also called a substitution cipher, replaces each letter of the alphabet with the three places after that original alphabet. So for a Caesar Cipher the alphabet pattern would be

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

Example : Using Caesar Cipher  “Rapid bits”  translates to  udslg elwv”. Here Rapid bits is plain message (P)  where as “udslg elwv” is  ciphered message (C)

So, if we generalize the idea of replacing each character with a new letter after nth letter, we arrive to the following equation. This can be easily tested by assigning a number to each letter as





Encrypted message is a function of position, so  C=E(P)=f(P+n-1)%26  + 1
Above message can be Decrypted as P=D(C)=(C-n+25)%26 +1

Note in the above equations % is a modulus

Rapid  bits  original number coding without shifting positions is
18 1 16 9 4   2 9 20 19
then if we shift 3 positions, 1st letter “R”,  with number 18, transforms to
C=E(18+3-1)%26 + 1 = 20+1=21
Here is a encrypted message with its number values
21 4 19 12 7  5 12 23 22.

So what is secret here? As you already noticed shifting a letter to nth character is a secret. Since there are only 26 alphabets in English, there are only 25 secret keys. Using brute force attack it is easy to decrypt the messages. However  ASCII table numbering is different from the above 1 to 26 number identification system.

 
 
Here is a script to encrypt a message by choosing a rotating position. You may need to figure out how to decrypt for nth rotation by reversing the flow for different positions.  Rot5, Rot13, Rot18, Rot47 are widely used, but they are not safe at all. Rot is an acronym for rotation.


1)      RotN Encryptor
private string RotNEncrypt(string s, int n, bool includeNumbers)
        {
            string result = "";
            char[] carray = s.ToCharArray();
            for (int i = 0; i < carray.Length; i++)
            {
                int ascii = carray[i];
                int rot = ascii;
                if (ascii > 64 && ascii < 91)
                {
                    rot = rot + n;                   
                    if (rot > 90)
                    {
                        rot += -90 + 64;
                    }
                    if (rot < 65)
                    {
                        rot += -64 + 90;
                    }
                }
                // Lowercase letters are 97 to 122
                else if (ascii > 96 && ascii < 123)
                {
                    rot = rot + n;
                    if (rot > 122) rot += -122 + 96;
                    if (rot < 97) rot += -96 + 122;
                }
                // Numeric digits are 48 to 57
                if (includeNumbers == true && ascii > 47 && ascii < 58)
                {
                    rot = rot + n;
                    if (rot > 47) rot += -57 + 47;
                    if (rot < 58) rot += -47 + 57;
                }

                char character = (Char)rot;
                result = result + character.ToString();
            }
            return result;
        }


2)      Rot13 Encrypter and Decrypter

private string Rot13(string s)
        {
            if (String.IsNullOrEmpty(s)) return s;
            char[] character = s.ToCharArray();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (c >= 97 && c <= 122)
                {
                    int rot = c + 13;
                    if (rot > 122) rot -= 26;
                    character[i] = (char)rot;
                }
                else if (c >= 65 && c <= 90)
                {
                    int rot = c + 13;
                    if (rot > 90) rot -= 26;
                    character[i] = (char)rot;
                }
                else
                {
                    character[i] = c;
                }
            }
            return new string(character);
        }


3)      Rot47 Encryptor and Decryptor

private string Rot47(string s)
        {
            string cipher = "";
            foreach (char c in s.ToCharArray())
            {
                if (c != ' ')
                {
                    int rot = c;
                    rot += 47;
                    if (rot > 126) rot -= 94;
                    if (rot < 33) rot += 94;
                    cipher = cipher + (char)rot.ToString();
                }
                else
                {
                    cipher = cipher + " ";
                }
              
            }
            return cipher;
        }