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://www.cnblogs.com/luxiaoxun/p/3378416.html,如需转载请自行联系原作者

相关文章
|
2月前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
2月前
|
存储 C#
C#学习系列相关之数组(一)---数组的定义与使用
C#学习系列相关之数组(一)---数组的定义与使用
|
5月前
|
存储 人工智能 C#
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
【Unity 3D】C#中数组、集合、栈、队列、哈希表、字典的讲解(附测试代码)
40 0
|
2月前
|
C#
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
39 1
|
4月前
|
存储 C# C++
C# 笔记2 - 数组、集合与与文本文件处理
C# 笔记2 - 数组、集合与与文本文件处理
48 0
|
4月前
|
存储 数据可视化 C#
C# Break 和 Continue 语句以及数组详解
它被用于“跳出” switch 语句。 break 语句也可用于跳出循环。 以下示例在 i 等于 4 时跳出循环: 示例:
59 0
|
4月前
|
存储 C#
C#基础语法(数组和函数)
C#基础语法(数组和函数)
19 1
|
5月前
|
Java C# 索引
【从Java转C#】第六章:数组
【从Java转C#】第六章:数组
|
5月前
|
存储 C# 索引
C# | 比较IEnumerable、List、数组
IEnumerable`定义了一组用于枚举集合的方法,包括`GetEnumerator`方法,该方法返回一个实现了`IEnumerator`接口的对象,用于枚举集合中的每个元素。`List`和数组都可以使用`foreach`循环来遍历其中的元素,这是因为它们都实现了`IEnumerable`接口。 由于数组在内存中开辟了一段连续的空间,因此可以直接通过索引访问元素,访问速度很快。而 List 则需要通过指针或引用来访问元素,速度相对较慢。 由于数组的大小是固定的,当需要添加或删除元素时,需要重新创建一个新数组,将原数组中的元素复制到新数组中,并添加或删除元素。
73 0
C# | 比较IEnumerable、List、数组
|
5月前
|
JSON C# 数据格式
C# | 使用DataGridView展示JSON数组
你想展示一个复杂的JSON数组数据吗?但是你却不知道该如何展示它,是吗?没问题,因为本文就是为解决这个问题而生的!使用DataGridView轻松地将JSON数组数据以表格的形式呈现出来,这样你就可以更加清晰地了解和处理数据了。 让我们一起来探索如何实现吧!
77 0
C# | 使用DataGridView展示JSON数组