Tuesday, July 31, 2012

How to set Hyperlink and bookmark in MS Word-2010

  • Select any word or phrase in your document for which you want to set bookmark. Copy that word/phrase.

  • Go to “Insert” tab and click “Bookmark” tool button. Paste the selected word/phrase in the Bookmark name text Box. Then click “Add” button. In this way, your bookmark is got added.
  • Now select another word/phrase to which you like to set Hyperlink (i.e. on clicking this word, you directly reach to your desired bookmark)
  • Click the “Hyperlink” tool button under “Insert” tab. Then a window appears.
  • Now select the option “Place in this document” in left pane that will show your all bookmarks and headings. Select the bookmark which you like and click “OK”.
In this way, we saw how we can set Hyperlink which refer to some bookmark in the same document.

Monday, July 30, 2012

Converting an Image into Byte Array and vice versa in C#

      

Putting a jpeg image into an Image Object (System.Drawing)

     Image m_imageIn= Image.FromFile("world.jpeg");

Converting any Image object into Byte Array:

       private byte[] ImageToByteArray(System.Drawing.Image m_imageIn)
        {
            MemoryStream oMemoryStream = new MemoryStream();
            
            // ImageFormat could be other formats like bmp,gif,icon,png etc.
            m_imageIn.Save(oMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
            return oMemoryStream.ToArray();
        }

     
Converting back Byte Array into Image object:       
 
private Image ByteArrayToImage(byte[] m_byteArrayIn)
        {
            MemoryStream oMemoryStream = new MemoryStream(m_byteArrayIn);
            Image oImage = Image.FromStream(oMemoryStream);
            return oImage;
        }


How to pass image from one windows form to another window form

Step-1:  Add a following reference to your project:
Sytem.Drawing


Step-2:  Make a property as following in one of your Form (say Form1) so that this property would be accessible to another form (say Form2)
      
  Image image_Form1;
        public Image Image_Form1
        {
            get { return image_Form1; }
            set { image_Form1 = value; }
        }



Step-3:  Then in your code get the image in a variable (say image_From1)
      
      image_Form1= Image.FromFile("world.bmp");


Step-4:  In your another form “Form2”, suppose you want to show the Form1’s image in a picture Box.
Make an object of Form1 in Form2.
Form1 Form1Object = new Form1();


Step-5:  Then you could assign Form1’s image to a picture Box like:
pictureBox.Image = Form1Object.Image_From1;

Friday, July 27, 2012

Thread Pool Example


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadPoolTest
{
   
    class Program
    {
 
        private const int NumThreads = 3;
        private static int[] inputArray;
        private static double[] resultArray;
        private static ManualResetEvent[] resetEvents;
        private static void Main(string[] args)
        {
            inputArray = new int[NumThreads];
            resultArray = new double[NumThreads];
            // An object that allows one thread to signal another thread
            // when something happens. 
            // In the case of this code, we use these events to signal the main
            // thread that a work item has been completed.
            resetEvents = new ManualResetEvent[NumThreads];
            Random rand = new Random();
            for (int s = 0; s < NumThreads; s++)
            {
                inputArray[s] = rand.Next(1, 5000000);
                //ManualResetEvent with its signaled state initially set to false,
                // and then we queue the work item. 
                resetEvents[s] = new ManualResetEvent(false);
 
                // Well, we are saying that a thread in the thread pool should run
                // the method DoWork, with the argument s. Any method that you want 
                // to queue up for the thread pool to run needs to take one 
                // argument, an object, and return void. The argument will end up 
                // being whatever you passed in as the second argument to the 
                // QueueUserWorkItem call - and in this case is the 'index' of this 
                // work item (the index in the various arrays that it needs to work 
                // with). And it makes sense that the method would have to return 
                // void - because it isn't actually returning 'to' anything, it is 
                // running out there all on its own as a separate thread.
 
                ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), (object)s);
            }
            Console.WriteLine("Waiting...");
            // we hit the very important call WaitHandle.WaitAll(resetEvents). This 
            // causes the main thread to block here until all the ManualResetEvent  
            // objects in the resetEvents array signal. When all of them have 
            // signaled, that means that all the work units have been completed, and 
            // so we continue on and print out all the results. 
            WaitHandle.WaitAll(resetEvents);
            Console.WriteLine("And the answers are: ");
            for (int i = 0; i < NumThreads; i++)
                Console.WriteLine(inputArray[i] + " -> " + resultArray[i]);
        }
        private static void DoWork(object o)
        {
            int index = (int)o;
            for (int i = 1; i < inputArray[index]; i++)
                resultArray[index] = inputArray[index]*100;
            resetEvents[index].Set();
        }
    }
}