[转][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代码编辑 ...
随机推荐
- 第一次玩博客,今天被安利了一个很方便JDBC的基于Spring框架的一个叫SimpleInsert的类,现在就来简单介绍一下
首先先对这段代码的简单介绍,我之前在需要操作JDBC的时候总是会因为经常要重新写SQL语句感到很麻烦.所以就能拿则拿不能拿的就简单地封装了一下. 首先是Insert.Spring框架的JDBC包里面的 ...
- 【Concurrency-ScheduledExecutorService】
简介 线程池执行者在ThreadPoolExecutor的基础上给我们提供了延时(delay)执行和周期执行的功能.性能会优于Timer包. 继承结构 参考: ThreadPoolExecutor E ...
- Java实现inputstream流的复制
获取到一个inputstream后,可能要多次利用它进行read的操作.由于流读过一次就不能再读了,而InputStream对象本身不能复制,而且它也没有实现Cloneable接口,所以得想点办法. ...
- window10单机安装storm集群
适合范围:storm自由开源的分布式实时计算系统,擅长处理海量数据.适合处理实时数据而不是批处理. 安装前的准备 1.安装zookeeper ①下载zookeeperhttps://zookeeper ...
- python基础(八)
一.token加盐处理# import itsdangerous## salt='sdf234^#$@G'# t = itsdangerous.TimedJSONWebSignatureSeriali ...
- DEBUG经历
在两年有余的学习生活中,我不仅在课堂上学到了很多东西,我也在一次次的错误中得到了宝贵的经验和教训.Bug和debug,构成了我生活中不可或缺的一部分. 我在编程中犯过的错误很多,无法一一阐述,再次说一 ...
- 前端自定义format函数,做字符串格式化功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [Python学习笔记] 字符串类型及操作
字符串处理 索引:返回字符串中单个字符 <字符串>[M] 切片:返回字符串中一段字符子串 <字符串>[M:N:K] 字符串格式化使用.format()方法
- 我的linux部署nginx步骤记录
http://www.runoob.com/linux/nginx-install-setup.html 安装prce找不到GCC c++文件 解决方法: yum install gcc-c++^C ...
- C++ 实数类
这是一堆我自己写完都怀疑人生的代码. 或许我见识太少了吧-- 实现一个实数类.这个类也可以看作是分数类.实现了基本的四则运算和比较.另外,与整数或小数的转换还没有实现,无限循环小数转分数也暂时没有实现 ...