C#中操作Stream與Byte Array

來源:互聯網
上載者:User
C#中操作Stream與Byte Array2008-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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.