惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 雪夜

VS统计代码量 DataTable 操作 - 雪夜 - 博客园 统计销售额 bootstrap 知识点 人脸识别 参考 转盒子 年龄段统计 model 数据注解 delphi ScriptGate 调用JS C#区块链零基础入门,学习路线图 转 SQLServer 2005 和自增长主键identity说再见——NEWSEQUENTIALID() ASP.NET Core and .NET Core Library Support 创新县(市、区)的主要条件 C# imgage图片转base64字符/base64字符串转图片另存成 CSS - 雪夜 - 博客园 Exception has been thrown by the target of an invocation 网站报错 高效通用分页存储过程 多表查询 JavaScript 知识记录 JQuery 全选 取消 博文阅读密码验证 - 博客园
base64编码的 文件 图片
雪夜 · 2017-08-17 · via 博客园 - 雪夜
//图片 转为    base64编码的文本
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            dlg.Title = "选择要转换的图片";
            dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                for (int i = 0; i < dlg.FileNames.Length; i++)
                {
                    ImgToBase64String(dlg.FileNames[i].ToString());
                }
            }
        }
        //图片 转为    base64编码的文本
        private void ImgToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);
                this.pictureBox1.Image = bmp;
                FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                String strbaser64 = Convert.ToBase64String(arr);
                sw.Write(strbaser64);

                sw.Close();
                fs.Close();
               // MessageBox.Show("转换成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("ImgToBase64String 转换失败\nException:" + ex.Message);
            }
        }

        //base64编码的文本 转为    图片
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            dlg.Title = "选择要转换的base64编码的文本";
            dlg.Filter = "txt files|*.txt";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                for (int i = 0; i < dlg.FileNames.Length; i++)
                {
                    Base64StringToImage(dlg.FileNames[i].ToString());
                }
                
            }
        }
        //base64编码的文本 转为    图片
        private void Base64StringToImage(string txtFileName)
        {
            try
            {
                FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(ifs);

                String inputStr = sr.ReadToEnd();
                byte[] arr = Convert.FromBase64String(inputStr);
                MemoryStream ms = new MemoryStream(arr);
                Bitmap bmp = new Bitmap(ms);

                //bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                //bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);
                //bmp.Save(txtFileName + ".gif", ImageFormat.Gif);
                //bmp.Save(txtFileName + ".png", ImageFormat.Png);
                ms.Close();
                sr.Close();
                ifs.Close();
                this.pictureBox2.Image = bmp;
                if (File.Exists(txtFileName))
                {
                    File.Delete(txtFileName);
                }
                //MessageBox.Show("转换成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Base64StringToImage 转换失败\nException:" + ex.Message);
            }
        }

//
public class base64Test5 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpPostedFile hpf = context.Request.Files["fileName"];

            //将文件流写到byte数组中
            Stream stream = hpf.InputStream;
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            //将byte数组转为base64String
            string base64String = Convert.ToBase64String(bytes);
            //将base64String 转为 byte数组
            byte[] byteArray = Convert.FromBase64String(base64String);

            string saveFileName = DateTime.Now.ToFileTime().ToString();
            string path = HttpContext.Current.Server.MapPath("../base/" + saveFileName + ".png");
            //使用文件流读取byte数组中的数据
            Stream s = new FileStream(path, FileMode.Append);

            s.Write(byteArray, 0, byteArray.Length);

            s.Close();
            
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

//

Image fromImage = Image.FromFile( txtImg.Text );
MemoryStream stream = new MemoryStream();
fromImage.Save( stream, imgFormat[extension] );
txtString.Text = Convert.ToBase64String( stream.GetBuffer() );


MemoryStream stream = new MemoryStream( Convert.FromBase64String( txtString.Text ) ); 
bitmap img = new Bitmap( stream ); 
img.Save( txtImg.Text ); 
MessageBox.Show( "Completed!" ); 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Base64Demo
{
    public partial class Form1 : Form
    {
        private string base64Str;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //

            string path = textBox1.Text;  //界面上第一个文件路径
            string tempPath = textBox3.Text; //界面上第二个文件路径
            FileStream filestream = new FileStream(path, FileMode.Open);

            byte[] bt = new byte[filestream.Length];

            //调用read读取方法
            filestream.Read(bt, 0, bt.Length);
            this.base64Str = Convert.ToBase64String(bt);
            filestream.Close();

            //将Base64串写入临时文本文件
            if (File.Exists(tempPath))
            {
                File.Delete(tempPath);
            }
            FileStream fs = new FileStream(tempPath, FileMode.Create);
            byte[] data = System.Text.Encoding.Default.GetBytes(this.base64Str);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string outPath = textBox2.Text;  //界面上第三个文件路径
            var contents = Convert.FromBase64String(this.base64Str);
            using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
            {
                fs.Write(contents, 0, contents.Length);
                fs.Flush();
            }
        }
    }
}