首页 > 开发 > 综合 > 正文

图片上传,包含读取图片字节大小及长宽,及Base64转换

2024-07-21 02:23:56
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

<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;
        }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表