In GDI +, scaling and cropping can be seen as the same operation, but the selection of the original region is different. You have no choice but to look at specific algorithms for better understanding.
C # code
- 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 the image
- /// </Summary>
- /// <Param name = "bmp"> original Bitmap </param>
- /// <Param name = "newW"> New width </param>
- /// <Param name = "newH"> new height </param>
- /// <Param name = "Mode"> reserved, unavailable for the moment </param>
- /// <Returns> processed images </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 algorithms
- 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>
- /// Crop -- use GDI +
- /// </Summary>
- /// <Param name = "B"> original Bitmap </param>
- /// <Param name = "StartX"> Start coordinate X </param>
- /// <Param name = "StartY"> Start coordinate Y </param>
- /// <Param name = "iWidth"> width </param>
- /// <Param name = "iHeight"> height </param>
- /// <Returns> the cropped 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 targets are all new Rectangle (0, 0, iWidth, iHeight). The Scaling Algorithm inserts new Rectangle (0, 0, bmp) into the target image. width, bmp. height), while cropping only inserts the new Rectangle (StartX, StartY, iWidth, iHeight) in the area with the top width and Height of the original area into the target area.