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

  1. 最全的C#图片处理帮助类ImageHelper

    最全的C#图片处理帮助类ImageHelper.cs 方法介绍: 生成缩略图 图片水印处理方法 图片水印位置处理方法 文字水印处理方法 文字水印位置的方法 调整光暗 反色处理 浮雕处理 拉伸图片 滤色 ...

  2. C# ImageHelper

    using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; ...

  3. WorldWind源码剖析系列:图像助手类ImageHelper

    图像助手类ImageHelper封装了对各种图像的操作.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: public static bool IsGdiSupportedImag ...

  4. App开发流程之加密工具类

    科技优家 2016-09-08 18:10 从这篇记录开始,记录的都算是干货了,都是一些编程日常的积累. 我建议先将基础的工具加入项目,后续的开发效率会呈指数增长.如果在专注功能开发过程中,才发现缺少 ...

  5. Devexpress

    1.隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; 2.得到当前选定记录某字段的值 sValue=Table.Rows[gri ...

  6. DevExpress GridControl使用方法

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 (1).gridView.AddN ...

  7. Winform开发框架之肖像显示保存控件的实现

    我们在开发一些Winform程序的时候,除了常规的显示普通数据外,有的时候需要显示一些人员肖像或者一些车辆等物体的图片,一般这些内容较小,所以以二进制存储在数据库是一个不错的方案.但由于它们虽然很常用 ...

  8. The Engine Document of JustWeEngine

    JustWeEngine - Android FrameWork An easy open source Android Native Game FrameWork. Github Game core ...

  9. JustWeTools - 自定义控件集

    JustWeTools - Some useful tools 项目地址 JustWe 现在有哪些模块? View自定义控件 PaintView画图工具(包含重构压感新版) CodeView代码编辑 ...

随机推荐

  1. c++ 第二章知识梳理

    2.1.c++语言概括 2.1.1)c++的产生 一个更好的c,由c演变而来 2.1.2)c++的特点 一是尽量兼容c,二是支持面向对象的方法.更安全,且简洁高效. 2.1.3~2.1.5 多数和C相 ...

  2. 二进制32位转十进制int

    public class BinaryToDecimal { public static int BinaryToDecimal(int binaryNumber){ int decimal = 0; ...

  3. 使用axios获取本地json数据

    1. 通过搜索 网上说不放在static文件夹中会报错  但是一直报错  放到根目录下的时候不报错了 2. 在main.js中引入axios import axios from 'axios' 3. ...

  4. JavaScript中常用的BOM对象(属性、方法)

    window对象 定义: 一个浏览器窗口实例 与窗口有关的信息(应用程序编程接口) ECMAScript规定的Global对象 方法 open(url),返回标识符 引用 即将打开窗口的.(调用该引用 ...

  5. mysql免安装版的下载与安装

    下载 打开:https://www.mysql.com/downloads/ 1.点击该项:

  6. 图解HTTPS协议

    原文出处: leezhong的博客 我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取.所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议. HTTPS简介 HTTPS其实 ...

  7. PA教材提纲 TAW12-2

    Unit1 Adjustment of SAP Standard Software(SAP标准软件修改) 1.1 Adjusting SAP Standard Software(如何修改SAP标准软件 ...

  8. node.js设置跨域

    app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*& ...

  9. $(window).scroll()无法触发问题

    在微信端开发中遇到一个这种问题:明明用的公共文件(代码如下图),其他页面每次都能触发这个滚动条$(window).scroll事件,以显示右下角“回到顶部”这个按钮图标 但是,问题来了,最该需要使用“ ...

  10. 转载 JAVA gc垃圾回收机制

    thanks:https://m.oschina.net/u/123553 一.GC概要 JVM堆相关知识    为什么先说JVM堆?    JVM的堆是Java对象的活动空间,程序中的类的对象从中分 ...