功能需求:

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(, SeekOrigin.Begin);
ms.Read(buffer, , 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;
}
//用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(, , 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+的问题

最新文章

  1. Notepad++源码编译及其分析
  2. 转行|如何成为企业想要的Android工程师
  3. hibernate配置文件中的schema="dbo"在MySQL数据库不可用
  4. Mecanim分析
  5. 重装系统分区时,发现一个叫LVM的东西,找出来和大家分享
  6. new-nav-js
  7. Unity Shader Billboard
  8. Netmask v. Address Prefix Length
  9. Android开发之万能适配器
  10. BIOS中断大全
  11. 认识css
  12. 动态规划 POJ3616 Milking Time
  13. listview优化(中)
  14. 进程pid理解
  15. 解决spring多线程不共享事务的问题
  16. 护眼党必备良心app
  17. Dart
  18. python中impyla包报'TSocket' object has no attribute 'isOpen'错误
  19. SQL-7查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t (group 与count)
  20. Unity3D学习笔记(二十二):ScrollView和事件接口

热门文章

  1. word2013删除下载的模板
  2. Android-Sqlite数据库的操作
  3. StringUtils中的常用的方法
  4. iOS CoreAnimation 核心动画
  5. 多线程相关------事件Event
  6. jcl-over-slf4j log桥接工具简介
  7. VirtualBox + CentOS 使用 NAT + Host-Only 方式联网
  8. ip_conntrack table full dropping packet错误的解决方法
  9. Symbols
  10. [dpdk] 熟悉SDK与初步使用 (一)(qemu搭建实验环境)