using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using ReligionServer.constant; namespace ReligionServer.util { /// <summary>
/// 图片操作的工具类
/// </summary>
public class ImageUtil { /// <summary>
/// Base64字符转图片单个存储: 将传入的Base64字符串image解析并转存到指定的target目录下, 返回FileInfo
/// </summary>
/// <param name="context"></param>
/// <param name="image"></param>
/// <returns></returns>
public static Model.FileInfo Base64ImageInsert(HttpContext context, String image) {
return ConvertBase64StringToImageAndInsert(context, image);
} /// <summary>
/// Base64字符转图片批量存储: 将传入的Base64字符串集合imageList解析并转存到指定的target目录下, 返回成功的FileInfo的List集合
/// </summary>
/// <param name="imageList"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public static List<Model.FileInfo> Base64ImageInsertBatch(HttpContext context, List<String> imageList) {
List<Model.FileInfo> fileInfoList = new List<Model.FileInfo>();
foreach (String item in imageList) {
Model.FileInfo temp = Base64ImageInsert(context, item);
if (temp != null) {
fileInfoList.Add(temp);
}
}
return fileInfoList;
} /// <summary>
/// 将传入Base64字符串转换为BitMap并进行转存
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static Model.FileInfo ConvertBase64StringToImageAndInsert(HttpContext context, String value) {
Model.FileInfo fileInfo = ParametersUtil.GetInstanceFormRequest<Model.FileInfo>(context, new Model.FileInfo());//使用ForeignKey初始化FileInfo
if (!CommonUtil.IsEmpty(fileInfo.ForeignKey)) {
MemoryStream memoryStream = null;
Bitmap bitmap = null;
try {
byte[] bytes = Convert.FromBase64String(parseBase64String(value));
String value_tmep = parseBase64String(value);
memoryStream = new MemoryStream(bytes);
bitmap = new Bitmap(memoryStream);
if (bitmap != null) {
String directory = DateUtil.CurrentDateTimeValue();//转存图片的文件夹
String targetPhysicalFilePath = context.Request.PhysicalApplicationPath + UploadConstant.UPLOAD_FILE_PATH + directory;//转存图片文件夹的物理路径
if (!Directory.Exists(targetPhysicalFilePath)) {
Directory.CreateDirectory(targetPhysicalFilePath);
}
String targetPath = targetPhysicalFilePath + "/" + CommonUtil.CreateFileName() + SuffixConstant.JPEG;//转存图片的全路径
//填充FileInfo
fileInfo.FileCode = CommonUtil.CreateId();
fileInfo.Name = CommonUtil.CreateFileName() + SuffixConstant.JPEG;
fileInfo.setType(SuffixConstant.JPEG.Substring());
fileInfo.Desc = parseBase64String(value);
fileInfo.Access = ;//0表示关联, 1表示独立
fileInfo.Path = UploadConstant.UPLOAD_FILE_PATH + directory + "/" + CommonUtil.CreateFileName() + SuffixConstant.JPEG;
fileInfo.setCreateTime();
bitmap.Save(targetPath, System.Drawing.Imaging.ImageFormat.Png);
return fileInfo;
}
} catch (Exception e) {
//暂时不做处理
System.Diagnostics.Debug.WriteLine(e.Message);
} finally {
if (null != memoryStream) {
//GDI+ 中发生一般性错误, 这个问题一般为输出路径不存在, 或者是对当前输出路径没有权限, 再者就是文件的生命周期问题 这个问题如下所示
//在Image的整个操作过程中, memoryStream是不能关闭的, 只有等待Image的操作完毕后才可以关闭, 否则会出现 GDI+ 中发生一般性错误
//BitMap继承Image
//百度解释:Bitmap 对象或一个 Image 对象从一个文件, 构造时该文件仍保留锁定对于对象的生存期。 因此, 无法更改图像并将其保存回它产生相同的文件
memoryStream.Close();
}
if (null != bitmap) {
//在Image操作完毕后, 回收Image所持有的资源
bitmap.Dispose();
}
}
}
return null;
} /// <summary>
/// 批量将传入的Base64字符串集合转为BitMap的List集合(暂时没有使用到)
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
private static List<Model.FileInfo> ConvertBase64StringToImageBatch(HttpContext context, List<String> values) { List<Model.FileInfo> fileInfoList = new List<Model.FileInfo>();
if (values != null) {
foreach (String item in values) {
fileInfoList.Add(ConvertBase64StringToImageAndInsert(context, item));
}
}
return fileInfoList;
} /// <summary>
/// 对传入的Base64字符串进行一定的处理, 不然在做图片转换的时候会有影响
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static String parseBase64String(String value) {
String result = String.Empty;//表示空字符串, 次字符串只读 int index = value.IndexOf(","); result = value.Substring(index + , value.Length - index - ); return result;
}
}
}

C#——图片操作类简单封装的更多相关文章

  1. C#——JSON操作类简单封装(DataContractJsonSerializer)

    Framework版本:.Net Framework 4 使用DataContractJsonSerializer时,实体请使用注解,格式如下 1.实体使用注解,并且提供get和set的public访 ...

  2. C#——文件操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...

  3. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  4. MySQL操作类的封装(PHP)

    <?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操 ...

  5. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (二) 图片裁剪

    图片裁剪参见: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail        一个js插件 http://www.mikes ...

  6. php 图片操作类,支持生成缩略图,添加水印,上传缩略图

    <?php class Image {     //类开始     public $originimage = ""; //源图片文件地址     public $image ...

  7. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (三) 图片裁剪

    官方的例子 是 长方形的. 我这里 用于 正方形的头像 所以  做如下  修改 #preview-pane .preview-container { width: 73px; height: 73px ...

  8. ADO.NET操作PostgreSQL:数据库操作类(已封装)

    1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...

  9. ADO.NET操作SQL Server:数据库操作类(已封装)

    1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...

随机推荐

  1. Python 扩展知识:编程习惯

    1. 使用四个空格作为缩进而不是Tab键2. 函数名定义时第二个单词首字母大写,如 getNum,类名定义时所有单词首字母大写,如 GetNum

  2. securecrt 的安装

    http://bbs.feng.com/read-htm-tid-6939481.html ssh  -t  ip地址@用户名  -p 22

  3. nutch爬取时Exception in thread “main” java.io.IOException: Job failed!

    用cygwin运行nutch 1.2爬取提示IOException: $ bin/nutch crawl urls -dir crawl -depth 3 -topN 10 crawl started ...

  4. 一加氢OS发布会 观看小结

    观看地址:http://v.youku.com/v_show/id_XMTI0ODkzNTg5Mg==.html?from=s1.8-1-1.2八种基本色调.33%自由:top33%位置放壁纸,底部桌 ...

  5. Sencha Cmd创建Ext JS示例项目

    Sencha提供了免费的Cmd工具,可以用来创建Ext JS项目并提供了一些便利的功能. Sencha也在官方文档中提供了一个示例来演示如何创建一个Sample Login App. 本文就介绍一下这 ...

  6. 网络子系统46_ip协议数据帧的转发

    //调用路径ip_rcv->ip_rcv_finish->dst_input->(skb->dst->input) //ip_forward以回调函数的形式,保存在skb ...

  7. ubuntu下nginx编译安装

    安装依赖库: sudo apt-get install libgd2-xpm sudo apt-get install libgd2-xpm-dev sudo apt-get install libg ...

  8. fiddler 面板内显示IP地址

    1.打开fiddler, 快捷键Ctrl+R  (菜单->Rules->Customize Rules…) 然后在CustomRules.js文件里Ctrl+F查找字符串:static f ...

  9. Oracle归档文件夹权限设置错误导致的数据库问题解决

    把oracle设置为归档模式并且为归档文件新建文件夹 /home/oracle/app/oracle/arch/orcl 但是在启动或者备份时候经常性出现错误 startup报错 startup同时日 ...

  10. Python-Select/Poll/Epoll使用

    select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被内核修改标志位,使 ...