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类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. 【作品】超实用C++分数类

    引言 我们说,编程语言的精髓在于封装,而面向对象语言完胜面向过程语言的原因就是具有更好的可封装性,而C++就是这样的一种多范型语言,常用而复杂的工作完全不必在每一份源文件中重敲,就好像我们不需要自己手 ...

  3. slf4j+logback搭建超实用的日志管理模块

    文章转自http://www.2cto.com/kf/201702/536097.html slf4j+logback搭建超实用的日志管理模块(对日志有编号管理):日志功能在服务器端再常见不过了,我们 ...

  4. U3D教程宝典之两步实现超实用的XML存档

    两步实现超实用的XML存档 本套存档的优点:易使用,跨平台,防作弊(内容加密 + 防拷贝) 脚本下载地址 使用方法非常简单:把GameDataManager和XmlSaver两个脚本添加至工程后(1) ...

  5. Android中三种超实用的滑屏方式汇总(转载)

    Android中三种超实用的滑屏方式汇总   现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...

  6. 《超实用的Node.js代码段》连载三:Node.js深受欢迎的六大原因

    <超实用的Node.js代码段>连载一:获取Buffer对象字节长度 <超实用的Node.js代码段>连载二:正确拼接Buffer Node.js是一种后起的优秀服务器编程语言 ...

  7. 超实用的HTML代码段(赵荣娇)

    第1章 创建HTML文档 11.1 HTML文档的基本结构 2 <html> <head> <title>Title of page</title> & ...

  8. C++ 可配置的类工厂

    项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...

  9. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

随机推荐

  1. 【vue】MongoDB+Nodejs+express+Vue后台管理项目Demo

    ¶项目分析 一个完整的网站服务架构,包括:   1.web frame ---这里应用express框架   2.web server ---这里应用nodejs   3.Database ---这里 ...

  2. Segment Tree Beats 区间最值问题

    Segment Tree Beats 区间最值问题 线段树一类特殊技巧! 引出:CF671C Ultimate Weirdness of an Array 其实是考试题,改题的时候并不会区间取最值,区 ...

  3. 20155202张旭 Exp7 网络欺诈技术防范

    20155202张旭 Exp7 网络欺诈技术防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击? 在同一局域网下比较容易受到DNS spoof攻击,攻击者可以冒充域名服务器,来发送伪造 ...

  4. 20155217《网络对抗》Exp04 恶意代码分析

    20155217<网络对抗>Exp04 恶意代码分析 实践内容 使用schtasks指令监控系统运行 使用sysmon工具监控系统运行 使用virscan分析恶意软件 使用systrace ...

  5. 20155223 Exp7 网络欺诈防范

    20155223 Exp7 网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击? 无设防或防护力特别弟弟低的公共局域网,或者是在同一个局域网下. 在日常生活工作中如何防范以上两 ...

  6. 20155229《网络对抗技术》Exp4:恶意代码分析

    实验内容 使用schtasks指令监控系统运行 schtasks指令:允许管理员在本地或远程系统上创建计划任务. SCHTASKS /Create [/S system [/U username [/ ...

  7. [SPOJ2939]Qtree5

    [SPOJ2939]Qtree5 Tags:题解 题意 链接 给你\(n\)个节点的黑白树,初始全黑.每次可以翻转某点颜色,或查询距离某点最近的白点的距离.\(n\le 10^5\).强制LCT,不准 ...

  8. Invitation Cards POJ-1511 (spfa)

    题目链接:Invitation Cards 题意: 给出一张有向图,现在要求从1到其他所有的结点的最小路径和与从所有其他结点到1的最小路径和之和. 题解: 求最小路径可以用SPFA来求解.从1到其他结 ...

  9. vue-router单页应用简单示例(三)

    用vue-resource向服务器请求数据 我们主要来了解一下以下内容: 模拟服务端返回数据 用vue-resource向服务器请求数据 模拟服务器返回数据   我们用vue-cli创建的项目中,已经 ...

  10. 使用阿里云Python SDK管理ECS安全组

    准备工作 本机操作系统:CentOS7 python版本:python2.7.5 还需要准备如下信息: 一个云账号.Access Key ID.Access Key Secret.安全组ID.Regi ...