超实用Image类
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices; public static class ImageHelper
{
private static float[][] ColorMatrix = null; static ImageHelper()
{
float[][] numArray = new float[5][];
numArray[0] = new float[] { 0.299f, 0.299f, 0.299f, 0f, 0f };
numArray[1] = new float[] { 0.587f, 0.587f, 0.587f, 0f, 0f };
numArray[2] = new float[] { 0.114f, 0.114f, 0.114f, 0f, 0f };
float[] numArray2 = new float[5];
numArray2[3] = 1f;
numArray[3] = numArray2;
numArray2 = new float[5];
numArray2[4] = 1f;
numArray[4] = numArray2;
ColorMatrix = numArray;
} public static Bitmap ConstructRGB24Bitmap(byte[] coreData, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
Marshal.Copy(coreData, 0, bitmapdata.Scan0, coreData.Length);
bitmap.UnlockBits(bitmapdata);
return bitmap;
} public static Image Convert(byte[] buff)
{
MemoryStream stream = new MemoryStream(buff);
Image image = Image.FromStream(stream);
stream.Close();
return image;
} public static byte[] Convert(Image img)
{
Image image = CopyImageDeeply(img);
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
byte[] buffer = stream.ToArray();
stream.Close();
image.Dispose();
return buffer;
} public static Bitmap ConvertToGrey(Image origin)
{
Bitmap image = new Bitmap(origin);
Graphics graphics = Graphics.FromImage(image);
ImageAttributes imageAttr = new ImageAttributes();
System.Drawing.Imaging.ColorMatrix newColorMatrix = new System.Drawing.Imaging.ColorMatrix(ColorMatrix);
imageAttr.SetColorMatrix(newColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
graphics.Dispose();
return image;
} public static Icon ConvertToIcon(Image img, int iconLength)
{
using (Bitmap bitmap = new Bitmap(img, new Size(iconLength, iconLength)))
{
return Icon.FromHandle(bitmap.GetHicon());
}
} public static Image ConvertToJPG(Image img)
{
MemoryStream stream = new MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
Image image = Image.FromStream(stream);
stream.Close();
return image;
} public static Image CopyImageDeeply(Image img)
{
Bitmap image = new Bitmap(img.Width, img.Height, img.PixelFormat);
Graphics graphics = Graphics.FromImage(image);
graphics.DrawImage(img, 0, 0, img.Width, img.Height);
graphics.Dispose();
return image;
} public static byte[] GetRGB24CoreData(Bitmap bm)
{
byte[] destination = new byte[(bm.Width * bm.Height) * 3];
BitmapData bitmapdata = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
Marshal.Copy(bitmapdata.Scan0, destination, 0, destination.Length);
bm.UnlockBits(bitmapdata);
return destination;
} public static bool IsGif(Image img)
{
FrameDimension dimension = new FrameDimension(img.FrameDimensionsList[0]);
return (img.GetFrameCount(dimension) > 1);
} public static byte[] ReviseRGB24Data(byte[] origin, Size originSize, Size newSize)
{
Bitmap image = ConstructRGB24Bitmap(origin, originSize.Width, originSize.Height);
Bitmap bitmap2 = new Bitmap(newSize.Width, newSize.Height);
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.DrawImage(image, 0f, 0f, new RectangleF(0f, 0f, (float) newSize.Width, (float) newSize.Height), GraphicsUnit.Pixel);
graphics.Dispose();
return GetRGB24CoreData(bitmap2);
} public static void Save(Image img, string path, ImageFormat format)
{
if ((img != null) && (path != null))
{
CopyImageDeeply(img).Save(path, format);
}
}
}
从db里取出image类型字段:
HeadImageData = dr["HeadImageData"] as byte[] ?? null
超实用Image类的更多相关文章
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- 【作品】超实用C++分数类
引言 我们说,编程语言的精髓在于封装,而面向对象语言完胜面向过程语言的原因就是具有更好的可封装性,而C++就是这样的一种多范型语言,常用而复杂的工作完全不必在每一份源文件中重敲,就好像我们不需要自己手 ...
- slf4j+logback搭建超实用的日志管理模块
文章转自http://www.2cto.com/kf/201702/536097.html slf4j+logback搭建超实用的日志管理模块(对日志有编号管理):日志功能在服务器端再常见不过了,我们 ...
- U3D教程宝典之两步实现超实用的XML存档
两步实现超实用的XML存档 本套存档的优点:易使用,跨平台,防作弊(内容加密 + 防拷贝) 脚本下载地址 使用方法非常简单:把GameDataManager和XmlSaver两个脚本添加至工程后(1) ...
- Android中三种超实用的滑屏方式汇总(转载)
Android中三种超实用的滑屏方式汇总 现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...
- 《超实用的Node.js代码段》连载三:Node.js深受欢迎的六大原因
<超实用的Node.js代码段>连载一:获取Buffer对象字节长度 <超实用的Node.js代码段>连载二:正确拼接Buffer Node.js是一种后起的优秀服务器编程语言 ...
- 超实用的HTML代码段(赵荣娇)
第1章 创建HTML文档 11.1 HTML文档的基本结构 2 <html> <head> <title>Title of page</title> & ...
- C++ 可配置的类工厂
项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...
- Android请求网络共通类——Hi_博客 Android App 开发笔记
今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
随机推荐
- Docker:一个装应用的容器
一:简介:你是否经历过“我本地运行没问题啊!““哪个哥们有写死循环了““完了,服务器撑不住了“等等问题,docker就是这么帮你解决问题的工具,它可以帮你把web应用自动化打包和发布,在服务型环境下进 ...
- Android病毒家族及行为(一)
1病毒名称:a.remote.GingerMaste中文名:病毒家族:GingerMast病毒类别:远程控制恶意行为:获取root权限,同时连接远端服务器,在其指令控制下静默下载其它恶意软件,给用户手 ...
- Ubuntu16.04LTS +Qt+boost1.66编译错误:consuming_buffers.hpp: parse error in template argument list
升级gcc版本至 6 以上.. 安装gcc-6系列与安装boost (Ubuntu16.04LTS)
- 微信小程序云开发之云函数创建
云函数 云函数是一段运行在云端的代码,无需管理服务器,在开发工具内编写.一键上传部署即可运行后端代码. 小程序内提供了专门用于云函数调用的 API.开发者可以在云函数内使用 wx-server-sdk ...
- Elasticsearch Java Rest Client API 整理总结 (一)——Document API
目录 引言 概述 High REST Client 起步 兼容性 Java Doc 地址 Maven 配置 依赖 初始化 文档 API Index API GET API Exists API Del ...
- PowerBI开发 第一篇:设计PowerBI报表
PowerBI是微软新一代的交互式报表工具,把相关的静态数据转换为酷炫的可视化的,能够根据filter条件,对数据执行动态筛选,从不同的角度和粒度上分析数据.PowerBI主要由两部分组成:Power ...
- 对html第一次尝试
1.对于写文档 修改后缀为html,双击进入为网页模式. 2.编写网页 1)新建 2)基本格式 <!DOCTYPE html><!-- ...
- Ubuntu侧边任务栏自动隐藏
设置>>Dock>>{自动隐藏Dock}选项打开
- Web挂马方式整理
一:框架挂马 <iframe src=地址 width=0 height=0></iframe> 二:js文件挂马 首先将以下代码 document.write("& ...
- 《口算大作战 2》DLC:算法真奇妙
211614331 王诚荣 211614354 陈斌 --第一次结对作业 DLC DLC:三年级混合运算模块现已更新!现在您可以愉快的使用三年级题库啦.同时您必须拥有本体才能使用此DLC 单击此处查看 ...