C#byte数组与Image的相互转换实例代码
作者:lqh 时间:2023-08-15 16:15:51
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(0, SeekOrigin.Begin);
ms.Read(buffer, 0, 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;
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/u013816709/article/details/50695713
标签:C#,byte数组,Image
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java程序设计语言的优势及特点
2022-08-16 09:12:57
详解Unity安卓共享纹理
2022-07-10 08:18:21
![](https://img.aspxhome.com/file/2023/1/111161_0s.png)
SpringMVC 向jsp页面传递数据库读取到的值方法
2022-03-29 00:51:15
Java构造代码块,静态代码块原理与用法实例分析
2023-11-03 09:03:45
![](https://img.aspxhome.com/file/2023/7/58707_0s.png)
Android事件冲突解决悬浮窗拖拽处理方案
2023-12-17 01:51:17
![](https://img.aspxhome.com/file/2023/9/91649_0s.jpg)
Android apk 插件启动内存释放问题
2022-05-16 07:26:39
详解Android Service与Activity之间通信的几种方式
2023-10-26 04:15:48
关于Spring Data Jpa 自定义方法实现问题
2023-11-28 10:08:32
springboot2.5.6集成RabbitMq实现Topic主题模式(推荐)
2021-10-03 22:01:22
![](https://img.aspxhome.com/file/2023/4/83494_0s.png)
Java基础之static关键字的使用讲解
2023-10-06 01:26:25
![](https://img.aspxhome.com/file/2023/9/66039_0s.jpg)
C#实现目录跳转(TreeView和SplitContainer)的示例代码
2023-07-31 11:07:59
![](https://img.aspxhome.com/file/2023/4/92964_0s.jpg)
Android实现多个连续带数字圆圈效果
2021-09-10 02:46:23
![](https://img.aspxhome.com/file/2023/1/88701_0s.jpg)
c#使用file.copy实现文件备份示例
2021-06-03 05:13:13
Spring Security权限想要细化到按钮实现示例
2022-10-14 20:52:38
![](https://img.aspxhome.com/file/2023/5/106155_0s.png)
Android View事件机制 21问21答
2022-10-14 04:27:10
java协程框架quasar和kotlin中的协程对比分析
2021-06-25 23:22:05
![](https://img.aspxhome.com/file/2023/4/75694_0s.png)
c#快速写本地日志方法
2021-08-24 09:31:17
![](https://img.aspxhome.com/file/2023/2/106382_0s.png)
在IntelliJ IDEA中多线程并发代码的调试方法详解
2022-01-09 03:05:45
![](https://img.aspxhome.com/file/2023/6/67146_0s.jpg)
C# ref and out的使用小结
2021-12-19 22:35:40
![](https://img.aspxhome.com/file/2023/1/106391_0s.png)
SpringBoot接入支付宝支付的方法步骤
2022-02-16 05:34:16
![](https://img.aspxhome.com/file/2023/9/100049_0s.png)