May 25, 2005

Useful Drawstring function

This version of Drawstring is useful it will fit the string to the given rectangle, automatically wrapping it as appropriate.
 Rectangle rect = this.ClientRectangle;
 graphics.Clear(Color.Black);
 rect.Inflate(-1,-1);
 graphics.DrawString(string.Concat("Onpaint error: \'", exception.Message, "\'")
  , theFont, Brushes.Red, rect);

Free Online HTML editor

Uses apsx HTML editor specified below. Free apsx HTML editor

Code to write to the App.Config file.

.Net provides something to read configuration file settings but nothing to write them back.

Draw Image To Fit A Rectangular Area

Following code draws an image scaled to a given rectangle, keeping the aspect ratio and centering the scaled image inside the rectangle using GDI+

 
  Graphics.DrawImage(image, FitToRect(this.ClientRectangle, image.Size));
 
 /// <summary>
 /// Fit the given src Size to the target Rectangle keeping the aspect
 /// ratio and centering the src inside the rectangle
 /// </summary>
 /// <param name="target"></param>
 /// <param name="src"></param>
 /// <returns></returns>
 static public Rectangle FitToRect(Rectangle target, Size src)
 {
    // Find the scale to fit the Src to the Target height
    float   sfh = (float)target.Height/(float)src.Height;
    // Find the scale to fit the Src to the Target width
    float   sfw = (float)target.Width/(float)src.Width;
    // Smaller of the 2 is the fit to view scale factor
    float   sf = Math.Min(sfh, sfw);
    // Find the size of the Src scaled to fit the target rectangle
    Size sizeToFit = new Size((int)Math.Round((double)src.Width*sf),
     (int)Math.Round((double)src.Height*sf) );
    Poiny loc = new Point((target.Width-sizeToFit.Width)/2,
                                    (target.Height-sizeToFit.Height)/2);
    // Now centre the scaled src size in the rectangle
    Rectangle rect = new Rectangle(loc, sizeToFit);
    return rect;
  }

May 20, 2005

ASP.NET and HTTP Protocol

Explains how HTTP protocol works. Very thorough run down of the workings of ASP.NET.

A Bunch of On line books. - These books are very basic but very detailed. They are from the same guys as above.

Difference between Invalidate, Update and Refresh

Whats the difference between Control.Invalidate, Control.Update and Control.Refresh? See here for a detailed explanantion

Summary:
Control.Invalidate() => Mark region/rectangle for painting in when next WM_PAINT is received. Asynchonouse
Control.Update() => Immediately send WM_PAINT to the WNDPROC() if update region is not empty. Synchonous
Control.Refresh() => Control.Invalidate() followed by Control.Update(). Synchonous

System.Buffer and System.BitConverter Classes

Discovered System.Buffer class that can directly manipulate the bytes of an array class objects. All its members are static.

 
 int[] myarr1 = newint[5] {1,2,3,4,5};
 int[] myarr2=newint[10] {0,0,0,0,0,6,7,8,9,10};
               
 //here we are copying offset to offet as bytes
 Buffer.BlockCopy(myarr1,0,myarr2,0,20);
It has 4 members:
  • BlockCopy - Copies a specified number of bytes from a source array starting at a particular offset to a destination array starting at a particular offset.
  • ByteLength - Returns the number of bytes in the specified array.
  • GetByte - Retrieves the byte at a specified location in a specified array.
  • SetByte - Assigns a specified value to a byte at a particular location in a specified

System.BitConverter class - Converts base data types to an array of bytes, and an array of bytes to base data types. See 'SelfMarshalledStruct' in OpenNETCF.Net for a sample usage. Works with the System.Buffer class for serialising/deserialising data types to byte arrays.