//图片转为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字符串转图片另存成的更多相关文章

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

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

  2. C# base64编码的字符串与图片互转

    protected string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = new Bitmap(Imagefilena ...

  3. 在图片上加字符-base64转图片-图片转base64

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Base64字符 转图片乱码问题

    网站做了个随机验证码图片功能,遇到了一个奇怪的问题——Base64字符集转图片乱码问题,问题描述如下 1.用java画笔将随机验证码绘制成图片 2.再将图片的二进制代码转换成Base64字符集,返回给 ...

  5. Base64 字符串转图片 问题整理汇总

    前言 最近碰到了一些base64字符串转图片的开发任务,开始觉得没啥难度,但随着开发的进展还是发现有些东西需要记录下. Base64 转二进制 这个在net有现有方法调用: Convert.FromB ...

  6. C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决

    最近用base64编码传图片遇到了点问题,总结下. 首先总结下base64编码的逻辑,来自网络:https://www.cnblogs.com/zhangchengye/p/5432276.html ...

  7. FromBase64String 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符

    js前台: <input id="upload_img_input" v-on:change="onFileChange" type="file ...

  8. Base-64 字符数组或字符串的长度无效等问题解决方案

    项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件…… ...

  9. Base64 报错 的解决办法 (Base-64 字符数组或字符串的长度无效。, 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符。)

    Base64 报错 的解决办法, 报错如下:1. FormatException: The input is not a valid Base-64 string as it contains a n ...

随机推荐

  1. 字符串 (string)与字节数组(byte[])之间的转换

    string str = "abc" //字符串转成编码为GB2312的byte[] byte[] pData =System.Text.Encoding.GetEncoding( ...

  2. python多线程的两种写法

    1.一般多线程 import threading def func(arg): # 获取当前执行该函数的线程的对象 t = threading.current_thread() # 根据当前线程对象获 ...

  3. Flask(2)- 装饰器的坑及解决办法、flask中的路由/实例化配置/对象配置/蓝图/特殊装饰器(中间件、重定义错误页面)

    一.装饰器的坑以及解决方法 1.使用装饰器装饰两个视图函数,代码如下 from flask import Flask, redirect, render_template, request, sess ...

  4. Django基础——模板层(template) (Day67)

    阅读目录 变量 标签 自定义过滤器和标签 模板层(template) 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. 1 2 3 4 ...

  5. knockout 学习使用笔记------绑定值时赋值失败

    在使用knockout绑定值的时候,发现无论怎么赋值都赋值失败,最后检查前端页面才发现,同一个属性绑定值的时候,绑定了两次,而在js中进行属性绑定的时候是双向绑定的,SO,产生了交互影响.谨记之. 并 ...

  6. And Design:拓荒笔记——Upload上传

    And Design:拓荒笔记——Upload上传 上传前

  7. 获取配置文件信息——configparser

    配置文件host.int格式如下: [host]product=xxxxxxxxxxtest=xxxxxxxxxx python 3.x代码如下: import os,configparser def ...

  8. $ 专治各种python字符编码问题疑难杂症

    标准动作 在脚本第一行指定编码格式: # coding:utf-8 将默认的ascii字符流处理方式变为utf-8: import sys sys.getdefaultencoding() 'asci ...

  9. 002. MySQL复制操作

    #### 1.Setting the Replication Master Configuration On a replication master, you must enable binary  ...

  10. contain与compareDocumentPosition

    contain方法由IE创建,用于判断元素之间是否是父亲与后代的关系,例如:如果A元素包含B元素,则返回true,否则,返回false eg: <div id= "a"> ...