C# byte数组与Image的相互转换

功能需求:

1、把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库。

2、把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示。

3、从图片byte数组得到对应图片的格式,生成一张图片保存到磁盘上。

这里的Image是System.Drawing.Image。

以下三个函数分别实现了上述三个需求:

// Convert Image to Byte[]
        private byte[] ImageToByte(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;
            }
        }

        // Convert Byte[] to Image
        private Image ByteToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }

        // Convert Byte[] to a picture
        private string CreateImageFromByte(string fileName, byte[] buffer)
        {
            string file = fileName; //文件名(不包含扩展名)
            Image image = ByteToImage(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";
            }
            //文件路径目录必须存在,否则先用Directory创建目录
            File.WriteAllBytes(file, buffer);
            return file;
        }
View Code
//用Graphics.DrawImage() 来copy Image到一个新建立的Bitmap 对象然后多这个新的Image 执行Resize
// Convert Image to Byte[]
 private byte[] ImageToByte(Image image)
        {
            System.IO.MemoryStream Ms = new MemoryStream();
            Bitmap bmp = new Bitmap(image.Width, image.Height);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height));
            bmp.Save(Ms, image.RawFormat);
            
            byte[] byteImg = Ms.GetBuffer();
            bmp.Dispose();
            g.Dispose();
            Ms.Close();

            return  byteImg;
        }
解决A generic error occurred in GDI+的问题

 

转载于:https://www.cnblogs.com/songjiali/p/3381883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值