C#中图片与BASE64码互相转换
//保存目录
string dir = "/upload/user/head";
//站点文件目录
string fileDir = HttpContext.Current.Server.MapPath("~" + dir);
//文件名称
string fileName = "headdemo" + DateTime.Now.ToString("yyyyMMddHHmmssff");
//保存文件所在站点位置
string filePath = Path.Combine(fileDir, fileName); if (!System.IO.Directory.Exists(fileDir))
System.IO.Directory.CreateDirectory(fileDir); //读图片转为Base64String
System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(Path.Combine(fileDir, "default.jpg"));
using (MemoryStream ms1 = new MemoryStream())
{
bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr1 = new byte[ms1.Length];
ms1.Position = ;
ms1.Read(arr1, , (int)ms1.Length);
ms1.Close();
UserPhoto = Convert.ToBase64String(arr1);
} //将Base64String转为图片并保存
byte[] arr2 = Convert.FromBase64String(UserPhoto);
using (MemoryStream ms2 = new MemoryStream(arr2))
{
System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
bmp2.Save(filePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp2.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
//bmp2.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
//bmp2.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
//将Base64String转为图片并保存
byte[] arr2 = Convert.FromBase64String(UserPhoto);
using (MemoryStream ms2 = new MemoryStream(arr2))
{
System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
////只有把当前的图像复制一份,然后把旧的Dispose掉,那个文件就不被锁住了,
////这样就可以放心覆盖原始文件,否则GDI+一般性错误(A generic error occurred in GDI+)
//System.Drawing.Bitmap bmpNew = new System.Drawing.Bitmap(bmp2);
//bmp2.Dispose();
//bmp2 = null;
bmp2.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp2.Save(filePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp2.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
//bmp2.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
//bmp2.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
bmp2.Dispose();
}
GDI+一般性错误(A generic error occurred in GDI+)
1.GDI+的前世今生
GDI+全称图形设备接口,Graphics Device Interface (GDI) ,他的爸爸叫做GDI, 用C写的。Windows XP出来以后用C++重新写了一下,变成了GDI+。从.NET Framework 1.0开始,GDI+就被正式封装在了.NET Framework里面,并被广泛地应用到了所有和图形图像相关的程序中。不幸的是,这个GDI+引入了微软有史以来最大的2个patch,造成了Microsoft IT, Support, Developer, Tester的无数麻烦。[1][2]
GDI+没有用显卡加速,所以Windows Vista推荐用Windows Display Driver Model (WDDM)了,支持渲染,3D加速。不过普通的应用程序,用GDI/GDI+其实是完全足够了,所以GDI+是在微软平台上开发图形图像程序的最好选择了。至少现在没有听说微软准备重新写GDI。
GDI+ 可以用来做图形处理,也可以做图像处理。这里只分析几个使用.NET Framework容易出错的地方。
2. GDI+一般性错误(A generic error occurred in GDI+)
这是使用GDI+的时候最滑稽的一个Exception,里面啥信息都没有。对于刚刚开始使用.NET Framework开发者来说,很难发现这个问题到底是为什么。
我们先来看看下面一段代码
Bitmap bmp = new Bitmap(fileName);
bmp.Save(fileName, ImageFormat.Jpeg);
这段代码的目的是要打开一个Bitmap,然后保存。可惜这段代码一定会给你一个GDI+一般性错误:
其中的Error Code是0x80004005, innerException是空。如果你查Windows的Error Code表,会发现这个错误原因是“Unspecified Error”,还是什么都不知道。这其实是.NET Framework封装不好的问题,我们可以调用
拿到Win32的Error, 32。这个错误代码就有点信息量了,在winerror.h里面,我们可以找到下面的定义:

//
// MessageId: ERROR_SHARING_VIOLATION
//
// MessageText:
//
// The process cannot access the file because it is being used by another process.
//
#define ERROR_SHARING_VIOLATION 32L

原来是文件不能写。其实MSDN里面有一句话,The file remains locked until the Bitmap is disposed。所以文件读取以后是锁着的,没有办法写。那如果我想做点改动然后再保存原来的文件怎么办呢?
这里有个土办法可以搞定这个问题
Bitmap bmp = new Bitmap(bmpTemp);
bmpTemp.Dispose();
bmp.Save(image, ImageFormat.Jpeg);
只要把当前的图像复制一份,然后把旧的Dispose掉,那个文件就不被锁住了,这样就可以放心覆盖原始文件了。
想想如果你要用GDI+写一个Painter,很容易你就会遇到这个问题。
C#中图片与BASE64码互相转换的更多相关文章
- JavaScript 图片与Base64数据互相转换脚本
JavaScript 图片与Base64数据互相转换脚本 注: 转换过程中注意跨域问题.测试页是否支持相关标签创建.dom结构. 方法一:非Html 5使用FileReader 使用XMLHttpRe ...
- oracle BLOG图片和CLOG base64码的转换
--BASE64转图片CREATE OR REPLACE FUNCTION DECODE_BASE64(P_CLOB_IN IN CLOB) RETURN BLOB IS V_BLOB BLOB; V ...
- [转]C#中图片.BYTE[]和base64string的转换
本文转自:http://blog.csdn.net/thebesttome/article/details/6870155 在C#中 图片到byte[]再到base64string的转换: Bitma ...
- C#中图片.BYTE[]和base64string的转换
在C#中 图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath); MemoryStream ms = ...
- JavaScript—图片与base64编码互相转换
图片转换为base64编码 <input type = "file" id = "file" onchange="popFileName(thi ...
- c# 图片 与 BASE64 字符串 互相转换。
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...
- C#中图片转换为Base64编码,Base64编码转换为图片
#region 图片转为base64编码的字符串 public string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = ...
- java中图片地址base64编码的相互转换
public class Base64Url { /** * 将base64编码字符串转换为图片 * @param imgStr: base64编码字符串 * @param path: 图片路径-具体 ...
- 图片和Base64之间的转换
public static Bitmap GetImageFromBase64String(string strBase) { try { MemoryStream stream = new Memo ...
随机推荐
- CryptoJS_Base64
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- xcode8+iOS10问题
.xcode升级到8.0后打印的问题 ()xcode8会打印一些莫名其妙的log 解决方法:Scheme里面添加OS_ACTIVITY_MODE = disable ()xcode8打印log不完整 ...
- 1.[WP Developer体验Andriod开发]之Andriod布局 VS WinPhone布局
0.写在前面的话 近来被HTML+CSS的布局折腾的死去活来,眼巴巴的看着CSS3中的flex,grid等更便捷更高效的的布局方式无法在项目中应用,心里那叫一个窝火啊,去你妹的兼容性,,, 最近体验下 ...
- 介绍MFSideMenu左右滑动控件的使用
昨天刚写完侧滑菜单的实例,今天在CocoaChina网站上看到一篇非常好的侧滑菜单设计案例文章,分享给大家.http://www.cocoachina.com/macdev/uiue/2013/071 ...
- Mapreduce体系架构
Mapreduce也采用master和slave的架构设计.Jobtracker负责作业的初始化和分配 与任务节点进行通信,协调整个作业的执行. 一个job分为两种task(map/reduce),包 ...
- json接口
http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?apikey=7waqfqbprs7pajbz2 ...
- c# 根据配置文件路径,设置和获取config文件 appSettings 节点值
/// <summary> /// 获取编译后的主配置文件节点值 /// </summary> /// <param name="key">&l ...
- Sublime Text的常用插件
工欲善其事,必先利其器.好的插件会更多的帮助我们装逼. 最新感悟:也不要装太多的插件.有些无用的插件可以删除,有时反而臃肿.bloger下载官方最新稳定版st3 3126下载下来25M左右.安装十来个 ...
- docker tomcat7 dubbo-admin monitor
docker run --name=dubbo_admin9201 -tid -p : -v /home/dubbo/admin:/usr/local/tomcat7/webapps/ROOT cen ...
- 在SQL SERVER中获取表中的第二条数据
在SQL SERVER中获取表中的第二条数据, 思路:先根据时间逆排序取出前2条数据作为一个临时表,再按顺时排序在临时表中取出第一条数据 sql语句如下: select top 1 * from(se ...