Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does asp.net realize the function of compressing a picture in proportion when the picture exceeds the specified size?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report