How does asp.net realize the function of compressing a picture in proportion when the picture exceeds the specified size?
This article will explain in detail how asp.net achieves the function of compressing pictures in proportion to the specified size. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
The code is as follows:
/ / /
/ / compress the picture
/ / /
/ / /
Public string ResizePic ()
{
# start compressing pictures with region
Bool IsImgFile = true; / / determine whether it is a picture file or not
String filePathName = "123"; / / the path to the file storage (folder name)
String fileName = "a.jpg"; / / the original name of the uploaded file
String fileSysName = DateTime.Now.ToString ("yyyyMMddHHmmssfff") + "_" + fileName; / / modified file name
String filePath = ""; / / File path
String strImgPath = "/ fileupload/"; / / upload path
If (IsImgFile)
{
Int maxWidth = 600; / / maximum image width limit
Int maxHeight = 400; / / maximum height limit of image
System.Drawing.Image imgPhoto =
System.Drawing.Image.FromFile (Server.MapPath (strImgPath) + filePathName + "/" + fileSysName)
Int imgWidth = imgPhoto.Width
Int imgHeight = imgPhoto.Height
If (imgWidth > imgHeight) / / if the width exceeds the height, the width shall prevail
{
If (imgWidth > maxWidth) / / if the width of the picture exceeds the limit
{
Float toImgWidth = maxWidth; / / width of the picture after compression
Float toImgHeight = imgHeight / (float) (imgWidth / toImgWidth); / / height of the compressed image
System.Drawing.Bitmap img = new System.Drawing.Bitmap (imgPhoto
Int.Parse (toImgWidth.ToString ())
Int.Parse (toImgHeight.ToString ())
String strResizePicName = Server.MapPath (strImgPath) + filePathName + "/ _ small_" + fileSysName
Img.Save (strResizePicName); / / Save the compressed picture
FilePath = strImgPath + filePathName + "/ _ small_" + fileSysName; / / returns the compressed image path
}
}
Else
{
If (imgHeight > maxHeight)
{
Float toImgHeight1 = maxHeight
Float toImgWidth2 = imgWidth / (float) (imgHeight / toImgHeight1)
System.Drawing.Bitmap img = new System.Drawing.Bitmap (imgPhoto
Int.Parse (toImgWidth2.ToString ())
Int.Parse (toImgHeight1.ToString ())
String strResizePicName = Server.MapPath (strImgPath) + filePathName + "/ _ small_" + fileSysName
Img.Save (strResizePicName)
FilePath = strImgPath + filePathName + "/ _ small_" + fileSysName
}
}
}
Return filePath
# endregion
}
This is the end of this article on "asp.net how to achieve the function of compressing pictures in proportion to the specified size". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.