C# imgage图片转base64字符/base64字符串转图片另存成
//图片转为base64编码的字符串
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 strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
bmp.Save(@"d:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(@"d:\"test.bmp", ImageFormat.Bmp);
//bmp.Save(@"d:\"test.gif", ImageFormat.Gif);
//bmp.Save(@"d:\"test.png", ImageFormat.Png);
ms.Close();
return bmp;
}
catch (Exception ex)
{
return null;
}
}
//图片转为base64编码的字符串
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 strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
bmp.Save(@"d:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(@"d:\"test.bmp", ImageFormat.Bmp);
//bmp.Save(@"d:\"test.gif", ImageFormat.Gif);
//bmp.Save(@"d:\"test.png", ImageFormat.Png);
ms.Close();
return bmp;
}
catch (Exception ex)
{
return null;
}
}
//2: 图片直接显示
string Base64String = "";//太长就不贴出来了
byte[] bytes = Convert.FromBase64String(Base64String);
ImageTagId.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(bytes);//
//
public System.Drawing.Image Base64ToImage(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
return image;
}
使用方法:
Base64ToImage(dd).Save(Server.MapPath("Hello.jpg"));
Image1.ImageUrl = "Hello.jpg";;
//4:转换成图片另存为然后图片显示出来
string dd = "";//太长就不贴出来了
var bytes = Convert.FromBase64String(dd);
using (var imageFile = new FileStream(@"d:\Hello1.jpg", FileMode.Create))
{
imageFile.Write(bytes, 0, bytes.Length);
imageFile.Flush();
}
//5://直接转换存为路径下图片
string base64Str=""/
e[] bytes = System.Convert.FromBase64String(dd);
File.WriteAllBytes(@"d:\newfile.jpg", bytes);
string filePath = @"d:\MyImage.jpg";
File.WriteAllBytes(filePath, Convert.FromBase64String(base64Str));
6:多张base64图片转换为图片另存为
public string Base64ToImage(string upimgPath, string base64String)
{
string goodspath = Server.MapPath(upimgPath); //用来生成文件夹
if (!Directory.Exists(goodspath))
{
Directory.CreateDirectory(goodspath);
}
var imgPath = string.Empty;
if (!string.IsNullOrEmpty(base64String))
{
var splitBase = base64String.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in splitBase)
{
var path = upimgPath + Guid.NewGuid() + ".jpg";
string filePath = Server.MapPath(path);// Server.MapPath(upimgPath + Guid.NewGuid() + ".jpg");
File.WriteAllBytes(filePath, Convert.FromBase64String(item));
imgPath += path + ";";
}
}
else { imgPath = ";"; }
return imgPath.TrimEnd(';');
}
使用方法:
va headimg="";
Base64ToImage("UpLoadImg/HeadImage/", headimg)
C# imgage图片转base64字符/base64字符串转图片另存成的更多相关文章
- base64编码的字符串与图片相互转换
#region 图片转为base64编码的字符串---ImgToBase64String /// <summary> /// 图片转为base64编码的字符串 /// </summa ...
- C# base64编码的字符串与图片互转
protected string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = new Bitmap(Imagefilena ...
- 在图片上加字符-base64转图片-图片转base64
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Base64字符 转图片乱码问题
网站做了个随机验证码图片功能,遇到了一个奇怪的问题——Base64字符集转图片乱码问题,问题描述如下 1.用java画笔将随机验证码绘制成图片 2.再将图片的二进制代码转换成Base64字符集,返回给 ...
- Base64 字符串转图片 问题整理汇总
前言 最近碰到了一些base64字符串转图片的开发任务,开始觉得没啥难度,但随着开发的进展还是发现有些东西需要记录下. Base64 转二进制 这个在net有现有方法调用: Convert.FromB ...
- C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决
最近用base64编码传图片遇到了点问题,总结下. 首先总结下base64编码的逻辑,来自网络:https://www.cnblogs.com/zhangchengye/p/5432276.html ...
- FromBase64String 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符
js前台: <input id="upload_img_input" v-on:change="onFileChange" type="file ...
- Base-64 字符数组或字符串的长度无效等问题解决方案
项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件…… ...
- Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)
Base64 报错 的解决办法, 报错如下:1. FormatException: The input is not a valid Base-64 string as it contains a n ...
随机推荐
- DOM 综合练习(一)
// 练习一: 完成一个好友列表的展开闭合效果 <html> <head> <style type="text/css"> // 对表格中的 u ...
- centos7 docker镜像加速器配置
CentOS的配置方式略微复杂,需要先将默认的配置文件复制出来 /lib/systemd/system/docker.service -> /etc/systemd/system/docker. ...
- mongo 一次插入多条
db.getCollection('organization').insert( [ {"organizationTitle" : "台州广播电台1", &qu ...
- golang redis的模式订阅
c := redisPool.Get() psc := redis.PubSubConn{c} psc.PSubscribe("aa*") for { switch v := ps ...
- 在PC上调试微信手机页面的三种方法
场景 假设一个手机页面,开发者对其做了限制,导致只能在微信客户端中打开.而众所周知手机上非常不利于调试页面,所以需要能在电脑上打开并进行调试.这里针对常见的三种页面做一下分析,一一绕过其限制,(当然不 ...
- boost智能指针总结
智能指针是一种具备指针类似行为的对象,当不在需要它的时候自动删除其引用的c++对象.直接点说就是自动析构C++对象. boost提供了6种智能指针,如下所示: scoped_ptr <boost ...
- Python(装饰器)
eval 内容从字符串中提取出来,用Python运行一遍__name__得到函数名字 闭包函数 定义: 函数内部定义的函数称为内部函数 该内部函数包含对外部作用域,而不是对全局作用域的名字的引用 那么 ...
- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类
<bean id="investorQueryConfigurer" class="org.springframework.beans.factory.config ...
- 三道半平面交测模板题 Poj1474 Poj 3335 Poj 3130
求半平面交的算法是zzy大神的排序增量法. ///Poj 1474 #include <cmath> #include <algorithm> #include <cst ...
- cdoj1344卿学姐种美丽的花
地址:http://acm.uestc.edu.cn/#/problem/show/1344 题目: 卿学姐种美丽的花 Time Limit: 8000/4000MS (Java/Others) ...