C # picture compression, generate thumbnails, add Watermark module

Source: Internet
Author: User
Tags static class

C # Image Processing Tool class, you can compress JPG images, images automatically generated thumbnails, add watermark to the image, and so on, return to the HD thumbnail, get the best picture scaling size, and get the type of picture, such as the class code as follows:

View Sourceprint?001using System;

002using System.Collections.Generic;

003using System.Linq;

004using System.Text;

005using System.IO;

006using System.Drawing;

007using System.Drawing.Drawing2D;

008using System.Drawing.Imaging;

009namespace CLB. Utility.tools

010{

011///

012///Picture Tool Class

013///

014 public static Class Imagehelper

015 {

016///

017///compression jpg picture

018///

019///The location of the image after compression

020///need to compress the picture address

021///Compression Quality: If 0, the default adjustment is 80

022 public static void Setcompressimage (String newfilename, String oldfilename, long quality)

023 {

024 if (quality = 0)

025 {

026 quality = 80;

027}

028 using (Bitmap bitmp = new Bitmap (oldfilename))

029 {

030 EncoderParameters EP = new EncoderParameters (1);

031 EP. Param[0] = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, Quality);

032 ImageCodecInfo myimagecodecinfo = Getencoderinfo ("Image/jpeg");

033 Bitmp. Save (NewFileName, Myimagecodecinfo, EP);

034 bitmp. Dispose ();

035}

036}

037///

038///return HD thumbnail

039///

040///Original Document

041///New File

042///Max Height

043///Max width

044///Quality, if 0, set to 80

045 public static void Setgoodimage (String fileName, string newFile, int maxheight, int maxwidth,longqualitys)

046 {

047 if (qualitys = 0)

048 {

049 Qualitys = 80;

050}

051 using (System.Drawing.Image img = System.Drawing.Image.FromFile (fileName))

052 {

053 System.Drawing.Imaging.ImageFormat

054 Thisformat = img. Rawformat;

055 Size newsize = newsize (maxwidth, MaxHeight, IMG. Width, IMG. Height);

056 Bitmap outbmp = new Bitmap (newsize.width, newsize.height);

057 Graphics g = graphics.fromimage (outbmp);

058//Set the painting quality of the canvas

059 g.compositingquality = compositingquality.highquality;

060 G.smoothingmode = smoothingmode.highquality;

061 G.interpolationmode = interpolationmode.highqualitybicubic;

062 G.drawimage (IMG, new Rectangle (0, 0, Newsize.width, newsize.height),

063 0, 0, IMG. Width, IMG. Height, GraphicsUnit.Pixel);

064 G.dispose ();

065//The following code to save the picture, set the compression quality

066 encoderparameters encoderparams = new EncoderParameters ();

067 long[] quality = new LONG[1];

068 quality[0] = Qualitys;

069 encoderparameter Encoderparam = new Encoderparameter (System.Drawing.Imaging.Encoder.Quality, Quality);

070 Encoderparams.param[0] = Encoderparam;

071//Get the ImageCodecInfo object containing information about the built-in image codec.

072 imagecodecinfo[] Arrayici = Imagecodecinfo.getimageencoders ();

073 ImageCodecInfo jpegici = null;

074 for (int x = 0;

075 x < arrayici.length;

076 x + +)

077 {

078 if (arrayici[x]. Formatdescription.equals ("JPEG"))

079 {

080 Jpegici = arrayici[x];

081//Set JPEG encoding

082 break;

083}

084}

085 if (Jpegici!= null)

086 {

087 Outbmp.save (NewFile, Jpegici, encoderparams);

088}

089 Else

090 {

091 Outbmp.save (NewFile, Thisformat);

092}

093 img. Dispose ();

094 Outbmp.dispose ();

095}

096}

097//Get to proportional optimum size

098 private static Size newsize (int maxwidth, int maxheight, int width, int height)

099 {

Double w = 0.0;

Double h = 0.0;

102 Double SW = convert.todouble (width);

Double sh = convert.todouble (height);

Double MW = convert.todouble (maxwidth);

Double MH = convert.todouble (maxheight);

if (SW < MW && SH < MH)

107 {

108 W = SW;

109 h = sh;

110}

' Else if ((sw/sh) > (MW/MH)

112 {

113 W = maxwidth;

114 H = (W * sh)/SW;

115}

116 Else

117 {

118 h = maxheight;

119 W = (H * sw)/sh;

120}

121 return New Size (Convert.ToInt32 (w), Convert.ToInt32 (h));

122}

123///

124///Get Picture type

125///

126///

127///

128 private static ImageCodecInfo Getencoderinfo (String mimetype)

129 {

130 Int J;

131 imagecodecinfo[] encoders;

132 encoders = Imagecodecinfo.getimageencoders ();

for (j = 0; J < encoders. Length; ++J)

134 {

135 if (Encoders[j]. MimeType = = mimetype)

136 return ENCODERS[J];

137}

138 return null;

139}

140///

141///Add watermark Effect

142///

143///Input Path

144///Output Path

145///Watermark File path

146///Watermark near the right pixel of the picture

147///Watermark near the bottom of the pixel

148///Transparency

149 public static void Setwatermark (String fileName, String newfilename,string waterimg,int rightspace,intbottomspace, int lucencypercent)

150 {

151 using (System.Drawing.Image Image = System.Drawing.Image.FromFile (fileName))

152 {

153 imagemodification WM = new Imagemodification ();

154 Wm. Drawedimagepath = waterimg; Watermark Picture

The Wm. Modifyimagepath = FileName; The path of the picture

156 Wm. Rightspace = Rightspace; Watermark Location

157 Wm. Bottoamspace = image. Height-bottomspace; Mercury position

158 Wm. Lucencypercent = lucencypercent; Transparency

159 Wm. Outpath = NewFileName; The file name that was generated

160 Wm. DrawImage ();

161 image. Dispose ();

162}

163}

164}

165}

        Note : More wonderful articles please pay attention to the triple programming Tutorial column.

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.