Use C # to access network pictures and local pictures (in the form of binary streams) to SQL Sever

Source: Internet
Author: User

First do the normal, store our local image, convert it into binary stream stored in the table corresponding to the database.

The code is as follows:

  stringPath =".. /.. /a.jpg"; FileStream FS=NewFileStream (path, FileMode.Open); intStreamlength = (int) fs. Length;//gets the length of the file stream.             byte[] image =New byte[Streamlength];//declares a byte array for saving picture filesFs. Read (Image,0, streamlength);//Convert a picture file to a byte array saveFS.            Close (); varp =NewPictureurl {pictureUrl1=image}; Db.pictureUrl.InsertOnSubmit (p);//Here you use LINQ statements to implement an INSERT record. Db. SubmitChanges ();

This situation is used more, but there are other situations, such as we want to access a picture on the network, but do not want to download it locally, it is very troublesome, just want to pass the path of the picture, to turn it into

Binary stream, which is stored in the database.

The code is as follows

            stringPath ="https://img3.doubanio.com/mpic/s8896281.jpg"; Uri URL=NewUri (path); WebRequest WebRequest=webrequest.create (URL); WebResponse WebResponse=Webrequest.getresponse (); Bitmap MyImage=NewBitmap (WebResponse.GetResponseStream ()); MemoryStream Ms=NewMemoryStream ();            Myimage.save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); varp =NewPictureurl {pictureUrl1=Ms.            ToArray ()};            Db.pictureUrl.InsertOnSubmit (P); Db. SubmitChanges ();

The code that reads the picture, like both, is displayed in the foreground through the image control

  var  from inch Select Picture ;             = pictures. First ();             New MemoryStream (MyPicture.pictureUrl1.ToArray ());             = Image.fromstream (Mymemorystream);

Operation Result:

Use C # to access network pictures and local pictures (in the form of binary streams) to SQL Sever

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.