C # resize storage function of the Image Upload Server

Source: Internet
Author: User

To meet project requirements, you need to upload the profile picture. It was originally implemented using third-party plug-ins. However, many of them are charged.

But do I need to spend the money? Yes? So I collected some information on the Internet ..

Sure enough. There are many great gods in the world.

A free plug-in is used.

 <script src="../Scripts/ajaxFileUpload.js" type="text/javascript"></script>

This is really powerful.

The image is uploaded to the server.

Then the problem arises. The image must be scaled. We wanted to crop the image.

However. Really annoying .. Cropping is also made. But I am not using it. The reason is. The interface is messy.

Finally, I figured it out. Upload an image. Use this plug-in to transfer to the server.

Then, use the code on the server side to crop the photo into an avatar. That is, scaling.

Code:

Public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; string action = context. request ["myaction"]. tostring (); If (Action = "updata") {string MSG = ""; string result = "0"; httppostedfile file = context. request. files [0]; string ext = path. getextension (file. filename ). tolower (); string newname = system. datetime. now. tostring ("yyyymmddhhmmss"); string temppath = "Upload/temp/"; string IMG = temppath + newname + ext; string filepath = context. server. mappath (IMG); temppath = context. server. mappath (temppath); If (! Directory. exists (temppath) {directory. createdirectory (temppath);} using (system. drawing. image originalimage = system. drawing. image. fromstream (file. inputstream) {int W = originalimage. width; int H = originalimage. height; If (W> 1400 | h> 1400) {MSG = "please upload an image smaller than 1400*1400 ";} else if (W <50 | H <50) {MSG = "please upload images larger than 50*50 ";} else {If (W> 300 || h> 300) {float sizem; using (system. drawing. image thumb = getthumbimage (originalimage, 200,200, out sizem) {thumb. save (filepath, system. drawing. imaging. imageformat. JPEG); Result = "1" ;}} else {}// file. saveas (filepath); // save} // result = 0; failed; 1; succeeded; MSG: Message string strwrite = ".. /ashx/"+ IMG; context. response. write (strwrite );}}

Modified according to some code on the Internet. Is this an alternative? The effect is correct.

A new problem is coming... The photo app will be rounded up.

The app side does not process the IMG control into a circle. Therefore, I was asked to directly draw the picture into a circle on the server side.

Baidu spent half a day .. Found a good code.

That is, IMG images are rounded. A class on csdn is used. I forgot the link. But the code can be pasted out.

Using system; using system. data; using system. configuration; using system. web; using system. drawing; using system. drawing. drawing2d; using system. drawing. imaging; /// <summary> /// abstract description of imageoption /// </Summary> public class imageoperation {/// <summary> /// generate a rounded corner (but only one corner) /// </Summary> /// <Param name = "image"> source image </param> /// <Param name = "roundcorner"> corner position </ param> // <returns> processed image </returns> Public static image createroundedcorner (image, roundrectangleposition roundcorner) {graphics G = graphics. fromimage (image); // ensure the image quality g. smoothingmode = smoothingmode. highquality; G. interpolationmode = interpolationmode. highqualitybicubic; G. compositingquality = compositingquality. highquality; rectangle rect = new rectangle (0, 0, image. width, image. height); // construct the external path graphicspath rectpath = createroundrectanglepath (rect, image. width/10, roundcorner); // fill the background of the rounded corner in white. Brush B = new solidbrush (color. white); G. drawpath (new pen (B), rectpath); G. fillpath (B, rectpath); G. dispose (); Return image ;} /// <summary> /// the rounded corner of the Target Image /// </Summary> Public Enum roundrectangleposition {// <summary> /// upper left corner /// </ summary> topleft, /// <summary> /// top right corner /// </Summary> topright, /// <summary> /// bottom left corner /// </Summary> bottomleft, /// <summary> /// bottom right corner /// </Summary> bottomright} /// <summary> /// construct the graphicspath path /// </Summary> // /<Param name = "rect"> </param> // <Param name = "radius"> </param> // <Param name = "rrposition"> Image returns graphicpath </param> Private Static graphicspath createroundrectanglepath (rectangle rect, int radius, roundrectangleposition rrposition) {graphicspath rectpath = new graphicspath (); Switch (rrposition) {Case roundrectangleposition. topleft: {rectpath. addarc (rect. left, rect. top, radius * 2, radius * 2,180, 90); rectpath. addline (rect. left, rect. top, rect. left, rect. top + radius); break;} case roundrectangleposition. topright: {rectpath. addarc (rect. right-radius * 2, rect. top, radius * 2, radius * 2,270, 90); rectpath. addline (rect. right, rect. top, rect. right-radius, rect. top); break;} case roundrectangleposition. bottomleft: {rectpath. addarc (rect. left, rect. bottom-radius * 2, radius * 2, radius * 2, 90, 90); rectpath. addline (rect. left, rect. bottom-radius, rect. left, rect. bottom); break;} case roundrectangleposition. bottomright: {rectpath. addarc (rect. right-radius * 2, rect. bottom-radius * 2, radius * 2, radius * 2, 0, 90); rectpath. addline (rect. right-radius, rect. bottom, rect. right, rect. bottom); break;} return rectpath ;} /// <summary> /// resize the image /// </Summary> /// <Param name = "B"> bitmap of the source image </param> /// <param name = "dstwidth"> Target width </param> /// <Param name = "dstheight"> target height </param> /// <returns> processed image bitmap </returns> Public static bitmap resizebitmap (Bitmap B, int dstwidth, int dstheight) {bitmap dstimage = new Bitmap (dstwidth, dstheight); system. drawing. graphics G = system. drawing. graphics. fromimage (dstimage); // sets the interpolation mode G. interpolationmode = system. drawing. drawing2d. interpolationmode. bilinear; // sets the smoothing mode G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; G. smoothingmode = smoothingmode. highquality; G. interpolationmode = interpolationmode. highqualitybicubic; // use the drawimage method of graphic to draw a new image by setting the size. drawimage (B, new rectangle (0, 0, dstimage. width, dstimage. height), new rectangle (0, 0, B. width, B. height), graphicsunit. pixel); G. save (); G. dispose (); Return dstimage ;}}

This should be done ..... So far. The upload image is displayed as a user's circular avatar. Basic implementation.

However, the efficiency is not very high. In general, modify the IMG control. This way, once and for all...

For me. Function. How to implement it. What are your comments?

C # resize storage function of the Image Upload Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.