//保存目录
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开发者来说,很难发现这个问题到底是为什么。

我们先来看看下面一段代码

string fileName = "sample.jpg";
Bitmap bmp = new Bitmap(fileName);
bmp.Save(fileName, ImageFormat.Jpeg);

这段代码的目的是要打开一个Bitmap,然后保存。可惜这段代码一定会给你一个GDI+一般性错误:

System.Runtime.InteropServices.ExternalException

其中的Error Code是0x80004005, innerException是空。如果你查Windows的Error Code表,会发现这个错误原因是“Unspecified Error”,还是什么都不知道。这其实是.NET Framework封装不好的问题,我们可以调用

Marshal.GetLastWin32Error()

拿到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 bmpTemp = new Bitmap(image);
Bitmap bmp = new Bitmap(bmpTemp);
bmpTemp.Dispose();
bmp.Save(image, ImageFormat.Jpeg);

只要把当前的图像复制一份,然后把旧的Dispose掉,那个文件就不被锁住了,这样就可以放心覆盖原始文件了。

想想如果你要用GDI+写一个Painter,很容易你就会遇到这个问题。

C#中图片与BASE64码互相转换的更多相关文章

  1. JavaScript 图片与Base64数据互相转换脚本

    JavaScript 图片与Base64数据互相转换脚本 注: 转换过程中注意跨域问题.测试页是否支持相关标签创建.dom结构. 方法一:非Html 5使用FileReader 使用XMLHttpRe ...

  2. oracle BLOG图片和CLOG base64码的转换

    --BASE64转图片CREATE OR REPLACE FUNCTION DECODE_BASE64(P_CLOB_IN IN CLOB) RETURN BLOB IS V_BLOB BLOB; V ...

  3. [转]C#中图片.BYTE[]和base64string的转换

    本文转自:http://blog.csdn.net/thebesttome/article/details/6870155 在C#中 图片到byte[]再到base64string的转换: Bitma ...

  4. C#中图片.BYTE[]和base64string的转换

    在C#中 图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath);                MemoryStream ms = ...

  5. JavaScript—图片与base64编码互相转换

    图片转换为base64编码 <input type = "file" id = "file" onchange="popFileName(thi ...

  6. c# 图片 与 BASE64 字符串 互相转换。

    using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...

  7. C#中图片转换为Base64编码,Base64编码转换为图片

    #region 图片转为base64编码的字符串 public string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = ...

  8. java中图片地址base64编码的相互转换

    public class Base64Url { /** * 将base64编码字符串转换为图片 * @param imgStr: base64编码字符串 * @param path: 图片路径-具体 ...

  9. 图片和Base64之间的转换

    public static Bitmap GetImageFromBase64String(string strBase) { try { MemoryStream stream = new Memo ...

随机推荐

  1. angular-select绑定之后option不能更新问题

    使用ng-option-- http://jsfiddle.net/sseletskyy/uky9m/1/ 以及select中加入自定义指令  convert-to-number .directive ...

  2. WPF中通过代码设置控件的坐标

    用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...

  3. 使用IO流实现一个简单的小Dome

    (一) 在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去:之后遍历IOTest ...

  4. SQL 对时间的处理

    --获取当前日期(如:yyyy-mm-dd)Select Datename(year,GetDate())+'-'+Datename(month,GetDate())+'-'+Datename(day ...

  5. 一些Unity基础操作的性能测试

    从以前一个文章转移过来的内容,以后会进一步进行测试  内容  毫秒数(Editor)  毫秒数(Build PC) 加减内部变量 4ms  1ms new List<int>() 559m ...

  6. Android 网络开发之WIFI

    WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为&qu ...

  7. 23-React Render Element

    第23节 React Render Element 1.Element 元素是反应应用程序的最小积木. 元素描述你在屏幕上看到的内容.: const element= <h1>你好,世界& ...

  8. 简述jpg。Gif。png-8.png-24的区别,分别使用场景

    gif.jpg.png格式的图片在网站制作中的区别 一.Gif格式特点: 1.透明性,Gif是一种布尔透明类型,既它可以是全透明,也可以是全不透明,但是它并没有半透明(alpha透明). 2.动画,G ...

  9. python 最大公约数

    求解两个整数(不能是负数)的最大公约数(要求两数不能同时为0)当两数都是0时,最大公约数为0方式一:穷举法 def GCU(m, n): if not m: return n elif not n: ...

  10. ubuntu文件夹建立软链接方法

    1:预备知识 -s 是代号(symbolic)的意思. 这里有两点要注意:第一,ln命令会保持每一处链接文件的同步性,也就是说,不论你改动了哪一处,其它的文件都会发生相同的变化:第二,ln的链接又软链 ...