• C# 生成 bmp 格式的图片


    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 = 5;    //每张图片显示多少条数据
            public int _picWidth = 100;  //图片宽度
            public int _picHeight = 100; //图片高度
            public int _tableStartX = 10; //画笔起始点相对画布的水平位置
            public int _tableStartY = 10; //画笔起始点相对画布的垂直位置
            public string _fontStyle = "宋体"; //默认字体
            public int _fontSize = 10; //默认字体大小
            public string _title = "公告"; //表格标题
            public int _rowHeight = 20; //表格行高(假设每行高度一样)
            public int _columnWidth = 30; //表格列宽(假设每列宽度一样)
            public int _rowDataSartX = 3; //表格数据相对表格的水平位置
            public int _rowDataSartY = 3; //表格数据相对表格的垂直位置
    
            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, 1);            
                //设置内容字体
                Font font = new Font(_fontStyle, _fontSize);
    
                //绘制表格标题
                graphic.DrawString(_title, font, brush, _tableStartX, _tableStartY);
    
                int row = 0;
                string studentName = string.Empty;
                
                //画表格并添加显示文字内容
                for (int i = startRowNo; i < nEnd; i++)
                {
                    //当前绘制行号
                    row = i - startRowNo;
    
                    //绘制表格第一列,在画板上画矩形
                    _tableStartX = 10;
                    _tableStartY = _rowDataSartX + _rowHeight * row;
                    graphic.DrawRectangle(pen, _tableStartX, _tableStartY, _columnWidth, _rowHeight);
                    //填充表格内容(第一列)
                    _tableStartX = 10;
                    _tableStartY = _rowDataSartX + _rowHeight * row;
                    studentName = dataList[i].StudentName.Length < 5 ? dataList[i].StudentName : (dataList[i].StudentName.Substring(0, 4) + "..");
                    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 = 0; 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", 120);//第一个参数为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 > 0)
                    {
                        //很重要,因为Position经常位于Stream的末尾,导致下面读取到的长度为0。 
                        sMarket.Position = 0;
    
                        using (BinaryReader br = new BinaryReader(sMarket))
                        {
                            byteArray = br.ReadBytes((int)sMarket.Length);
                        }
                    }
                }
                catch
                {
                    //other exception handling 
                }
                return byteArray;
            }
        }
    }
  • 相关阅读:
    关于前端输入框的限制和有效值
    js,jquery转json的几种方法
    java,js,jstl,EL的简单交互
    mysql字段冲突报错
    js的一些压缩和优化性能
    一个不错的html素材网站
    redis之数据操作详解
    redis之持久化操作
    redis之django-redis
    redis知识总汇
  • 原文地址:https://www.cnblogs.com/hellowzl/p/9072211.html
Copyright © 2020-2023  润新知