1. {
  2. internal static class ImageHelper
  3. {
  4. public static Bitmap CloneBitmap(Image source)
  5. {
  6. if (source == null)
  7. return null;
  8.  
  9. Bitmap image = new Bitmap(source.Width, source.Height);
  10. image.SetResolution(source.HorizontalResolution, source.VerticalResolution);
  11. using (Graphics g = Graphics.FromImage(image))
  12. {
  13. g.DrawImageUnscaled(source, , );
  14. }
  15. return image;
  16.  
  17. // this can throw OutOfMemory when creating a grayscale image from a cloned bitmap
  18. // return source.Clone() as Bitmap;
  19. }
  20.  
  21. public static void Save(Image image, Stream stream)
  22. {
  23. Save(image, stream, ImageFormat.Png);
  24. }
  25.  
  26. public static void Save(Image image, string fileName, ImageFormat format)
  27. {
  28. using (FileStream stream = new FileStream(fileName, FileMode.Create))
  29. {
  30. Save(image, stream, format);
  31. }
  32. }
  33.  
  34. public static void Save(Image image, Stream stream, ImageFormat format)
  35. {
  36. if (image == null)
  37. return;
  38. if (image is Bitmap)
  39. image.Save(stream, format);
  40. else if (image is Metafile)
  41. {
  42. Metafile emf = null;
  43. using (Bitmap bmp = new Bitmap(, ))
  44. using (Graphics g = Graphics.FromImage(bmp))
  45. {
  46. IntPtr hdc = g.GetHdc();
  47. emf = new Metafile(stream, hdc);
  48. g.ReleaseHdc(hdc);
  49. }
  50. using (Graphics g = Graphics.FromImage(emf))
  51. {
  52. g.DrawImage(image, , );
  53. }
  54. }
  55. }
  56.  
  57. public static byte[] Load(string fileName)
  58. {
  59. if (!String.IsNullOrEmpty(fileName))
  60. return File.ReadAllBytes(fileName);
  61. return null;
  62. }
  63.  
  64. public static Image Load(byte[] bytes)
  65. {
  66. if (bytes != null && bytes.Length > )
  67. {
  68. try
  69. {
  70. return new ImageConverter().ConvertFrom(bytes) as Image;
  71. }
  72. catch
  73. {
  74. Bitmap errorBmp = new Bitmap(, );
  75. using (Graphics g = Graphics.FromImage(errorBmp))
  76. {
  77. g.DrawLine(Pens.Red, , , , );
  78. g.DrawLine(Pens.Red, , , , );
  79. }
  80. return errorBmp;
  81. }
  82. }
  83. return null;
  84. }
  85.  
  86. public static byte[] LoadURL(string url)
  87. {
  88. if (!String.IsNullOrEmpty(url))
  89. {
  90. using (WebClient web = new WebClient())
  91. {
  92. return web.DownloadData(url);
  93. }
  94. }
  95. return null;
  96. }
  97.  
  98. public static Bitmap GetTransparentBitmap(Image source, float transparency)
  99. {
  100. if (source == null)
  101. return null;
  102.  
  103. ColorMatrix colorMatrix = new ColorMatrix();
  104. colorMatrix.Matrix33 = - transparency;
  105. ImageAttributes imageAttributes = new ImageAttributes();
  106. imageAttributes.SetColorMatrix(
  107. colorMatrix,
  108. ColorMatrixFlag.Default,
  109. ColorAdjustType.Bitmap);
  110.  
  111. int width = source.Width;
  112. int height = source.Height;
  113. Bitmap image = new Bitmap(width, height);
  114. image.SetResolution(source.HorizontalResolution, source.VerticalResolution);
  115.  
  116. using (Graphics g = Graphics.FromImage(image))
  117. {
  118. g.Clear(Color.Transparent);
  119. g.DrawImage(
  120. source,
  121. new Rectangle(, , width, height),
  122. , , width, height,
  123. GraphicsUnit.Pixel,
  124. imageAttributes);
  125. }
  126. return image;
  127. }
  128.  
  129. public static Bitmap GetGrayscaleBitmap(Image source)
  130. {
  131. Bitmap grayscaleBitmap = new Bitmap(source.Width, source.Height, source.PixelFormat);
  132.  
  133. // Red should be converted to (R*.299)+(G*.587)+(B*.114)
  134. // Green should be converted to (R*.299)+(G*.587)+(B*.114)
  135. // Blue should be converted to (R*.299)+(G*.587)+(B*.114)
  136. // Alpha should stay the same.
  137. ColorMatrix grayscaleMatrix = new ColorMatrix(new float[][]{
  138. new float[] {0.299f, 0.299f, 0.299f, , },
  139. new float[] {0.587f, 0.587f, 0.587f, , },
  140. new float[] {0.114f, 0.114f, 0.114f, , },
  141. new float[] { , , , , },
  142. new float[] { , , , , }});
  143.  
  144. ImageAttributes attributes = new ImageAttributes();
  145. attributes.SetColorMatrix(grayscaleMatrix);
  146.  
  147. // Use a Graphics object from the new image
  148. using (Graphics graphics = Graphics.FromImage(grayscaleBitmap))
  149. {
  150. // Draw the original image using the ImageAttributes we created
  151. graphics.DrawImage(source,
  152. new Rectangle(, , grayscaleBitmap.Width, grayscaleBitmap.Height),
  153. , , grayscaleBitmap.Width, grayscaleBitmap.Height,
  154. GraphicsUnit.Pixel, attributes);
  155. }
  156.  
  157. return grayscaleBitmap;
  158. }
  159. }
  160. }

来自: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. ES6 模板字符串Template String

    1. 模板字符串简介: 顾名思义,模板字符串是用来定义一个模板是使用的,就像Vue,React中的template语法. 首先,先来了解一下template string的基本用法: 在ES5中,我们 ...

  2. C语言 进制转换

    这个程序仅仅是由十进制转换为其他进制的过程,其转换的规则如下图所示. 我使用的思路:首先在除基的过程中用一个数组保存余数,然后在输出进制转换结果的时候倒序输出,并且在输出前判断余数是否大于10,如果大 ...

  3. AGV

    AGV AGV是(Automated Guided Vehicle)的缩写,意即“自动导引运输车”,是指装备有电磁或光学等自动导引装置,它能够沿规定的导引路径行驶,具有安全保护以及各种移载功能的运输车 ...

  4. MySQL之UNION与UNION ALL

    数据表中的数据如下: UNION: 可以获取books表与articles表中所有不同的title,如果两个表中title相同的只会显示一个.  UNION ALL : 可以获取books表与arti ...

  5. 《xxx系统》质量属性战术

    <xxx系统>质量属性战术 可用性:重新引入 用户每填写一份表单,表单查看中即时更新所有信息. 易用性:系统主动 对于下拉框的选项较多时,用户可先进行部分输入,系统进行实时检索显示与用户输 ...

  6. pytorch查看模型weight与grad

    在用pdb debug的时候,有时候需要看一下特定layer的权重以及相应的梯度信息,如何查看呢? 1. 首先把你的模型打印出来,像这样 2. 然后观察到model下面有module的key,modu ...

  7. [ 随手记 2 ] C/C++ 数组/指针/传数组到函数/指针数组/数组指针

    1.=================================================================== 1,数组是一块内存连续的数据.2,指针是一个指向内存空间的变 ...

  8. string 迭代器

    #include <iostream>#include <string>#include<algorithm>#define m 10000000using nam ...

  9. 20155208徐子涵 《网络对抗技术》Web基础

    20155208徐子涵 <网络对抗技术>Web基础 实验要求 Web前端HTML Web前端javascipt Web后端:MySQL基础:正常安装.启动MySQL,建库.创建用户.修改密 ...

  10. 不应该使用String.valueOf的场景

    今天在接口中接收参数转换String时遇到一个巨大的坑,也是自己疏忽大意所致---- 事情是这样的,项目中接口的公共入参对象为Map<String,Object>,而sql中需要的参数为S ...