//保存目录
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. Oracle RAC安装部署文档

    1.    部署环境步骤 1.1 软件环境 操作系统:CentOS release 6.5(推荐使用5.*的系统)192.168.1.151    racnode1 192.168.1.152    ...

  2. 自发行python版本制作(一)

    最近使用python开发一些小玩意,发现python实在很符合我的理念:轻量级,功能强大,开放. python是一种脚本语言,不像java那样需要沉重的编译过程.这使得python更显得轻巧灵便,可以 ...

  3. 小结一下前段时间做的rpgdemo

    虽然说已经是彻底放弃继续做那个demo了(代码结构混乱,想增加新功能非常的不方便),不过还是花了一点心血在里面的,毕竟这是我开始学习unity游戏制作的初衷,不过果然是学的越多越发现自己的不足... ...

  4. 我的Android第二章

    前言 之前有很多人遇到了关于内部类的问题[主要在android的学习之中会大量的使用到],内部类是什么,内部类怎么定义,内部类的分类,内部类的好处,内部类如何访问,这里我们来结合代码简单的理解一下 1 ...

  5. 脚本改yum源

  6. sql-ASCII函数运用

    declare @a int = ASCII('A') declare @b int = ASCII('F') declare @c int = 1 while(@a <= @b) begin ...

  7. Java生成带小图标的二维码-google zxing 工具类

    近期一直忙于开发微信商城项目,应客户要求,要开发个有图标的二维码.经过两次改版,终于实现了该功能(第一次没有小图标,这次才整合好的),如下是完整代码 . 该代码使用Java7开发,另外使用 core- ...

  8. Oracle(修改表结构和约束)

    目标: 1.添加和修改列 2.添加,enable,disable,或者remove约束 3.删除表 4.删除表中所有数据并回到表定义的初始状态(截断表) 5.修改对象的名字 6.给对象添加注释,从数据 ...

  9. $.ajax()方法所有参数详解;$.get(),$.post(),$.getJSON(),$.ajax()详解

    [一]$.ajax()所有参数详解 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注 ...

  10. adnroid 监听软键盘的显隐

    首先注明出处,我只是想做个笔记:http://www.jb51.net/article/64820.htm 第一眼看到的时候,觉得不太可靠,因为之前都是看到这样处理的,根据监听一个根布局的size,可 ...