超实用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 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
随机推荐
- 【H5】直接拨打电话
一般<a href="tel:400-663-5999">400-663-5999</a>实现. 而形如<a href="tel:*9204 ...
- 大数据入门第二十五天——elasticsearch入门
一.概述 推荐路神的ES权威指南翻译:https://es.xiaoleilu.com/010_Intro/00_README.html 官网:https://www.elastic.co/cn/pr ...
- CLR回收非托管资源
一.非托管资源 在<垃圾回收算法之引用计数算法>.<垃圾回收算法之引用跟踪算法>和<垃圾回收算法之引用跟踪算法>这3篇文章中,我们介绍了垃圾回收的一些基本概念和原理 ...
- Luogu P2483 【模板】k短路([SDOI2010]魔法猪学院)
说实话,看到这道题的洛谷评级我傻了(传说中的最高难度) 然后看完题目才确定这真的是一道k短路的裸题. 也就敲了个A*吧,15分钟竟然没有调试一遍过. 欧洲玄学. 看题目,主要是找几条从1走到n的路加起 ...
- StringUtils类方法归纳
StringUtils方法概览 IsEmpty/IsBlank - checks if a String contains text IsEmpty/IsBlank – 检查字符串是否有内容. Tri ...
- python3获取文件及文件夹大小
获取文件大小 os.path.getsize(file_path):file_path为文件路径 >>> import os >>> os.path.getsize ...
- zabbix设置微信报警的配置过程
zabbix设置微信报警的配置过程 转发:https://blog.csdn.net/qq_31613055/article/details/78831607 微信企业号的申请 注册的地址https: ...
- 更改jenkins的默认工作空间并迁移插件和配置数据
最近刚使用阿里云ECS centos服务器,购买的是40G的系统盘,60G的数据盘. 昨天在查看服务器磁盘空间的时候,偶然发现 /dev/vda1 下面40G的空间已使用17G, 因为服务器才开始使用 ...
- 英特尔和 Valve* 将英特尔® Embree 光线追踪技术添加至全新 Steam* Audio 插件
本文从英特尔® Embree 光线追踪技术着手,深入探讨英特尔与 Valve 合作带来的优势:一方面,开发人员使用英特尔高度优化的库创建场景,可以显著加快编译速度:另一方面,逼真的声效可以增强游戏性, ...
- LintCode——筛子求和
描述:扔n个骰子,向上面的数字之和为 S .给定 Given n,请列出所有可能的 S 值及其相应的概率. 样例:给定n=1,返回 [ [1, 0.17], [2, 0.17], [3, 0.17], ...