C# 二进制图片串互转
C# byte数组与Image的相互转换
功能需求:
1、把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库。
2、把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示。
3、从图片byte数组得到对应图片的格式,生成一张图片保存到磁盘上。
这里的Image是System.Drawing.Image。
//Get an image from file
Image image = Image.FromFile("D:\\test.jpg");
Bitmap bitmap = new Bitmap("D:\\test.jpg");
以下三个函数分别实现了上述三个需求:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text; namespace NetUtilityLib
{
public static class ImageHelper
{
/// <summary>
/// Convert Image to Byte[]
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static byte[] ImageToBytes(Image image)
{
ImageFormat format = image.RawFormat;
using (MemoryStream ms = new MemoryStream())
{
if (format.Equals(ImageFormat.Jpeg))
{
image.Save(ms, ImageFormat.Jpeg);
}
else if (format.Equals(ImageFormat.Png))
{
image.Save(ms, ImageFormat.Png);
}
else if (format.Equals(ImageFormat.Bmp))
{
image.Save(ms, ImageFormat.Bmp);
}
else if (format.Equals(ImageFormat.Gif))
{
image.Save(ms, ImageFormat.Gif);
}
else if (format.Equals(ImageFormat.Icon))
{
image.Save(ms, ImageFormat.Icon);
}
byte[] buffer = new byte[ms.Length];
//Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
ms.Seek(, SeekOrigin.Begin);
ms.Read(buffer, , buffer.Length);
return buffer;
}
} /// <summary>
/// Convert Byte[] to Image
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static Image BytesToImage(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
Image image = System.Drawing.Image.FromStream(ms);
return image;
} /// <summary>
/// Convert Byte[] to a picture and Store it in file
/// </summary>
/// <param name="fileName"></param>
/// <param name="buffer"></param>
/// <returns></returns>
public static string CreateImageFromBytes(string fileName, byte[] buffer)
{
string file = fileName;
Image image = BytesToImage(buffer);
ImageFormat format = image.RawFormat;
if (format.Equals(ImageFormat.Jpeg))
{
file += ".jpeg";
}
else if (format.Equals(ImageFormat.Png))
{
file += ".png";
}
else if (format.Equals(ImageFormat.Bmp))
{
file += ".bmp";
}
else if (format.Equals(ImageFormat.Gif))
{
file += ".gif";
}
else if (format.Equals(ImageFormat.Icon))
{
file += ".icon";
}
System.IO.FileInfo info = new System.IO.FileInfo(file);
System.IO.Directory.CreateDirectory(info.Directory.FullName);
File.WriteAllBytes(file, buffer);
return file;
}
}
}
C# 二进制图片串互转的更多相关文章
- python模块介绍- binascii:二进制和ASCII互转以及其他进制转换
20.1 binascii:二进制和ASCII互转作用:二进制和ASCII互相转换. Python版本:1.5及以后版本 binascii模块包含很多在二进制和ASCII编码的二进制表示转换的方法.通 ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
- [.ashx檔?泛型处理程序?]基础入门#5....ADO.NET 与 将DB里面的二进制图片还原 (范例下载 & 大型控件的ImageField)
[.ashx檔?泛型处理程序?]基础入门#5....ADO.NET 与 将DB里面的二进制图片还原 (范例下载 & 大型控件的ImageField) http://www.dotblogs.c ...
- [.ashx檔?泛型处理例程?]基础入门#3....ADO.NET 与 将DB里面的二进制图片还原
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/20/ashx_beginner_03_db_picture_show.aspx ...
- 将数据库二进制图片导出显示到EPPlus Excel2007中
1.EPPlus Excel 控件可以参考我的另一篇博客:http://blog.163.com/pei_huiping/blog/static/206573067201281810549984/ 这 ...
- atitit.无损传输二进制数据串传输网络
atitit.无损传输二进制数据串传输网络 1. gbk的网络传输问题,为什么gbk不能使用来传输二进制数据 1 2. base64 2 3. iso-8859-1 (推荐) 2 4. utf-8 ...
- .NET中二进制图片的存储与读取
判断HttpContext是否为空: string configPath = "img/defaultPhoto.png"; if (HttpContext.Current != ...
- JSP读取数据库二进制图片并显示
用JSP从数据库中读取二进制图片并显示在网页上 环境mysql+tomcat: 先在mysql下建立如下的表. 并存储了二进制图像(二进制格式存储图片可以参考我的另一篇博客:https://www.c ...
- Python如何向SQLServer存储二进制图片
需求是需要用python往 SqlServer中的image类型字段中插入二进制图片 核心代码,研究好几个小时的代码: 安装pywin32,adodbapi image_url = "图片链 ...
随机推荐
- POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)
题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...
- 转载的 Linux下chkconfig命令详解
Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...
- shell篇(二)
Linux的shell种类比较多,常见的有:Bourne Shell(/user/bin/sh或者/bin/sh), Bourne Again Shell(/user/bin/bash或者/bin/b ...
- redis windows版本下载
https://github.com/dmajkic/redis/downloads http://windows.php.net/downloads/pecl/snaps/redis/3.1.4rc ...
- animation-fill-mode
animation-fill-mode: none:默认值.不设置对象动画之外的状态 forwards:结束后保持动画结束时的状态,但当animation-direction为0,则动画不执行,持续保 ...
- Kafka集群监控工具之二--Kafka Eagle
基于kafka: kafka_2.11-0.11.0.0.tgz kafka-eagle-bin-1.2.1.tar.gz 1.下载解压 tar -zxvf kafka-eagle-bin-1.2.1 ...
- python 打印输出至文件中, 'wt'读写文件方式,会把原文件内容清空
会把原文件内容清空,写最新的 文件必须以文本模式打开,如果文件是二进制模式的话,打印会出错
- linux常用命令:chmod 命令
chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...
- Linux服务器---apache支持cgi
Apache支持cgi 1.打开Apache配置文件httpd.conf,搜索“cgi”,找到下面的一段,去掉“addhandler”前面的“#“,这样就开启了Apache的cgi功能 [root@ ...
- php array_multisort对数据库结果多个字段进行排序
php array_multisort对数据库结果多个字段进行排序$data 数组中的每个单元表示一个表中的一行.这是典型的数据库记录的数据集合. 例子中的数据如下:volume | edition ...