Save pictures to database and C # read pictures

Source: Internet
Author: User

The picture is saved to the database, if SQL Server is the image type, if it is saved to Oracle is the BLOB type, in C # corresponds to the byte[] type, and only need to cast the read data to the line (byte[]) object .

1. Save the picture as a byte array

parameter is picture path, return byte[] Type

 Public byte [] Getpicturedata (string  ImagePath)    {       new  FileStream (ImagePath, FileMode.Open);        byte New byte [File. Length];        0 , by. Length);       File. Close ();        return by ;    }

parameter is image, return byte[] Type

 Public byte[] Getpicturedata (System.Drawing.Image imgphoto) {//convert image to stream data and save as byte[]MemoryStream Mstream=NewMemoryStream ();       Imgphoto.save (mstream,system.drawing.imaging.imageformat.bmp); byte[]bydata=NewByte[mstream.       Length]; Mstream. Position=0; Mstream. Read (Bydata,0, bydata.length); Mstream.       Close (); returnBydata; }  

2. Converting a byte array to a picture

//parameter is a byte[] type, and the return value is an Image object     PublicSystem.Drawing.Image Returnphoto (byte[] streambyte) {MemoryStream me=NewMemoryStream (Streambyte); returnSystem.Drawing.Image.FromStream (MS); }      //parameter is a byte[] type, there is no return value, which is for ASP. Export the picture from the output to the Web page     Public voidWritephoto (byte[] streambyte) {Response.ContentType="Image/gif";    Response.BinaryWrite (Streambyte); }

3. Conversion of Byte[] and string

    // byte[] Convert to string    byte [] by;     string str=System.Convert.ToBase64String (by);     // convert String to byte[]    string str;     byte [] by=system.convert.frombase64string (str);

Save pictures to database and C # read pictures

Related Article

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.