C # Public helper Class Image helper class

Source: Internet
Author: User

You know, development projects in addition to the data access layer is very important, is common, here provides a powerful and practical tools.

Image class:

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.IO;usingSystem.Drawing.Imaging;usingSystem.Drawing;usingsystem.web;namespacecommon{ Public  Static classImage {#regionImage format Conversion/// <summary>      ///Image Format Conversion/// </summary>      /// <param name= "Orifilename" >Original file relative path</param>      /// <param name= "Desiredfilename" >generate target file relative path</param>      /// <returns></returns>      ///jpg uses lossy compression, so JPG images may degrade image sharpness, and pixels will not degrade.///GIF uses lossless compression so GIF images do not degrade image sharpness and pixels, but GIF formats support only 256-color images.        Public Static BOOLConvertimage (stringOrifilename,stringdesiredfilename) {          stringExtname = desiredfilename.substring (Desiredfilename.lastindexof ('.')+1).          ToLower ();          ImageFormat Desiredformat; //according to the extension name, specify ImageFormat          Switch(extname) { Case "BMP": Desiredformat=imageformat.bmp;  Break;  Case "gif": Desiredformat=imageformat.gif;  Break;  Case "JPEG": Desiredformat=Imageformat.jpeg;  Break;  Case "ico": Desiredformat=Imageformat.icon;  Break;  Case "PNG": Desiredformat=imageformat.png;  Break; default: Desiredformat=Imageformat.jpeg;  Break; }          Try{System.Drawing.Image Imgfile=System.Drawing.Image.FromFile (Webpathtran (orifilename));              Imgfile.save (Webpathtran (desiredfilename), Desiredformat); return true; }          Catch           {              return false; }      }        #endregion      #regionPicture Zoom/// <summary>      ///Picture fixed size zoom/// </summary>      /// <param name= "Orifilename" >source file relative address</param>      /// <param name= "Desiredfilename" >destination file relative address</param>      /// <param name= "Intwidth" >Target file width</param>      /// <param name= "Intheight" >High target file</param>      /// <param name= "ImageFormat" >picture file Format</param>       Public Static BOOLChangeimagesize (stringOrifilename,stringDesiredfilename,intIntwidth,intintheight, ImageFormat imageformat) {            stringSourcefilenamestr =webpathtran (orifilename);//source picture name Path            stringTransferfilenamestr = Webpathtran (desiredfilename);//Destination picture name pathFileStream Myoutput =NULL; Try{System.Drawing.Image.GetThumbnailImageAbort Myabort=NewSystem.Drawing.Image.GetThumbnailImageAbort (Imageabort); Image Sourceimage= System.Drawing.Image.FromFile (Orifilename);//source Picture DefinitionImage targetimage = Sourceimage.getthumbnailimage (intwidth, Intheight, Myabort, IntPtr.Zero);//purpose Picture Definition//The targetfilenamestr picture is relaxed to intwidth, and the height is intheightMyoutput =NewFileStream (Transferfilenamestr, FileMode.Create, FileAccess.Write, fileshare.write);                Targetimage.save (Myoutput, ImageFormat);                Myoutput.close (); return true; }            Catch{myoutput.close (); return false; }                         }      Private Static  BOOLImageabort () {return false; }      #endregion      #regionText watermark/// <summary>        ///Text Watermark/// </summary>        /// <param name= "Wtext" >Watermark Text</param>        /// <param name= "source" >Original picture physical file name</param>        /// <param name= "target" >Generate picture Physical file name</param>         Public Static BOOLImagewatertext (stringWtext,stringSourcestringtarget) {            BOOLResflag =false; Image Image=image.fromfile (source); Graphics Graphics=graphics.fromimage (image); Try{graphics. DrawImage (Image,0,0, image. Width, image.              Height); Font Font=NewSystem.Drawing.Font ("Verdana", -); Brush Brush=NewSystem.Drawing.SolidBrush (System.Drawing.Color.Green); Graphics. DrawString (wtext, font, brush, *, *); Image.              Save (target); Resflag=true; }            Catch(Exception) {Throw; }            finally{graphics.                Dispose (); Image.                          Dispose (); }              returnResflag; }        #endregion      #regionImage watermark/// <summary>        ///Create a picture watermark on a picture/// </summary>        /// <param name= "Path" >Original server picture path</param>        /// <param name= "PATH_SYP" >Image path with picture watermark generated</param>        /// <param name= "PATH_SYPF" >Watermark Picture Path</param>       Public Static BOOLImagewaterpic (stringSourcestringTargetstringWaterpicsource) {            BOOLResflag =false; Image Sourceimage=image.fromfile (source); Graphics Sourcegraphics=graphics.fromimage (sourceimage); Image Waterpicsourceimage=Image.FromFile (Waterpicsource); Try{sourcegraphics. DrawImage (Waterpicsourceimage,NewSystem.Drawing.Rectangle (sourceimage. Width-waterpicsourceimage.width, Sourceimage. Height-waterpicsourceimage.height, Waterpicsourceimage.width, Waterpicsourceimage.height),0,0, Waterpicsourceimage.width, Waterpicsourceimage.height, GraphicsUnit.Pixel); Sourceimage.            Save (target); }            Catch(Exception) {Throw; }            finally{sourcegraphics.                Dispose (); Sourceimage.                Dispose ();            Waterpicsourceimage.dispose (); }            returnResflag; }        #endregion      /// <summary>      ///path conversion (converted to absolute path)/// </summary>      /// <param name= "path" ></param>      /// <returns></returns>      Private Static stringWebpathtran (stringpath) {          Try          {              returnHttpContext.Current.Server.MapPath (path); }          Catch          {              returnpath; }      }    }}

C # Public helper Class Image helper class

Related Article

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.