什么是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
{
///
/// IO帮助类
///
public class IOHelper
{
//是否已经加载了JPEG编码解码器
private static bool _isloadjpegcodec = false;
//当前系统安装的JPEG编码解码器
private static ImageCodecInfo _jpegcodec = null;
///
/// 获得文件物理路径
///
///
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 序列化
///
/// XML序列化
///
/// 序列对象
/// XML文件路径
/// 是否成功
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;
}
///
/// XML反序列化
///
/// 目标类型(Type类型)
/// XML文件路径
/// 序列对象
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 水印,缩略图
///
/// 获得当前系统安装的JPEG编码解码器
///
///
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;
}
///
/// 生成缩略图
///
/// 图片路径
/// 缩略图路径
/// 缩略图宽度
/// 缩略图高度
/// 生成缩略图的方式
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();
}
}
///
/// 生成图片水印
///
/// 源图路径
/// 水印图片路径
/// 保存路径
/// 位置
/// 透明度
/// 质量
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();
}
}
///
/// 生成文字水印
///
/// 源图路径
/// 保存路径
/// 水印文字
/// 文字大小
/// 文字字体
/// 位置
/// 质量
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帮助类及其实例介绍
分享地址:http://www.jxjierui.cn/article/jjisid.html


咨询
建站咨询
