Getting and setting window center and width Example for C#

//LEADDICOM1 is a DICOM Dataset defined outside this method
private void GettingAndSettingWindowCenterAndWidth()
{
   // This example will add a new "Window Width" and
   // "Window Center" to the dataset , or replace
   // the existing one(s)
   bool bAddWindow = false;
   LTDICLib.LWindowAttributes WindowAttributes = null;
   short iRet = 0;
   int lWindowCount = 0;
   int lWindowIndex = 0;
   short iBitsStored = 0;
   lWindowCount = 0;
   lWindowIndex = 0;
   bAddWindow = true;
   iBitsStored = 12;
   lWindowCount = LEADDICOM1.WindowCount;
   if (lWindowCount > 0)
   {
      iRet = LEADDICOM1.GetWindowAttributes(0, 0);
      if (iRet != 0)
      {
         MessageBox.Show("error");
         return;
      }
      WindowAttributes = LEADDICOM1.WindowAttributes;
      // Half the width for the first window
      WindowAttributes.WindowWidth = WindowAttributes.WindowWidth / 2;
      WindowAttributes.WindowCenter = WindowAttributes.WindowCenter / 2;
   }
   else
   {
      // This represents an identity VOI LUT transformation in the case
      // where no Modality LUT is specified and the stored pixel data are
      // iBitsStored bit unsigned integers.
      WindowAttributes = LEADDICOM1.WindowAttributes;
      WindowAttributes.WindowWidth = (System.Math.Pow(2, iBitsStored));
      WindowAttributes.WindowCenter = (System.Math.Pow(2, (iBitsStored - 1)));
      WindowAttributes.WindowCWExplaination = "";
   }
   lWindowIndex = lWindowCount;
   if (bAddWindow == false)
   {
      // Delete the existing window(s)
      iRet = LEADDICOM1.DeleteWindow(0);
      if (iRet != 0)
      {
         MessageBox.Show("error");
         return;
      }
      lWindowIndex = 0;
   }
   // Add the new window
   iRet = LEADDICOM1.SetWindowAttributes(lWindowIndex, 0);
   if (iRet != 0)
   {
      MessageBox.Show("error");
      return;
   }
}