Document Solutions for Imaging
Features / Work with Graphics / Align Image
In This Topic
    Align Image
    In This Topic

    DsImaging provides you an option to set the alignment of an image within its container using the ImageAlign class. This class provides you an option to center, scale, or stretch an image with respect to the bitmap.

    Image of a beautiful home aligned to the center

    To align an image at the center of its container:

    1. Initialize the GcBitmap class.
    2. Create a drawing surface to draw shapes using CreateGraphics method of the GcBitmap class which returns an instance of the GcBitmapGraphics class.
    3. Invoke the DrawImage method of GcGraphics class to draw an image and set the image alignment to center using the ImageAlign class to pass it as a parameter to the method.
      C#
      Copy Code
      //Initialize GcBitmap
      GcBitmap origBmp = new GcBitmap(1000, 1000, true);
      
      //Create the graphics for the Bitmap
      GcBitmapGraphics g = origBmp.CreateGraphics(Color.AliceBlue);
      
      //Get the image and define the image rectangle
      var image = Image.FromFile(Path.Combine("Resources", "Images", "tudor.jpg"));
      var imgRec = new Rectangle(50, 50, image.Width + 100, image.Height + 100);
      g.FillRectangle(imgRec, Color.Gray);
      
      //Draw the image with "CenterImage" alignment mode
      g.DrawImage(image, imgRec, null, ImageAlign.CenterImage);
      
      //Save the image
      origBmp.SaveAsJpeg("AlignImageCenter.jpeg");
      
      Note: For rendering large or complex text and graphics, you can use Skia library. For more information about the library and its usage, see Render using Skia Library