[转][C#]ImageHelper
{
internal static class ImageHelper
{
public static Bitmap CloneBitmap(Image source)
{
if (source == null)
return null;
Bitmap image = new Bitmap(source.Width, source.Height);
image.SetResolution(source.HorizontalResolution, source.VerticalResolution);
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImageUnscaled(source, , );
}
return image;
// this can throw OutOfMemory when creating a grayscale image from a cloned bitmap
// return source.Clone() as Bitmap;
}
public static void Save(Image image, Stream stream)
{
Save(image, stream, ImageFormat.Png);
}
public static void Save(Image image, string fileName, ImageFormat format)
{
using (FileStream stream = new FileStream(fileName, FileMode.Create))
{
Save(image, stream, format);
}
}
public static void Save(Image image, Stream stream, ImageFormat format)
{
if (image == null)
return;
if (image is Bitmap)
image.Save(stream, format);
else if (image is Metafile)
{
Metafile emf = null;
using (Bitmap bmp = new Bitmap(, ))
using (Graphics g = Graphics.FromImage(bmp))
{
IntPtr hdc = g.GetHdc();
emf = new Metafile(stream, hdc);
g.ReleaseHdc(hdc);
}
using (Graphics g = Graphics.FromImage(emf))
{
g.DrawImage(image, , );
}
}
}
public static byte[] Load(string fileName)
{
if (!String.IsNullOrEmpty(fileName))
return File.ReadAllBytes(fileName);
return null;
}
public static Image Load(byte[] bytes)
{
if (bytes != null && bytes.Length > )
{
try
{
return new ImageConverter().ConvertFrom(bytes) as Image;
}
catch
{
Bitmap errorBmp = new Bitmap(, );
using (Graphics g = Graphics.FromImage(errorBmp))
{
g.DrawLine(Pens.Red, , , , );
g.DrawLine(Pens.Red, , , , );
}
return errorBmp;
}
}
return null;
}
public static byte[] LoadURL(string url)
{
if (!String.IsNullOrEmpty(url))
{
using (WebClient web = new WebClient())
{
return web.DownloadData(url);
}
}
return null;
}
public static Bitmap GetTransparentBitmap(Image source, float transparency)
{
if (source == null)
return null;
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.Matrix33 = - transparency;
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
int width = source.Width;
int height = source.Height;
Bitmap image = new Bitmap(width, height);
image.SetResolution(source.HorizontalResolution, source.VerticalResolution);
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(Color.Transparent);
g.DrawImage(
source,
new Rectangle(, , width, height),
, , width, height,
GraphicsUnit.Pixel,
imageAttributes);
}
return image;
}
public static Bitmap GetGrayscaleBitmap(Image source)
{
Bitmap grayscaleBitmap = new Bitmap(source.Width, source.Height, source.PixelFormat);
// Red should be converted to (R*.299)+(G*.587)+(B*.114)
// Green should be converted to (R*.299)+(G*.587)+(B*.114)
// Blue should be converted to (R*.299)+(G*.587)+(B*.114)
// Alpha should stay the same.
ColorMatrix grayscaleMatrix = new ColorMatrix(new float[][]{
new float[] {0.299f, 0.299f, 0.299f, , },
new float[] {0.587f, 0.587f, 0.587f, , },
new float[] {0.114f, 0.114f, 0.114f, , },
new float[] { , , , , },
new float[] { , , , , }});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(grayscaleMatrix);
// Use a Graphics object from the new image
using (Graphics graphics = Graphics.FromImage(grayscaleBitmap))
{
// Draw the original image using the ImageAttributes we created
graphics.DrawImage(source,
new Rectangle(, , grayscaleBitmap.Width, grayscaleBitmap.Height),
, , grayscaleBitmap.Width, grayscaleBitmap.Height,
GraphicsUnit.Pixel, attributes);
}
return grayscaleBitmap;
}
}
}
来自:https://github.com/FastReports/FastReport
[转][C#]ImageHelper的更多相关文章
- 最全的C#图片处理帮助类ImageHelper
最全的C#图片处理帮助类ImageHelper.cs 方法介绍: 生成缩略图 图片水印处理方法 图片水印位置处理方法 文字水印处理方法 文字水印位置的方法 调整光暗 反色处理 浮雕处理 拉伸图片 滤色 ...
- C# ImageHelper
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; ...
- WorldWind源码剖析系列:图像助手类ImageHelper
图像助手类ImageHelper封装了对各种图像的操作.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: public static bool IsGdiSupportedImag ...
- App开发流程之加密工具类
科技优家 2016-09-08 18:10 从这篇记录开始,记录的都算是干货了,都是一些编程日常的积累. 我建议先将基础的工具加入项目,后续的开发效率会呈指数增长.如果在专注功能开发过程中,才发现缺少 ...
- Devexpress
1.隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值 sValue=Table.Rows[gri ...
- DevExpress GridControl使用方法
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...
- Winform开发框架之肖像显示保存控件的实现
我们在开发一些Winform程序的时候,除了常规的显示普通数据外,有的时候需要显示一些人员肖像或者一些车辆等物体的图片,一般这些内容较小,所以以二进制存储在数据库是一个不错的方案.但由于它们虽然很常用 ...
- The Engine Document of JustWeEngine
JustWeEngine - Android FrameWork An easy open source Android Native Game FrameWork. Github Game core ...
- JustWeTools - 自定义控件集
JustWeTools - Some useful tools 项目地址 JustWe 现在有哪些模块? View自定义控件 PaintView画图工具(包含重构压感新版) CodeView代码编辑 ...
随机推荐
- vue 用户停留页面超过30分钟未操作 强制退出到登录页面
先说下主要实现思路,通过给你的根节点绑定mouseover事件,首先声明下当前时间,每次滑过时记录下滑过的时间,两个时间转化成毫秒数,进行对比,如果超过30分钟,则清除token,跳转到login.h ...
- CodeBlocks无法调试的解决方法
闲话: 万万没想到我也会写这个东西.一开始软件工程课的时候老师要求我们写博客园,一直都是被动地在写博客.刚刚在重温C语言的时候发现的各种各样问题觉得还是写下来比较好,一旦以后自己又忘了呢……(摊手 顺 ...
- SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关
0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ...
- CMake 笔记
1. configure_file configure_file()让你可以在代码文件中使用CMake中定义的变量. configure_file(<input> <output&g ...
- ubuntu apt 安装 mpv
安装 curl -s https://non-gnu.uvt.nl/debian/uvt_key.gpg | sudo apt-key add - sudo add-apt-repository &q ...
- js实现软键盘
<p><img id="img" onclick="javascript:var s=document.createElement('script'); ...
- Stackoverflow热门问题
1. JavaScript如何重定向到其他网页 如何使用JavaScript将用户从一个网页重定向到另一个网页? 2. JavaScript闭包是如何工作的 只知道JavaScript闭包的概念,但是 ...
- 转 Mac 下自带的中文输入法不显示汉字提示问题
原文 https://blog.csdn.net/moxi_wang/article/details/50721326 当时聊天的时候不知道那个手指头按错了什么键 导致Mac自带的中文输入法不能提示显 ...
- 初用jdbc来运行事务
dao层 public Connection getConnection() throws Exception { Class.forName(driver); if (con == null || ...
- Python学习之路基础篇--11-12Python基础,函数的装饰器
对于装饰器来说,就是在不改变函数的调用的情况下,对函数的前后增加了些许功能,这完全符合函数的 开放封闭 原则.装饰器的本质 其实就是一个闭包函数. 这是一个装饰器的步骤图 ret = func(*ar ...