.NET/Answer Detail

Use of the Global Namespace Aliases

Ankit tewari
32 Points
16 Posts

Hi,

  Here is code for cropping image using Js. I want to know that why we are using such type of name space 

using SD = System.Drawing;

SD is worked here like object but we can  access directly properties if we added namespace. But in this code I don't understand how SD worked. 

using (SD.Image OriginalImage = SD.Image.FromFile(Img))

Whole code is as following.... 

using System.IO;
using SD = System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
  try
  {
    using (SD.Image OriginalImage = SD.Image.FromFile(Img))
    {
      using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
      {
        bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);

        using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
        {
          Graphic.SmoothingMode = SmoothingMode.AntiAlias;
          Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
          Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
          Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
          MemoryStream ms = new MemoryStream();
          bmp.Save(ms, OriginalImage.RawFormat);
          return ms.GetBuffer();
        }
      }
    }
  }
  catch (Exception Ex)
  {
    throw (Ex);
  }
}
Total Answered: 3
Total Marked As Answer: 0
Posted On: 26-Oct-2017 21:35
Answers
Rahul Maurya
Moderator
1054 Points
202 Posts
 
 
Posted On: 26-Oct-2017 23:52

It is Global Namespace Alias. The global namespace alias is useful when the member might be hidden by another entity of the same name. 

In your case, I think, you can remove it if there is no entity with same name in imported namespaces. For more see https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/how-to-use-the-global-namespace-alias

Nice... - hambi  13-Nov-2017 06:38
Smith
202 Points
86 Posts
 
 
Posted On: 27-Oct-2017 00:09

It is useful to disambiguate - for example:

using WinformTimer = System.Windows.Forms.Timer;
using ThreadingTimer = System.Threading.Timer;

Otherwise, if you use both System.Windows.Forms.Timer and System.Timers.Timer in the same file you'd have to keep giving the full names (since Timer could be confusing).

Ankit tewari
32 Points
16 Posts
 
 
Posted On: 27-Oct-2017 03:47

Thanks..

When replying / answering a question please:

  1. Read the question and other answers carefully.
  2. Understand that English isn't everyone's first language so be sympathetic of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for explanation, ignore it. rudeness is not allowed.
Let's work to help programmers, not make them feel stupid.

Blog

Active User (2)

 Log In to Chat