圖片上傳,包含讀取圖片位元組大小及長寬,及Base64轉換

來源:互聯網
上載者:User
上傳|轉換 <form id="PicAdd" method="post" runat="server" enctype="multipart/form-data">
    <input type="file" runat="server" id="Pic" NAME="Pic">
</form>
'++++++++++++++++++++++以下為讀取代碼
HttpPostedFile     UpFile=Pic.PostedFile;//讀取資料
            FileLength=UpFile.ContentLength;//取得資料的長度
//判斷是不是上傳了圖片
            if(FileLength==0)
            {
                Message.InnerHtml="請選擇要上傳的檔案";
                Message.Style["color"]="red";
            }
            else
            {
                //這裡判斷一下位元組大小
                PIC.PicClass Length=new PIC.PicClass();
                bool CheckResult=Length.CheckBytes(FileLength);
                if(CheckResult==false)
                {
                    Message.InnerHtml="圖片位元組數不能超過20K";
                    Message.Style["color"]="red";
                }
                else
                {
                    //判斷圖片的長和寬
                    Byte[] FileByteArray=new byte[FileLength];//圖象檔案臨時儲存到Byte數組裡
                    Stream StreamObject=UpFile.InputStream;//建立資料流對象
                    StreamObject.Read (FileByteArray,0,FileLength);
                    //轉換成Base64格式
                    PIC.PicClass ByteToBase=new PIC.PicClass();
                    string Base64String=ByteToBase.ByteToBase64(FileByteArray,0,FileByteArray.Length );
                    //建立圖片對象
                    System.Drawing.Image MyImage=System.Drawing.Image.FromStream(StreamObject);
                    //開始判斷圖片的大小
                    PIC.PicClass CheckImage=new PIC.PicClass();
                    bool CheckImageResult=CheckImage.CheckImage(MyImage.Width ,MyImage.Height );
                    if(CheckImageResult==false)
                    {
                        Message.InnerHtml="請注意圖片長寬控制在132*146之內";
                        Message.Style["color"]="red";
                    }
                    else
                    {
                        try
                        {
                           //這裡省略了資料庫的操作
                         }
‘++++++++++++以下為判斷位元組大小,圖片大小,及Base64轉換
public bool CheckBytes(int FileLength)
        {
            bool Result=true;
            int Length=20480;
            if(FileLength>Length)
            {
                Result=false;
            }
            return Result;
        }
        public bool CheckImage(int Width,int Height)
        {
            bool Result=true;
            int width=132,height=146;
            if(Width>width||Height>height)
            {
                Result=false;
            }
            return Result;
        }
        //這裡是進行二進位到Base64的轉換
        public string ByteToBase64(byte[] ByteArry,int Start,int Length)
        {
            string Result;
            Result=System.Convert.ToBase64String(ByteArry,Start,Length);
            return Result;
        }

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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