protected string ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename); 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();
return Convert.ToBase64String(arr);
}
catch (Exception ex)
{
return null;
}
} //threeebase64编码的字符串转为图片
protected Bitmap Base64StringToImage(string base64Code)
{
try
{
string sPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
if (sPath.Substring(sPath.Length - 1, 1) != "\\")
{
sPath += "\\";
}
if (!Directory.Exists(sPath + "Temp"))
{
Directory.CreateDirectory(sPath + "Temp");
} string sFileName = sPath + "Temp\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff"); byte[] arr = Convert.FromBase64String(base64Code);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms); string f1 = sFileName + ".jpg";
string f2 = sFileName + ".bmp";
string f3 = sFileName + ".gif";
string f4 = sFileName + ".png"; bmp.Save(sFileName + ".jpg", ImageFormat.Jpeg);
bmp.Save(sFileName + ".bmp", ImageFormat.Bmp);
bmp.Save(sFileName + ".gif", ImageFormat.Gif);
bmp.Save(sFileName + ".png", ImageFormat.Png);
ms.Close(); #region 读取本地文件 FileStream fs = new FileStream(f1, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close(); Copy(photo); #endregion return bmp;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return null;
}
} protected Bitmap Copy(byte[] arr)
{
try
{
string sPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
if (sPath.Substring(sPath.Length - 1, 1) != "\\")
{
sPath += "\\";
}
if (!Directory.Exists(sPath + "Temp\\Temp"))
{
Directory.CreateDirectory(sPath + "Temp\\Temp");
} string sFileName = sPath + "Temp\\Temp\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff"); MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms); string f1 = sFileName + ".jpg";
string f2 = sFileName + ".bmp";
string f3 = sFileName + ".gif";
string f4 = sFileName + ".png"; bmp.Save(sFileName + ".jpg", ImageFormat.Jpeg);
bmp.Save(sFileName + ".bmp", ImageFormat.Bmp);
bmp.Save(sFileName + ".gif", ImageFormat.Gif);
bmp.Save(sFileName + ".png", ImageFormat.Png);
ms.Close(); return bmp;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return null;
}
}

  

C# base64编码的字符串与图片互转的更多相关文章

  1. base64编码的字符串与图片相互转换

    #region 图片转为base64编码的字符串---ImgToBase64String /// <summary> /// 图片转为base64编码的字符串 /// </summa ...

  2. C# base64编码的文本与图片互转

    /// <summary> /// base64编码的文本转为图片 /// </summary> /// <param name="txtFilePath&qu ...

  3. 将图片转换为Base64编码的字符串

    图片以文件的形式存在,可以在表单中使用. 也可以转换成Base64编码的字符串,从而在css.js中以字符串的形式使用图片.例如,在css中设置背景图片.在js中用ajax上传图片. <!DOC ...

  4. C# 在网页中将Base64编码的字符串显示成图片

    在写一个接口,返回的json里面有图片,是Base64编码的字符串. 测试接口的时候,发现原来在html显示,是直接可以将Base64编码的字符串显示成图片的. 格式如下: <img src=d ...

  5. 十六进制字符串jpg图片互转

    十六制字符串jpg图片互转(格式:FFD8FFE000104A******)如:FFD8FFE000104A46494600010100000100010000FFDB0043000806060706 ...

  6. base64编码的字符串(含有中文) 前端解码

    base64编码的字符串(含有中文) 前端解码 https://xue5602.github.io/2018/12/19/atob%E8%A7%A3%E7%A0%81utf-8%E5%AD%97%E7 ...

  7. Base64和本地以及在线图片互转

    package com.ruoyi.common.utils; import java.io.ByteArrayOutputStream; import java.io.FileInputStream ...

  8. Base64编码和其在图片的传输的应用

    Base64 [原文链接] 目前Base64已经成为网络上常见的传输8Bit字节代码的编码方式之一.做支付系统时,系统之间的报文交互都需要使用Base64对明文进行转码,然后再进行签名或加密,之后再进 ...

  9. js 显示 base64编码 的二进制流 图片

    Data URI scheme.Data URI scheme是在RFC2397中定义的,目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入.比如上面那串字符,其实是一张小图片,将这些字 ...

随机推荐

  1. 使用 ftrace 调试 Linux 内核,第 1 部分【转】

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-ftrace1/index.html ftrace 是 Linux 内核中提供的一种调试工具.使用 ...

  2. linux-open-source-development-tools【重点】

    https://www.pluralsight.com/blog/software-development/linux-open-source-development-tools https://ww ...

  3. C核心 那些个关键字

    概述 - C语言老了 目前而言(2017年5月12日) C语言中有 32 + 5 + 7 = 44 个关键字. 具体如下 O(∩_∩)O哈哈~ -> C89关键字 char short int ...

  4. linux磁盘占用跟每个文件夹大小总和不符

    1.一种情况是删除了大文件但是没有释放出来,因为有进程还在调用使用 最简单的方法是reboot下服务器再对比下: 2.查看服务器空间使用情况 df -h cd / du -sh *

  5. Centos7 环境准备

    Centos7 环境准备 #关闭防火墙 systemctl stop firewalld systemctl disable firewalld #关闭selinux sed -i 's/SELINU ...

  6. mysql安装依赖perl(Data::Dumper)

    http://blog.itpub.net/29989552/viewspace-2128991/

  7. Openstack 云主机深入了解 (十六)

    一)云主机深入了解 1.云主机在计算节点以进程方式运行 2.监听vnc的端口,vnc默认端口从5900开始, 多台云主机,端口递增 3.云主机桥接网卡,与宿主机联通网络 提示:在openstack环境 ...

  8. OpenStack 镜像服务 Glance部署(七)

    创建虚拟机我们需要有glance的支持,因为glance是提供镜像的服务. Glance有两个比较重要的服务: Glance-api:接受云系统镜像的构建.删除.读取请求 Glance-Registr ...

  9. cookie使用和销毁

    一.cookie导读,理解什么是cookie 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4kb左右)存储到客户端的硬盘或内存.并且读可以取出来的一种技术. 2.当你浏览某网 ...

  10. Ghostscript 中 ps2pdf 命令在 windows msys 下的运行错误问题。

    前两天看到了 miloyip/game-programmer 这个项目觉得特别有用,真是好东西,明确了指出了学习路线,尤其是新手.不过打开看,有些书对应的亚马逊链接是无效的,比如<Tricks ...