Application scenarios
We assume that there will be the following scenario:
Scenario 1: In the training system, in the function of uploading courseware training video materials, we will upload the course cover picture and output it in the course details in the future at the specified location.
Scenario 2: In the talent website, in the enterprise management backend, the company's LOGO content pictures will be uploaded, used for the company introduction page or job recruitment details page, etc.
Scene 3: In the mall system, in the backend of the product release, the main image promotional pictures and other key introductory pictures of the product will be uploaded, and used to display and promote them in the product details page.
The above scenarios will use a common function to query. A characteristic point of query is to display the course cover picture, the company logo picture and the product main promotional picture involved in the above scene. Usually, in order to improve the display efficiency of query performance, thumbnails of the original image will be displayed in the query list, because in order to achieve the display effect, the pictures in the details are of higher quality and larger in size.
Therefore, generating thumbnails mainly achieve the following goals:
1. Thumbnails can load data more quickly in a web browser while ensuring the display quality as much as possible through compression technology.
2. Smaller data volume can save traffic costs.
3. Making and storing new thumbnails (only for display during query) can attract users more intuitively and improve the system experience.
Development and operation environment
Operating system: Windows Server 2019 DataCenter
.net version: .netFramework 4.0 or above
Development Tools: VS2019 C#
Method design
public Byte[] MakeThumbnail method (making thumbnail) call parameters are shown in the following table:
Serial number | parameter | type | illustrate |
---|---|---|---|
1 | originalImagePath | string | Physical path image file address, non-unique option |
2 | bvalue | Byte[] | Byte[] type data, non-unique option |
3 | thumbnailPath | string | Non-essential option, the method returns the compressed Byte[] array data. If the output file path thumbnailPath is specified at the same time, this text will be generated at the same time. |
4 | width=0 | int | Specifies the width of the output thumbnail, which is 0 by default, which is expressed as the width of the original image. |
5 | height=0 | int | Specifies the height of the output thumbnail, which is 0 by default, which is expressed as the height of the original image. |
6 | mode | string | mode is the compression method: "HW": Specify the height and width scaling (possible deformation), "W": Specify the width, height to scale, "H": Specify the height and width to scale, "Cut": Specify the height and width to scale (not deformed), the default parameter="Cut" |
7 | interpolationMode | . Drawing2D. InterpolationMode |
Specifies the algorithm used when scaling or rotating an image, default value =. |
Physical path file originalImagePath or Byte[] type data bvalue, both are passed at the same time with the physical path file preferred.
Implement code
Method Code
//Create a thumbnail (compressed image), which can receive two parameters, the physical path file originalImagePath or Byte[] type data bvalue, and both are passed at the same time, with the physical path file being preferred.//The method returns the compressed Byte[] array data. If the output file path thumbnailPath is specified at the same time, this file will be generated at the same time.//Specify the width and height of the output thumbnail. If it is 0, the default is the width or height of the original image.//mode is the compression method: "HW": Specify the height and width scaling (possible deformation), "W": Specify the width, height in proportion, "H": Specify the height and width in proportion, "Cut": Specify the height and width in proportion (not deformed) public Byte[] MakeThumbnail(string originalImagePath, Byte[] bvalue, string thumbnailPath, int width=0, int height=0, string mode="Cut", . interpolationMode= .) { originalImage; if (originalImagePath != "") { originalImage = (originalImagePath); } else { originalImage = (new (bvalue)); } int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = ; int oh = ; if (towidth == 0) { towidth = ow; } if (toheight == 0) { toheight = oh; } switch (mode) { case "HW"://Specify the height and width scaling (may be deformed) break; case "W"://Specify width, height according to proportion toheight = * towidth / ; break; case "H"://Specify height, width according to proportion towidth = * toheight / ; break; case "Cut"://Specify the height and width cut (no deformation) if ((double) / (double) > (double)towidth / (double)toheight) { oh = ; ow = * towidth / toheight; y = 0; x = ( - ow) / 2; } else { ow = ; oh = * toheight / towidth; x = 0; y = ( - oh) / 2; } break; default: break; } // Create a new bmp image bitmap = new (towidth, toheight); // Create a new drawing board g = (bitmap); //Set high-quality interpolation method = interpolationMode ; //Set high quality and low speed to show smoothness = .; //Clear the canvas and fill it with transparent background color (); //Draw the specified part of the original picture at the specified position and according to the specified size (originalImage, new (0, 0, towidth, toheight), new (x, y, ow, oh), ); try { //Save thumbnails in jpg format mstream = new (); format = ; toFormat = ; if (()) { toFormat = ; } else if (()) { toFormat = ; } else if (()) { toFormat = ; } else if (()) { toFormat = ; } else if (()) { toFormat = ; } else if (()) { toFormat = ; } else if (()) { toFormat = ; } else if (()) { toFormat = ; } else if (()) { toFormat = ; } (mstream, toFormat); byte[] byData = new Byte[]; = 0; (byData, 0, ); (); if (thumbnailPath != "") { (thumbnailPath, toFormat); } return byData; } catch ( e) { throw e; } finally { (); (); (); } }
Call Example
This call example implements the determination of the uploaded image size, and automatically performs compression processing if the image is greater than 2Mb.
string upfilename = + "\\"; //Uploaded image pathstring mtfilename = + "\\"; //Picture path of thumbnailif ((upfilename)) { FileInfo fileInfo = new FileInfo(upfilename); float _fsize = / (1024*1024); if (_fsize >= 2) { MakeThumbnail(upfilename, null, mtfilename); } else { mtfilename = upfilename; } ("Result Filename is :"+mtfilename); }
summary
The output thumbnail can adopt dynamic output and static storage methods. Dynamic output consumes performance, and static storage consumes space. We can exchange space for time to obtain higher performance. We need to decide which method to adopt is more balanced based on the actual situation of the project.
This is the article about C#’s implementation of thumbnails for generating specified images. For more related content on C#’s image generation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!