Picture upload, including read image byte size and width, and Base64 conversion

Source: Internet
Author: User
Tags base64 bool
Upload | convert <form id= "Picadd" method= "POST" runat= "server" enctype= "Multipart/form-data" >
<input type= "File" runat= "server" id= "pic" name= "pic" >
</form>
' ++++++++++++++++++++++ read the code below
Httppostedfile upfile=pic.postedfile;//Read Data
filelength=upfile.contentlength;//to get the length of the data
Judge whether or not to upload the picture
if (filelength==0)
{
Message.innerhtml= "Please select the file to upload";
message.style["Color"]= "red";
}
Else
{
Here to determine the size of the byte
Pic. Picclass length=new PIC. Picclass ();
BOOL Checkresult=length.checkbytes (filelength);
if (Checkresult==false)
{
Message.innerhtml= "Picture bytes cannot exceed 20K";
message.style["Color"]= "red";
}
Else
{
Judge the length and width of a picture
Byte[] Filebytearray=new byte[filelength];//image files are temporarily stored in a byte array
Stream streamobject=upfile.inputstream;//establish a data flow object
Streamobject.read (filebytearray,0,filelength);
Convert to BASE64 format
Pic. Picclass bytetobase=new PIC. Picclass ();
String Base64string=bytetobase.bytetobase64 (Filebytearray,0,filebytearray.length);
Create a Picture object
System.Drawing.Image Myimage=system.drawing.image.fromstream (Streamobject);
Start judging the size of the picture
Pic. Picclass checkimage=new PIC. Picclass ();
BOOL Checkimageresult=checkimage.checkimage (myimage.width, myimage.height);
if (Checkimageresult==false)
{
Message.innerhtml= "Please note that the picture width control within the 132*146";
message.style["Color"]= "red";
}
Else
{
Try
{
This omits the operation of the database
}
' ++++++++++++ below to determine byte size, picture size, and Base64 conversion
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;
}
This is a binary to Base64 conversion.
public string ByteToBase64 (byte[] bytearry,int start,int Length)
{
string result;
Result=system.convert.tobase64string (bytearry,start,length);
return result;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.