C# 图片与字节数组(byte[])二进制相互转换,获取大小像素扩展名

 

private void button1_Click(object sender, EventArgs e)
        {
            string pPath = Application.StartupPath + @"\QQ.jpg";
            FileInfo fi = new FileInfo(pPath);
            byte[] Temp = imageToByteArray(pPath);
            Image img = byteArrayToImage(Temp);
            int W = img.Width;
            int H = img.Height;

            string Fm = fi.Extension;//扩展名
            long size = fi.Length; //
            string Fn = fi.Name;
            string info = "名称:" + Fn + "  分辨率:" + W + "*" + H;
            info += "  格式:" + Fm + "  大小:" + ((size > 1024) ? ((float)((float)size / 1024.0)).ToString("0.00") + "KB" : size + "B");
            this.label1.Text = info;
            this.pictureBox1.Height = H;
            this.pictureBox1.Width = W;
            this.pictureBox1.Image = img;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string pPath = Application.StartupPath + @"\QQ.jpg";
            byte[] Temp = imageToByteArray(pPath);
            StringBuilder Sb = new StringBuilder();
            for (int i = 0; i < Temp.Length; i++)
            {
                Sb.Append(Temp[i].ToString());
            }
            richTextBox1.Text = Sb.ToString();

        }
        /// <summary>
        /// 图片转为Byte字节数组
        /// </summary>
        /// <param name="FilePath">路径</param>
        /// <returns>字节数组</returns>
        private byte[] imageToByteArray(string FilePath)
        {
            using (MemoryStream ms = new MemoryStream())
            {

                using (Image imageIn = Image.FromFile(FilePath))
                {

                    using (Bitmap bmp = new Bitmap(imageIn))
                    {
                        bmp.Save(ms, imageIn.RawFormat);
                    }

                }
                return ms.ToArray();
            }
        }
        /// <summary>
        /// 字节数组生成图片
        /// </summary>
        /// <param name="Bytes">字节数组</param>
        /// <returns>图片</returns>
        private Image byteArrayToImage(byte[] Bytes)
        {
            using (MemoryStream ms = new MemoryStream(Bytes))
            {
                Image outputImg = Image.FromStream(ms);
                return outputImg;
            }
        }

阅读更多
个人分类: .NET And C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭