In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the classic problems of ASP.NET picture processing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, the verification code.
When we register or log in on some websites, we may encounter places where we have to fill in the CAPTCHA. At that time, we didn't understand what such a picture was all about. In fact, this is a randomly generated picture. We need to create a new aspx page or an ashx general program processing page in the background to be responsible for generating such a picture. The following is an example to show you such a process!
First of all, let's create a new login page to simulate user login. The code is as follows:
Account: password: verification code: please enter: then we do the following in the handler function of the Click event of the btnuplod button: protected void btnupload_Click (object sender, EventArgs e) {if (FileUpload1.FileName.Trim ()! = ") {string extension = Path.GetExtension (FileUpload1.FileName) / / get the suffix string fileName = DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + DateTime.Now.Minute.ToString () of the file first; / / rename the uploaded image string path = Server.MapPath (".") + "\\ images\\" + fileName + extension; FileUpload1.PostedFile.SaveAs (path) in the format of year, month and minute on the current date; / / save the picture in the specified path Bitmap img = new Bitmap (path) / / create a new Bitmap object and use it as the artboard Graphics g = Graphics.FromImage (img); g.DrawString ("Hello Olive!", new Font ("Verdana", 30), Brushes.GreenYellow, new PointF (20Magazine 20); / / write "Hello Olive" in the picture (20L20) at g.Dispose (); newPath = Server.MapPath (".") + "\\ images\" + fileName + "_ New" + extension; img.Save (newPath) / rename and save the picture with text, and delete the original picture img.Dispose (); if (File.Exists (path)) {File.Delete (path);} touxiangpath= "~ / images/" + fileName + "_ New" + extension;} else {Response.Write ("alert ('Please select the file to upload first!)") ;}}
The effect is as shown in the figure:
Third, generate thumbnails
Nowadays, many blogs, personal homepages, spaces and so on have settings for editing personal information, and you may need to upload avatars when editing personal information. let's talk about how to generate slightly shrunk avatars.
In terms of page layout, let's refer to the layout above:
But add another line to show the generated thumbnail.
First of all, we need to create a new CutImage class CutImage.cs, which is specifically used to zoom the image, as follows:
/ / (the code for zooming this image refers to king-, the blogger of Zi blog Park.)
/ / capture the picture
/ / /
/ / the path of the original image
/ / New image path
/ / the width of the thumbnail
/ / the height of the thumbnail
/ / intercept mode
Code:
Public static void CutImg (string oPath, string nPaht, int w, int hjinger string mode) {Image oimg = Image.FromFile (oPath); int nToWidth = w; int nToHeight = h; int x = 0; int y = 0; int oWidth = oimg.Width; int oHeight = oimg.Height; switch (mode) {case "HW": / / zooms to the specified width and height and may deform break Case "W": / / specify width, height scale nToHeight = oWidth*oHeight/ nToWidth; break; case "H": / / specify height, width scale nToWidth=oWidth*oHeight/nToHeight; break; case "CUT": / / scale if ((oimg.Width / oimg.Height) > (nToWidth / nToHeight)) {oHeight = oimg.Height; oWidth= oimg.Height * nToWidth / nToHeight by specified width and height Y = 0; x = (oimg.Width-oWidth) / 2;} else {oWidth = oimg.Width; oHeight = oimg.Width * nToHeight / nToWidth; x = 0; y = (oimg.Height-oHeight) / 2;} break; default: break;} / / create a new BMP picture Image bitmap = new Bitmap (nToWidth, nToHeight) / / create a new artboard Graphics gp = Graphics.FromImage (bitmap); gp.InterpolationMode = InterpolationMode.High; gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; / / empty the canvas and fill gp.Clear (Color.Transparent) with the background color as transparent; gp.DrawImage (oimg, new Rectangle (0,0, nToWidth, nToHeight), new Rectangle (x, y, oWidth, oHeight), GraphicsUnit.Pixel) / / draw a new thumbnail try {bitmap.Save (nPaht, System.Drawing.Imaging.ImageFormat.Jpeg);} catch (Exception e) {throw e;} finally {oimg.Dispose (); bitmap.Dispose ();}}
Then the handler code for the Click event of the btnupload button of our aspx page is as follows:
Protected void btnupload_Click (object sender, EventArgs e) {if (FileUpload1.FileName.Trim ()! = "") {/ /. / /. Omit the same if (File.Exists (path)) {File.Delete (path);} string p = Server.MapPath (".") + "\\ images\"; touxiangpath= "~ / images/" + fileName + "_ New" + extension; TouXiang.ImageUrl = touxiangpath; CutImage.CutImg (newPath, p + "olive.jpg", 100,200, "CUT") / / call the CutImg function of the CutImage class of zooming the image. Save it as "olive.jpg" here to facilitate the display of the following references, or save it to other names and formats. Luesuotu.ImageUrl = "~ / images/olive.jpg";} else {Response.Write ("alert ('Please select the file to upload first!')") ;}}
The effect is as shown in the figure:
This is the end of the content of "what are the classic problems of dealing with ASP.NET pictures"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.