Compress the remote picture and return

Source: Internet
Author: User
Tags bmp image

    PublicActionResult Pic () {varStram =NewSystem.Net.WebClient (). OpenRead ("http://www.mj100.com/img/index/z_ban02.jpg"); byte[] bytes= makesmallimg (Stram, -, -); returnFile (Bytes,"Image/jpeg","1.jpg"); }        /// <summary>        ///compress picture 15-5-4 by Qian/// </summary>        /// <param name= "Fromfilestream" ></param>        /// <param name= "Templatewidth" ></param>        /// <param name= "Templateheight" ></param>        /// <returns></returns>         Public byte[] makesmallimg (System.IO.Stream fromfilestream, System.Double templatewidth, System.Double templateheight) { //get a Picture object from a file and use the embedded color management information in the streamSystem.Drawing.Image myimage = System.Drawing.Image.FromStream (Fromfilestream,true); //thumbnail width, heightSystem.Double newwidth = myimage.width, Newheight =Myimage.height; //wide cross-sectional view of the stencil            if(Myimage.width > Myimage.height | | myimage.width = =myimage.height) {if(Myimage.width >templatewidth) {                    //wide-Press-fit, high-scaleNewwidth =Templatewidth; Newheight= Myimage.height * (Newwidth/myimage.width); }            }            //tall in the template of the vertical diagram            Else            {                if(Myimage.height >templateheight) {                    //high-Press-fit, wide-scale scaleNewheight =Templateheight; Newwidth= Myimage.width * (Newheight/myimage.height); }            }            //Get picture sizeSystem.Drawing.Size mysize =NewSize (int) Newwidth, (int) newheight); //Create a new BMP imageSystem.Drawing.Image bitmap =NewSystem.Drawing.Bitmap (Mysize.width, mysize.height); //Create a new artboardSystem.Drawing.Graphics g =System.Drawing.Graphics.FromImage (bitmap); //setting high-quality interpolation methodsG.interpolationmode =System.Drawing.Drawing2D.InterpolationMode.High; //set high quality, low speed rendering smoothnessG.smoothingmode =System.Drawing.Drawing2D.SmoothingMode.HighQuality; //clear the canvas .g.clear (Color.White); //draw at the specified locationG.drawimage (MyImage,NewSystem.Drawing.Rectangle (0,0, Bitmap. Width, Bitmap. Height),NewSystem.Drawing.Rectangle (0,0, Myimage.width, myimage.height), System.Drawing.GraphicsUnit.Pixel); using(MemoryStream stream =NewMemoryStream ()) {Bitmap.                Save (stream, imageformat.jpeg);                G.dispose ();                Myimage.dispose (); Bitmap.                Dispose (); //Output byte stream                returnStream.            ToArray (); }                    }

Compress the remote picture and return

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.