Image zoom (with source)

Source: Internet
Author: User

A small tool that scales pictures by a given size, supporting the scaling of animated GIF graphs.

Main code :

 Public classFormatimage { Public Static stringformatallimage (imageinfo info) {System.Drawing.Image Image=System.Drawing.Image.FromFile (info.            Frompath); Try            {                intImageWidth =image.                Width; intImageHeight =image.                Height; //The picture itself is wider and more relaxed, judging whether there is a need for scaling                if(ImageWidth > Info.) Sizewidth && imageheight >info. Sizeheigth) {//determine if a picture is a dynamic graph (animated pictures have multiple frames)                    if(Image. Getframecount (NewFramedimension (image. framedimensionslist[0])) >1)                    {                        //decompose and scale each frame image firstilist<string> imageurls =Decompose (info, image); //Zoom Out of merge                        returnMerger (info. Outpath, Imageurls, info.                    Time); }                    Else                    {                        //scale a static diagram                        returnFormatstaticimage (info, image); }                }                Else{image.                    Dispose (); returnSavenoformatimage (info); }            }            Catch(Exception) {Throw; }            finally{image.            Dispose (); }        }        /// <summary>        ///GIF dynamic Image decomposition/// </summary>        /// <param name= "info" ></param>        /// <param name= "image" ></param>        /// <returns></returns>        Private Staticilist<string>Decompose (imageinfo info, System.Drawing.Image Image) {//decompositionilist<string> imageurls =Newlist<string>();  for(inti =0; I < image. Getframecount (NewFramedimension (image. framedimensionslist[0])); i++) {image. Selectactiveframe (NewFramedimension (image. framedimensionslist[0]), i); System.Drawing.Image Formatimage=NewBitmap (info. Sizewidth, Info.                Sizeheigth); Graphics g=graphics.fromimage (formatimage);                G.clear (Color.Blue); G.interpolationmode=System.Drawing.Drawing2D.InterpolationMode.Low; G.smoothingmode=System.Drawing.Drawing2D.SmoothingMode.Default; G.drawimage (Image,NewSystem.Drawing.Rectangle (0,0, Formatimage.width, formatimage.height),NewSystem.Drawing.Rectangle (0,0, image. Width, image.                Height), System.Drawing.GraphicsUnit.Pixel); stringSavepath = Path.Combine (Path.GetFullPath (info). Outpath), DateTime.Now.ToString ("yyyymmddhhmmssffff") +". PNG");                Formatimage.save (Savepath, System.Drawing.Imaging.ImageFormat.Png); Imageurls.            ADD (Savepath); }            returnImageurls; }        /// <summary>        ///gif dynamic picture merge/// </summary>        /// <param name= "Outpath" ></param>        /// <param name= "Imageurls" ></param>        /// <returns></returns>        Private Static stringMerger (stringOutpath, ilist<string> Imageurls,intTime ) {            //Merging            stringImageName = DateTime.Now.ToString ("yyyymmddhhmmssffff") +". gif"; stringOutputfilepath =Path.Combine (Path.GetFullPath (Outpath), ImageName); Animatedgifencoder e=NewAnimatedgifencoder ();            E.start (Outputfilepath); //picture interval per frameE.setdelay (time); E.setrepeat (0);  for(inti =0; I < Imageurls. Count; i++) {e.addframe (System.Drawing.Image.FromFile (imageurls[i));            } e.finish (); returnImageName; }        /// <summary>        ///Static picture Scaling/// </summary>        /// <param name= "info" ></param>        /// <param name= "image" ></param>        /// <returns></returns>        Private Static stringformatstaticimage (imageinfo info, System.Drawing.Image image) {image=System.Drawing.Image.FromFile (info.            Frompath); System.Drawing.Image Formatimage=NewBitmap (info. Sizewidth, Info.            Sizeheigth); Graphics g=graphics.fromimage (formatimage); G.interpolationmode=System.Drawing.Drawing2D.InterpolationMode.High; G.smoothingmode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;            G.clear (Color.Blue); G.drawimage (Image,NewSystem.Drawing.Rectangle (0,0, Formatimage.width, formatimage.height),NewSystem.Drawing.Rectangle (0,0, image. Width, image.            Height), System.Drawing.GraphicsUnit.Pixel); stringImageName = DateTime.Now.ToString ("yyyymmddhhmmssffff") +". jpg"; stringSavepath =Path.Combine (Path.GetFullPath (info.            Outpath), ImageName); Formatimage.save (info. Outpath+"\\"+ImageName, Info.            Outimageformat);            G.dispose (); returnImageName; }        /// <summary>        ///Save Picture/// </summary>        /// <param name= "info" ></param>        /// <returns></returns>        Private Static stringsavenoformatimage (imageinfo info) {stringImageName =Path.getfilename (info.            Frompath); stringSavepath =Path.Combine (Path.GetFullPath (info.            Outpath), ImageName); FileStream FS=NULL; Try{FS=File.create (info.                Frompath); File.Copy (info. Frompath, Info.                Outpath); returnImageName; }            Catch(Exception) {Throw; }            finally{fs.            Close (); }        }    }

Link: Http://pan.baidu.com/s/1mgBxB7M Password: W1GD

Image zoom (with source)

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.