首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PictureForm.cs

PictureForm.cs

作者头像
静默虚空
发布2022-05-07 18:28:49
1590
发布2022-05-07 18:28:49
举报
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 = "\r\t\r\t";
                //for (uint i = 0x0; i <= 0xf; i++)
                //{

                //    str += i.ToString("x") + "\r\t";
                //}
                //str += "\r\n";

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

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

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

                //    str += tempStr.ToUpper() + "\r\t";
                //    length--;

                //    if (count == 16)
                //    {
                //        memRowIndex += 0xf;
                //        str += "\r\n" + memRowIndex.ToString("X08") + "h:\r\t";   //每行的行头标识
                //        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;
                }
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-07-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档