PictureForm.cs

2022-05-07 18:28:49 浏览数 (2)

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using Packet;

namespace TSProducer
{
    public partial class PictureForm : Form
    {
        private string gPicFilePath;             //全局变量,图片文件路径

        public string strPNG = "";

        public string FromPngValue
        {
            get
            {
                return this.textBoxPicture.Text;
            }
            set
            {
                this.textBoxPicture.Text = value;
            }
        }

        public event EventHandler accept;

        public PictureForm()
        {
            InitializeComponent();
        }
        public PictureForm(string filePath)
        {
            InitializeComponent();
            gPicFilePath = filePath;
            showPic();
            showPicContent();
            setFormTitle();
        }

        public void showPic()
        {
            Image img = Image.FromFile(gPicFilePath);
            pictureBox1.Image = img;
        }

        public void showPicContent()
        {
            try
            {
                byte[] pngBytes = ImgToBytes(gPicFilePath);
                strPNG = Convert.ToBase64String(pngBytes);
                textBoxPicture.Text = strPNG;

                /*************************************以十六进制显示图片内容**********************************************/
                //FileStream fs = new FileStream(gPicFilePath, FileMode.Open, FileAccess.Read);
                //BinaryReader br = new BinaryReader(fs);

                //string str = "rtrt";
                //for (uint i = 0x0; i <= 0xf; i  )
                //{

                //    str  = i.ToString("x")   "rt";
                //}
                //str  = "rn";

                //UInt64 memRowIndex = 00000000; //用来标示行号
                //str  = memRowIndex.ToString("X08")   "h:rt";

                //int count = 0;
                //int length = (int)fs.Length;

                //while (length > 0)
                //{
                //    count  ;
                //    byte tempByte = br.ReadByte();
                //    string tempStr = tempByte.ToString("X02");

                //    str  = tempStr.ToUpper()   "rt";
                //    length--;

                //    if (count == 16)
                //    {
                //        memRowIndex  = 0xf;
                //        str  = "rn"   memRowIndex.ToString("X08")   "h:rt";   //每行的行头标识
                //        count = 0;

                //    }
                //}
                //textBoxPicture.Text = str;
                //br.Close();
                //fs.Close();
                /***********************************************************************************/
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设置文件标题
        /// </summary>
        protected void setFormTitle()
        {
            FileInfo fileinfo = new FileInfo(gPicFilePath);
            this.Text = fileinfo.Name   " - Picture";
        }
        
        protected static byte[] ImgToBytes(string gPicFilePath)
        {
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                Bitmap bmap = new Bitmap(gPicFilePath);
                bmap.Save(ms, ImageFormat.Png);
                ms.Flush();
                byte[] pngBytes = ms.ToArray();
                return pngBytes;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }

        public string getPngStr()
        {
            try
            {
                byte[] pngBytes = ImgToBytes(gPicFilePath);
                strPNG = Convert.ToBase64String(pngBytes);
                return strPNG;
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 插入图片的信息到主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnClickInsertPicBits(object sender, EventArgs e)
        {
            if (accept != null)
            {
                accept(this, EventArgs.Empty);
            }
        }

        private void OnClickOpenPic(object sender, EventArgs e)
        {
            if (openPicFileDlg.ShowDialog() == DialogResult.OK)
            {
                gPicFilePath = openPicFileDlg.FileName;
                showPic();
                showPicContent();
                setFormTitle();
            }
        }
        /// <summary>
        /// 测试读取保存在package中的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnClickTest(object sender, EventArgs e)
        {
            if (openPicFileDlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    gPicFilePath = openPicFileDlg.FileName;
                    FileStream fs = new FileStream(gPicFilePath, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
                    string str = br.ReadString();
                    //str = str.Replace(CDT.CDTTableStr, "");
                    byte[] pngBytes = Convert.FromBase64String(str);
                    MemoryStream ImageMS = new MemoryStream();
                    ImageMS.Write(pngBytes, 0, pngBytes.Length);
                    Bitmap resultBitmap = new Bitmap(ImageMS);
                    pictureBox1.Image = resultBitmap;
                    textBoxPicture.Text = str;
                }
                catch (IOException ex)
                {
                    throw ex;
                }
            }
        }
    }
}
cs

0 人点赞