zoukankan      html  css  js  c++  java
  • PictureForm.cs

    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;
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    linux常用命令整理
    总结五大常用算法!
    python数组(列表、元组及字典)
    网络编程---笔记1
    python3与python2的区别 记录一波
    Python学习week3-python数据结构介绍与列表
    Python学习week2-python介绍与pyenv安装
    Python学习week1-linux文件系统与IO重定向
    Python学习week1-计算机基础
    css3文本多行省略
  • 原文地址:https://www.cnblogs.com/jingmoxukong/p/2120872.html
Copyright © 2011-2022 走看看