本篇文章给大家带来的内容是介绍io帮助类是什么?io帮助类的实例介绍(附代码)。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
using system;using system.io;using system.web;using system.drawing;using system.drawing.imaging;using system.drawing.drawing2d;using system.xml.serialization;namespace zmm.core{ /// <summary> /// io帮助类 /// </summary> public class iohelper { //是否已经加载了jpeg编码解码器 private static bool _isloadjpegcodec = false; //当前系统安装的jpeg编码解码器 private static imagecodecinfo _jpegcodec = null; /// <summary> /// 获得文件物理路径 /// </summary> /// <returns></returns> public static string getmappath(string path) { if (httpcontext.current != null) { return httpcontext.current.server.mappath(path); } else { return system.web.hosting.hostingenvironment.mappath(path); } } #region 序列化 /// <summary> /// xml序列化 /// </summary> /// <param name="obj">序列对象</param> /// <param name="filepath">xml文件路径</param> /// <returns>是否成功</returns> public static bool serializetoxml(object obj, string filepath) { bool result = false; filestream fs = null; try { fs = new filestream(filepath, filemode.create, fileaccess.write, fileshare.readwrite); xmlserializer serializer = new xmlserializer(obj.gettype()); serializer.serialize(fs, obj); result = true; } catch (exception ex) { throw ex; } finally { if (fs != null) fs.close(); } return result; } /// <summary> /// xml反序列化 /// </summary> /// <param name="type">目标类型(type类型)</param> /// <param name="filepath">xml文件路径</param> /// <returns>序列对象</returns> public static object deserializefromxml(type type, string filepath) { filestream fs = null; try { fs = new filestream(filepath, filemode.open, fileaccess.read, fileshare.readwrite); xmlserializer serializer = new xmlserializer(type); return serializer.deserialize(fs); } catch (exception ex) { throw ex; } finally { if (fs != null) fs.close(); } } #endregion #region 水印,缩略图 /// <summary> /// 获得当前系统安装的jpeg编码解码器 /// </summary> /// <returns></returns> public static imagecodecinfo getjpegcodec() { if (_isloadjpegcodec == true) return _jpegcodec; imagecodecinfo[] codecslist = imagecodecinfo.getimageencoders(); foreach (imagecodecinfo codec in codecslist) { if (codec.mimetype.indexof(jpeg) > -1) { _jpegcodec = codec; break; } } _isloadjpegcodec = true; return _jpegcodec; } /// <summary> /// 生成缩略图 /// </summary> /// <param name="imagepath">图片路径</param> /// <param name="thumbpath">缩略图路径</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void generatethumb(string imagepath, string thumbpath, int width, int height, string mode) { image image = image.fromfile(imagepath); string extension = imagepath.substring(imagepath.lastindexof(.)).tolower(); imageformat imageformat = null; switch (extension) { case .jpg: case .jpeg: imageformat = imageformat.jpeg; break; case .bmp: imageformat = imageformat.bmp; break; case .png: imageformat = imageformat.png; break; case .gif: imageformat = imageformat.gif; break; default: imageformat = imageformat.jpeg; break; } int towidth = width > 0 ? width : image.width; int toheight = height > 0 ? height : image.height; int x = 0; int y = 0; int ow = image.width; int oh = image.height; switch (mode) { case hw://指定高宽缩放(可能变形) break; case w://指定宽,高按比例 toheight = image.height * width / image.width; break; case h://指定高,宽按比例 towidth = image.width * height / image.height; break; case cut://指定高宽裁减(不变形) if ((double)image.width / (double)image.height > (double)towidth / (double)toheight) { oh = image.height; ow = image.height * towidth / toheight; y = 0; x = (image.width - ow) / 2; } else { ow = image.width; oh = image.width * height / towidth; x = 0; y = (image.height - oh) / 2; } break; default: break; } //新建一个bmp image bitmap = new bitmap(towidth, toheight); //新建一个画板 graphics g = graphics.fromimage(bitmap); //设置高质量插值法 g.interpolationmode = interpolationmode.high; //设置高质量,低速度呈现平滑程度 g.smoothingmode = smoothingmode.highquality; //清空画布并以透明背景色填充 g.clear(color.transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.drawimage(image, new rectangle(0, 0, towidth, toheight), new rectangle(x, y, ow, oh), graphicsunit.pixel); try { bitmap.save(thumbpath, imageformat); } catch (exception ex) { throw ex; } finally { if (g != null) g.dispose(); if (bitmap != null) bitmap.dispose(); if (image != null) image.dispose(); } } /// <summary> /// 生成图片水印 /// </summary> /// <param name="originalpath">源图路径</param> /// <param name="watermarkpath">水印图片路径</param> /// <param name="targetpath">保存路径</param> /// <param name="position">位置</param> /// <param name="opacity">透明度</param> /// <param name="quality">质量</param> public static void generateimagewatermark(string originalpath, string watermarkpath, string targetpath, int position, int opacity, int quality) { image originalimage = null; image watermarkimage = null; //图片属性 imageattributes attributes = null; //画板 graphics g = null; try { originalimage = image.fromfile(originalpath); watermarkimage = new bitmap(watermarkpath); if (watermarkimage.height >= originalimage.height || watermarkimage.width >= originalimage.width) { originalimage.save(targetpath); return; } if (quality < 0 || quality > 100) quality = 80; //水印透明度 float iii; if (opacity > 0 && opacity <= 10) iii = (float)(opacity / 10.0f); else iii = 0.5f; //水印位置 int x = 0; int y = 0; switch (position) { case 1: x = (int)(originalimage.width * (float).01); y = (int)(originalimage.height * (float).01); break; case 2: x = (int)((originalimage.width * (float).50) - (watermarkimage.width / 2)); y = (int)(originalimage.height * (float).01); break; case 3: x = (int)((originalimage.width * (float).99) - (watermarkimage.width)); y = (int)(originalimage.height * (float).01); break; case 4: x = (int)(originalimage.width * (float).01); y = (int)((originalimage.height * (float).50) - (watermarkimage.height / 2)); break; case 5: x = (int)((originalimage.width * (float).50) - (watermarkimage.width / 2)); y = (int)((originalimage.height * (float).50) - (watermarkimage.height / 2)); break; case 6: x = (int)((originalimage.width * (float).99) - (watermarkimage.width)); y = (int)((originalimage.height * (float).50) - (watermarkimage.height / 2)); break; case 7: x = (int)(originalimage.width * (float).01); y = (int)((originalimage.height * (float).99) - watermarkimage.height); break; case 8: x = (int)((originalimage.width * (float).50) - (watermarkimage.width / 2)); y = (int)((originalimage.height * (float).99) - watermarkimage.height); break; case 9: x = (int)((originalimage.width * (float).99) - (watermarkimage.width)); y = (int)((originalimage.height * (float).99) - watermarkimage.height); break; } //颜色映射表 colormap colormap = new colormap(); colormap.oldcolor = color.fromargb(255, 0, 255, 0); colormap.newcolor = color.fromargb(0, 0, 0, 0); colormap[] newcolormap = { colormap }; //颜色变换矩阵,iii是设置透明度的范围0到1中的单精度类型 float[][] newcolormatrix ={ new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, iii, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; //定义一个 5 x 5 矩阵 colormatrix matrix = new colormatrix(newcolormatrix); //图片属性 attributes = new imageattributes(); attributes.setremaptable(newcolormap, coloradjusttype.bitmap); attributes.setcolormatrix(matrix, colormatrixflag.default, coloradjusttype.bitmap); //画板 g = graphics.fromimage(originalimage); //绘制水印 g.drawimage(watermarkimage, new rectangle(x, y, watermarkimage.width, watermarkimage.height), 0, 0, watermarkimage.width, watermarkimage.height, graphicsunit.pixel, attributes); //保存图片 encoderparameters encoderparams = new encoderparameters(); encoderparams.param[0] = new encoderparameter(encoder.quality, new long[] { quality }); if (getjpegcodec() != null) originalimage.save(targetpath, _jpegcodec, encoderparams); else originalimage.save(targetpath); } catch (exception ex) { throw ex; } finally { if (g != null) g.dispose(); if (attributes != null) attributes.dispose(); if (watermarkimage != null) watermarkimage.dispose(); if (originalimage != null) originalimage.dispose(); } } /// <summary> /// 生成文字水印 /// </summary> /// <param name="originalpath">源图路径</param> /// <param name="targetpath">保存路径</param> /// <param name="text">水印文字</param> /// <param name="textsize">文字大小</param> /// <param name="textfont">文字字体</param> /// <param name="position">位置</param> /// <param name="quality">质量</param> public static void generatetextwatermark(string originalpath, string targetpath, string text, int textsize, string textfont, int position, int quality) { image originalimage = null; //画板 graphics g = null; try { originalimage = image.fromfile(originalpath); //画板 g = graphics.fromimage(originalimage); if (quality < 0 || quality > 100) quality = 80; font font = new font(textfont, textsize, fontstyle.regular, graphicsunit.pixel); sizef sizepair = g.measurestring(text, font); float x = 0; float y = 0; switch (position) { case 1: x = (float)originalimage.width * (float).01; y = (float)originalimage.height * (float).01; break; case 2: x = ((float)originalimage.width * (float).50) - (sizepair.width / 2); y = (float)originalimage.height * (float).01; break; case 3: x = ((float)originalimage.width * (float).99) - sizepair.width; y = (float)originalimage.height * (float).01; break; case 4: x = (float)originalimage.width * (float).01; y = ((float)originalimage.height * (float).50) - (sizepair.height / 2); break; case 5: x = ((float)originalimage.width * (float).50) - (sizepair.width / 2); y = ((float)originalimage.height * (float).50) - (sizepair.height / 2); break; case 6: x = ((float)originalimage.width * (float).99) - sizepair.width; y = ((float)originalimage.height * (float).50) - (sizepair.height / 2); break; case 7: x = (float)originalimage.width * (float).01; y = ((float)originalimage.height * (float).99) - sizepair.height; break; case 8: x = ((float)originalimage.width * (float).50) - (sizepair.width / 2); y = ((float)originalimage.height * (float).99) - sizepair.height; break; case 9: x = ((float)originalimage.width * (float).99) - sizepair.width; y = ((float)originalimage.height * (float).99) - sizepair.height; break; } g.drawstring(text, font, new solidbrush(color.white), x + 1, y + 1); g.drawstring(text, font, new solidbrush(color.black), x, y); //保存图片 encoderparameters encoderparams = new encoderparameters(); encoderparams.param[0] = new encoderparameter(encoder.quality, new long[] { quality }); if (getjpegcodec() != null) originalimage.save(targetpath, _jpegcodec, encoderparams); else originalimage.save(targetpath); } catch (exception ex) { throw ex; } finally { if (g != null) g.dispose(); if (originalimage != null) originalimage.dispose(); } } #endregion }}
以上就是io帮助类是什么?io帮助类的实例介绍(附代码)的详细内容。