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;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM