图片与字节数组相互转换的方法
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class _2Stream : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void FileToStream(object sender, EventArgs e)
{
//将JPG图片转化成字节数组
Image image = Image.FromFile("E:/1.jpg"); //或者使用Server.MapPath
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, 0, (int)ms.Length);
//遍历字节数组
for (int i = 0; i < buffer.LongLength; i++)
{
message.Text += buffer[i].ToString();
}
//将字节数组转化成图像文件(自定义格式)并保存
MemoryStream ms2 = new MemoryStream(buffer, 0, buffer.Length);
ms2.Seek(0, SeekOrigin.Begin);
Image image2 = Image.FromStream(ms2);
image2.Save("E:\\2.gif", ImageFormat.Gif);
}
}
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class _2Stream : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void FileToStream(object sender, EventArgs e)
{
//将JPG图片转化成字节数组
Image image = Image.FromFile("E:/1.jpg"); //或者使用Server.MapPath
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, 0, (int)ms.Length);
//遍历字节数组
for (int i = 0; i < buffer.LongLength; i++)
{
message.Text += buffer[i].ToString();
}
//将字节数组转化成图像文件(自定义格式)并保存
MemoryStream ms2 = new MemoryStream(buffer, 0, buffer.Length);
ms2.Seek(0, SeekOrigin.Begin);
Image image2 = Image.FromStream(ms2);
image2.Save("E:\\2.gif", ImageFormat.Gif);
}
}