* Stream 和 byte[] 之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 文件之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 写入文件
/// </summary>
public void StreamToFile(Stream stream,string fileName)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
/// <summary>
/// 从文件读取 Stream
/// </summary>
public Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 文件之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 写入文件
/// </summary>
public void StreamToFile(Stream stream,string fileName)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
/// <summary>
/// 从文件读取 Stream
/// </summary>
public Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
C#中结合Post发送的Stream与Byte Array的操作似乎资料很少.下面是我这几天的研究成果.功能是将同文件夹下的a.jpg复制生成b.jpg.代码如下:
byteArray.aspx.cs
sing System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
/*@
Author:frank
Site:www.2solo.cn
Date:2008.02.20
Info:C#复制图片,流与byteArray的应用,生成图片部分
*/
namespace bArray {
public partial class imgHandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Stream sin = Page.Request.InputStream;
System.Drawing.Image img = System.Drawing.Bitmap.FromStream(sin);
Bitmap bmp = new Bitmap(img);
MemoryStream bmpStream = new MemoryStream();
bmp.Save(bmpStream, System.Drawing.Imaging.ImageFormat.Jpeg);
FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath("b.jpg"), FileMode.Create);
bmpStream.WriteTo(fs);
bmpStream.Close();
fs.Close();
bmpStream.Dispose();
fs.Dispose();
Response.Write("成功");
}
catch
{
Response.Write("失败");
}
}
}
}
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
/*@
Author:frank
Site:www.2solo.cn
Date:2008.02.20
Info:C#复制图片,流与byteArray的应用,生成图片部分
*/
namespace bArray {
public partial class imgHandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Stream sin = Page.Request.InputStream;
System.Drawing.Image img = System.Drawing.Bitmap.FromStream(sin);
Bitmap bmp = new Bitmap(img);
MemoryStream bmpStream = new MemoryStream();
bmp.Save(bmpStream, System.Drawing.Imaging.ImageFormat.Jpeg);
FileStream fs = new FileStream(System.Web.HttpContext.Current.Server.MapPath("b.jpg"), FileMode.Create);
bmpStream.WriteTo(fs);
bmpStream.Close();
fs.Close();
bmpStream.Dispose();
fs.Dispose();
Response.Write("成功");
}
catch
{
Response.Write("失败");
}
}
}
}
gopost.aspx.cs
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Text;
/*@
Author:frank
Site:www.2solo.cn
Date:2008.02.20
info:C#复制图片,流与byteArray的应用,提交图片部分
*/
namespace gopost
{
public partial class postHandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
postImage();
}
private void postImage()
{
try
{
HttpWebRequest request;
string imgUrl = System.Web.HttpContext.Current.Server.MapPath("a.jpg");
request = (HttpWebRequest)HttpWebRequest.Create(http://localhost/byteArray/byteArray.aspx);
request.KeepAlive = true;
request.Method = "POST";
byte[] byteArray = CvtImgBArr((System.Drawing.Image)new Bitmap(@imgUrl), ImageFormat.Jpeg);
request.ContentType = "image/JPEG";
request.ContentLength = byteArray.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
Response.Write("复制图片成功");
}
catch
{
Response.Write("复制图片失败");
}
}
private static byte[] CvtImgBArr(System.Drawing.Image imageToConvert, ImageFormat formatOfImage)
{
byte[] imArr;
try
{
using (MemoryStream myms = new MemoryStream())
{
imageToConvert.Save(myms, formatOfImage);
imArr = myms.ToArray();
}
}
catch (Exception) { throw; }
return imArr;
}
}
}
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Text;
/*@
Author:frank
Site:www.2solo.cn
Date:2008.02.20
info:C#复制图片,流与byteArray的应用,提交图片部分
*/
namespace gopost
{
public partial class postHandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
postImage();
}
private void postImage()
{
try
{
HttpWebRequest request;
string imgUrl = System.Web.HttpContext.Current.Server.MapPath("a.jpg");
request = (HttpWebRequest)HttpWebRequest.Create(http://localhost/byteArray/byteArray.aspx);
request.KeepAlive = true;
request.Method = "POST";
byte[] byteArray = CvtImgBArr((System.Drawing.Image)new Bitmap(@imgUrl), ImageFormat.Jpeg);
request.ContentType = "image/JPEG";
request.ContentLength = byteArray.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
Response.Write("复制图片成功");
}
catch
{
Response.Write("复制图片失败");
}
}
private static byte[] CvtImgBArr(System.Drawing.Image imageToConvert, ImageFormat formatOfImage)
{
byte[] imArr;
try
{
using (MemoryStream myms = new MemoryStream())
{
imageToConvert.Save(myms, formatOfImage);
imArr = myms.ToArray();
}
}
catch (Exception) { throw; }
return imArr;
}
}
}