using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Threading; namespace Utility
{
/// <summary>
/// 绘制一张图片,图片内容为标题+表格数据
/// </summary>
public class CSharpDraw
{
public int _pageSize = ; //每张图片显示多少条数据
public int _picWidth = ; //图片宽度
public int _picHeight = ; //图片高度
public int _tableStartX = ; //画笔起始点相对画布的水平位置
public int _tableStartY = ; //画笔起始点相对画布的垂直位置
public string _fontStyle = "宋体"; //默认字体
public int _fontSize = ; //默认字体大小
public string _title = "公告"; //表格标题
public int _rowHeight = ; //表格行高(假设每行高度一样)
public int _columnWidth = ; //表格列宽(假设每列宽度一样)
public int _rowDataSartX = ; //表格数据相对表格的水平位置
public int _rowDataSartY = ; //表格数据相对表格的垂直位置 private void CreateTablePicture(List<Student> dataList, int startRowNo, string picSavePath)
{
int nEnd = (startRowNo + _pageSize > dataList.Count) ? dataList.Count : (startRowNo + _pageSize); //新建一个默认大小的图片
Bitmap bmp = new Bitmap(_picWidth, _picHeight);
//利用该图片对象生成画板
Graphics graphic = Graphics.FromImage(bmp);
//设置黑色背景
graphic.Clear(Color.Black); //画刷用来绘制表格线条,画笔用来绘制文字内容
//新建一个画刷
SolidBrush brush = new SolidBrush(Color.Red);
//定义一个红色、线条宽度为1的画笔
Pen pen = new Pen(Color.Red, );
//设置内容字体
Font font = new Font(_fontStyle, _fontSize); //绘制表格标题
graphic.DrawString(_title, font, brush, _tableStartX, _tableStartY); int row = ;
string studentName = string.Empty; //画表格并添加显示文字内容
for (int i = startRowNo; i < nEnd; i++)
{
//当前绘制行号
row = i - startRowNo; //绘制表格第一列,在画板上画矩形
_tableStartX = ;
_tableStartY = _rowDataSartX + _rowHeight * row;
graphic.DrawRectangle(pen, _tableStartX, _tableStartY, _columnWidth, _rowHeight);
//填充表格内容(第一列)
_tableStartX = ;
_tableStartY = _rowDataSartX + _rowHeight * row;
studentName = dataList[i].StudentName.Length < ? dataList[i].StudentName : (dataList[i].StudentName.Substring(, ) + "..");
graphic.DrawString(studentName, font, brush, _tableStartX, _tableStartY); //绘制第二列,在画板上画矩形
_tableStartX += _columnWidth;
graphic.DrawRectangle(pen, _tableStartX, _tableStartY, _columnWidth, _rowHeight);
//填充表格内容(第二列)
_tableStartX += _columnWidth;
graphic.DrawString(dataList[i].Count.ToString(), font, brush, _tableStartX, _tableStartY);
} //释放资源
graphic.Dispose();
//注意:程序要有该目录下该文件的访问权限
bmp.Save(picSavePath, ImageFormat.Bmp);
} public List<string> ManagePictureGenerate(List<Student> dataList, string picPath)
{
List<string> templist = new List<string>(); string strPicPath = string.Empty;
int page = (int)Math.Ceiling((double)dataList.Count / _perPageCount);
for (int i = ; i < page; i++)
{
strPicPath = string.Format("{0}\\{1}{2}.bmp", picPath, DateTime.Now.ToString("HHmmss"), i);//采用公共的绘图方法
SaveDrawPicForPosition(dataList, _perPageCount * i, strPicPath, picType); tempList.Add(strPicPath);
} return lst;
} public bool PingIp()
{
Ping pingSender = new Ping();
PingReply reply = pingSender.Send("127.0.0.1", );//第一个参数为ip地址,第二个参数为ping的时间
if (reply.Status == IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
}
}
using System.IO;
using System.Windows.Media.Imaging; namespace Utility
{
public class ImageHelper
{
public static BitmapImage StreamBitmapImage(Stream stream)
{
BitmapImage bmp = null;
try
{
bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = stream;
bmp.EndInit();
}
catch
{
bmp = null;
}
return bmp;
} /// <summary>
/// byte[]转换为BitmapImage
/// </summary>
/// <param name="byteArray"></param>
/// <returns></returns>
public static BitmapImage ByteArrayToBitmapImage(byte[] byteArray)
{
BitmapImage bmp = null;
try
{
bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(byteArray);
bmp.EndInit();
}
catch
{
bmp = null;
}
return bmp;
} /// <summary>
/// BitmapImage转换为byte[]
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static byte[] BitmapImageToByteArray(BitmapImage bmp)
{
byte[] byteArray = null;
try
{
Stream sMarket = bmp.StreamSource;
if (sMarket != null && sMarket.Length > )
{
//很重要,因为Position经常位于Stream的末尾,导致下面读取到的长度为0。
sMarket.Position = ; using (BinaryReader br = new BinaryReader(sMarket))
{
byteArray = br.ReadBytes((int)sMarket.Length);
}
}
}
catch
{
//other exception handling
}
return byteArray;
}
}
}

C# 生成 bmp 格式的图片的更多相关文章

  1. [自制操作系统] BMP格式文件读取&图形界面系统框架/应用接口设计

    本文将介绍在本人JOS中实现的简单图形界面应用程序接口,应用程序启动器,以及一些利用了图形界面的示例应用程序. 本文主要涉及以下部分: 内核/用户RW/RW调色板framebuffer共享区域 8bi ...

  2. 将jpg压缩成webp格式的图片

    cwebp名称 cwebp -压缩图像文件为的WebP文件概要 cwebp [选项] INPUT_FILE -o output_file.webp描述 cwebp压缩使用的WebP格式的图像.输入格式 ...

  3. VS2015上OpenCV-2.4.13安装与Hi35xx .jpg/.bmp格式转.bgr格式开发

    因为Hi3559AV100后期深度学习开发需要用到.bgr格式的图片,而目前在手的一般为.jpg或.bmp格式的图片,下面随笔将给出基于OpenCV-2.4.13的格式转换,实现Hi35xx .jpg ...

  4. 图片bmp格式转换为jpg格式

    一下代码经过个人测试,可用 注意:将jpg格式的图片重命名为bmp格式,在该代码中是不能转换的,会报空值异常!而且IE10是显示不了这样的图片的 import java.awt.Image; impo ...

  5. c#图片生成png格式和原图不同

    下面这种,会生成和原图类似的图片,png格式的图片该是空的地方仍旧是空的

  6. Bmp格式图片与16进制的互相转换简解 Python

    BMP TO HEX 首先介绍Github上一个简单的Bmp转成16进制的py: https://github.com/robertgallup/bmp2hex 网上这种例子很多.思路也简单:将bmp ...

  7. C# 生成 DataMatrix 格式的二维码

    该文主要是利用OnBarcode.dll 生成 DataMatrix 格式的二维码的一些简单方法和操作技巧.关于QrBarcode的二维码比较常见和简单,网上有很多资源. 1.附件为dll 2.利用上 ...

  8. 一般源码安装添加的GD库 是不支持 jpeg 格式的图片的

    一般源码安装添加的GD库 是不支持 jpeg 格式的图片的,只支持如下格式 GD Support enabled GD Version bundled (2.0.34 compatible) GIF ...

  9. 你所能用到的BMP格式介绍

    原理篇: 一.编码的意义. 让我们从一个简单的问题开始,-2&-255(中间的操作符表示and的意思)的结果是多少,这个很简单的问题,但是能够写出解答过程的人并不 多.这个看起来和图片格式没有 ...

随机推荐

  1. 【中文排序】mysql order by 中文排序

    1. 在MySQL中,我们经常会对一个字段进行排序查询,但进行中文排序和查找的时候,对汉字的排序和查找结果往往都是错误的. 这种情况在MySQL的很多版本中都存在. 如果这个问题不解决,那么MySQL ...

  2. android中webview的实现

    设置从当前页面打开链接,而不是跳转到系统默认浏览器打开: webview.setWebViewClient(new WebViewClient(){ @Override public boolean ...

  3. python 时间四舍五入

    假设时间格式为 YYYYMMDDhhmm , 比如201508010001 代表2015年8月1日0点01分. 现在有需求,要求一个start 和一个 end 变量的字符串 都是这种格式的时间. 需要 ...

  4. Python学习系列之异常处理

    什么是异常处理 python内置了一套try···except···finally的错误处理机制 当程序出错的时候进行捕捉,然后根据捕捉到的错误信息进行响相应的处理 常用的内建异常 初识异常处理 如例 ...

  5. Centos samba install

    Ready Change Root Password passwd root 在提示下建立新密码 静态IP vi /etc/sysconfig/network-scripts/ifcfg-eth0  ...

  6. STL非变易算法

    非变易算法:原则上不会变更操作数据的算法. [1]    for_each:逐个容器元素,原型for_each(InputIter first, InputIter last, Function f) ...

  7. WindowFromPoint -- 获得包括指定点的窗体的句柄

     WindowFromPoint 函数功能: 该函数获得包括指定点的窗体的句柄. 函数原型: HWND WindowFromPoint(POINT Point): 參数: Point:指定一个被检 ...

  8. SpringMVC学习指南-前言

    SpringMVC是Spring框架中用于Web应用快速开发的一个模块. SpringMVC基于Spring框架.Servlet和JSP. ------------------------------ ...

  9. linux启动基本流程

    linux启动序列 1.CPU初始化    CPU自身初始化.从某个固定位置(0xfffffff0)取指令并运行,该指令为跳转指令.跳转到BIOS代码的首部. 2.装载BIOS    BIOS被固化在 ...

  10. iOS开发——基础篇——assign,copy,retain之间的区别以及weak和strong的区别

    @property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...