Example Method for Scaling and Cropping Pictures in C

  • 2021-12-21 04:42:24
  • OfStack

In this paper, an example is given to describe the method of scaling and clipping pictures in C #. Share it for your reference, as follows:


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Project
{
  class ImageOperation
  {
    /// <summary>
    /// Resize Picture 
    /// </summary>
    /// <param name="bmp"> Primitive Bitmap </param>
    /// <param name="newW"> New width </param>
    /// <param name="newH"> A new height </param>
    /// <param name="Mode"> Retained, temporarily unused </param>
    /// <returns> Work with later pictures </returns>
    public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH, int Mode)
    {
      try
      {
        Bitmap b = new Bitmap(newW, newH);
        Graphics g = Graphics.FromImage(b);
        //  Quality of interpolation algorithm 
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
        g.Dispose();
        return b;
      }
      catch
      {
        return null;
      }
    }
    /// <summary>
    ///  Tailoring  --  Use GDI+
    /// </summary>
    /// <param name="b"> Primitive Bitmap</param>
    /// <param name="StartX"> Start coordinates X</param>
    /// <param name="StartY"> Start coordinates Y</param>
    /// <param name="iWidth"> Width </param>
    /// <param name="iHeight"> Height </param>
    /// <returns> Cut-out Bitmap</returns>
    public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
    {
      if (b == null)
      {
        return null;
      }
      int w = b.Width;
      int h = b.Height;
      if (StartX >= w || StartY >= h)
      {
        return null;
      }
      if (StartX + iWidth > w)
      {
        iWidth = w - StartX;
      }
      if (StartY + iHeight > h)
      {
        iHeight = h - StartY;
      }
      try
      {
        Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmpOut);
        g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
        g.Dispose();
        return bmpOut;
      }
      catch
      {
        return null;
      }
    }
  }
}

The goals are actually new Rectangle(0, 0, iWidth, iHeight) The scaling algorithm plugs the whole original image into the target area new Rectangle(0, 0, bmp.Width, bmp.Height) The clipping only inserts the area new Rectangle (StartX, StartY, iWidth, iHeight) with equal width and height in the original area into the target area 1: 1.

For more readers interested in C # related content, please check the topics on this site: "Summary of C # Picture Operation Skills", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: