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

C # how to upload multiple pictures and generate thumbnails

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

Share

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

This article mainly introduces "how to upload multiple pictures and generate thumbnails". In daily operation, I believe many people have doubts about how to upload multiple pictures and generate thumbnails. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to upload multiple pictures and generate thumbnails". Next, please follow the editor to study!

Foreground code:

The copy code is as follows:

Li

{

List-style: none

Padding-top: 10px

}

Function ValidImage (id, msg) {

$(id) .parent () .append ("" + msg + "")

Return false

}

The foreground is just a few controls and a ValidImage method.

Background code:

The copy code is as follows:

Protected void btnPostFile_ServerClick (object sender, EventArgs e)

{

String filePath = Server.MapPath ("/ uploadImg")

Const int size = 5242880

If (! Directory.Exists (filePath))

{

Directory.CreateDirectory (filePath)

}

If (Request.Files.Count > 0)

{

For (int I = 0; I

< Request.Files.Count; i++) { HttpPostedFile postFile = Request.Files[i]; string uploadFileID = string.Format("#upload{0}", i + 1); //当前的上传控件ID,因为jquery要调用就加了# string msg = null; //提示信息 if (postFile.FileName.Trim().Length size) { msg = "文件太大"; Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端 continue; } string savePath = Path.Combine(filePath, postFile.FileName); //图片的保存地址 if (!File.Exists(savePath)) { postFile.SaveAs(Path.Combine(filePath, postFile.FileName)); //如果文件不存在就保存 } else { msg = "文件" + postFile.FileName + "已经存在"; Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端 continue; } if (IsImg(savePath)) //通过IsImg方法验证文件是否是图片,或者格式是否正确 { SmallImg(postFile.InputStream, postFile.FileName); } else { msg = "只能上传JGP、PNG类型的图片,请检查文件格式是否正确"; Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端 File.Delete(savePath); //如果不是图片就删除 } } } } 复制代码 代码如下: #region 验证上传文件的格式 /// /// 验证上传文件是否是图片 /// /// 文件的保存路径 /// private bool IsImg(string FilePath) { using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read)) { bool result = false; BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8); string strImg = ""; byte buffer; try { buffer = br.ReadByte(); strImg = buffer.ToString(); buffer = br.ReadByte(); strImg += buffer.ToString(); } catch { fs.Close(); br.Close(); } if (strImg == "255216" || strImg == "13780")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar { result = true; } return result; } } #endregion 复制代码 代码如下: #region 将图片生成缩略图 /// /// 生成缩略图 /// private void SmallImg(Stream oStream, string FileName) { using (System.Drawing.Image img = System.Drawing.Image.FromStream(oStream)) { int newWidth = 100; int newHeight = 80; int oldWidth = img.Width; int oldHeight = img.Height; if (oldWidth >

OldHeight)

{

NewHeight = (int) Math.Floor ((double) oldHeight * (double) newWidth / (double) oldWidth)

}

Else

{

NewWidth = (int) Math.Floor ((double) oldWidth * (double) newHeight / (double) oldHeight)

}

Using (Bitmap bmp = new Bitmap (newWidth, newHeight))

{

Using (Graphics g = Graphics.FromImage (bmp))

{

G.Clear (Color.Transparent)

G.InterpolationMode = InterpolationMode.High

G.CompositingQuality = CompositingQuality.HighQuality

G.SmoothingMode = SmoothingMode.HighQuality

G.DrawImage (img, new Rectangle (0,0, newWidth, newHeight), new Rectangle (0,0, oldWidth, oldHeight), GraphicsUnit.Pixel)

String newFileName = Path.GetFileNameWithoutExtension (FileName) + "_ small" + Path.GetExtension (FileName); / / thumbnail name

String filePath = Server.MapPath ("/ uploadImg/") + newFileName

Bmp.Save (filePath)

}

}

}

}

# endregion

At this point, the study on "how to upload multiple pictures and generate thumbnails" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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