IO帮助类是什么?IO帮助类的实例介绍(附代码)

IO帮助类是什么?IO帮助类的实例介绍(附代码),第1张

概述IO帮助类是什么?IO帮助类的实例介绍(附代码) 本篇文章给大家带来的内容是介绍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帮助类的实例介绍(附代码)全部内容,希望文章能够帮你解决IO帮助类是什么?IO帮助类的实例介绍(附代码)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1211702.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存