架构深渊

慢慢走进程序的深渊……关注领域驱动设计、测试驱动开发、设计模式、企业应用架构模式……积累技术细节,以设计架构为宗。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#中操作Stream与Byte Array

Posted on 2008-10-09 20:54  chen eric  阅读(1622)  评论(0编辑  收藏  举报
C#中操作Stream与Byte Array
2008-08-01 20:41

本文为frank的学习笔记,转载请注明原文链接

C#中结合Post发送的Stream与Byte Array的操作似乎资料很少.下面是我这几天的研究成果.功能是将同文件夹下的a.jpg复制生成b.jpg.代码如下:

byteArray.aspx.cs

using 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("失败");
            }
        }
    }
}


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;
        }

    }
}

相对来说,byte Array在Html表单中的应用可能一直被忽视,但是正确的应用byte Array可以大大的优化程序,并做出一些意想不到的效果来。当然,我研究byte Array主要还是为了最近的Flash研究.

附上范例文件:http://www.2solo.cn/upload/cbyteArray_2soloDOTcn.zip