Use C # to access the network image and local image (in the form of a binary stream) in SQL Server ),

Source: Internet
Author: User

Use C # to access the network image and local image (in the form of a binary stream) in SQL Server ),

First, it is common to store our local images and convert them into binary streams and store them in the table corresponding to the database.

The Code is as follows:

 

String path = ".. /.. /A.jpg "; FileStream fs = new FileStream (path, FileMode. open); int streamLength = (int) fs. length; // obtain the Length of the file stream. Byte [] image = new byte [streamLength]; // declares a byte array for saving the image file fs. read (image, 0, streamLength); // converts an image file into a byte array to save fs. close (); var p = new pictureUrl {pictureUrl1 = image}; db. pictureUrl. insertOnSubmit (p); // insert records using the linq statement. Db. SubmitChanges ();

 

This situation is often used, but there are other cases. For example, if we want to access an image on the network, but do not want to download it to a local location, it is very troublesome, you only want to convert the image

Binary stream, stored in the database.

The Code is as follows:

 

            string path = "https://img3.doubanio.com/mpic/s8896281.jpg";            Uri url = new Uri(path);            WebRequest webRequest = WebRequest.Create(url);            WebResponse webResponse = webRequest.GetResponse();            Bitmap myImage = new Bitmap(webResponse.GetResponseStream());            MemoryStream ms = new MemoryStream();            myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);            var p = new pictureUrl            {                pictureUrl1 = ms.ToArray()            };            db.pictureUrl.InsertOnSubmit(p);            db.SubmitChanges();

 

The code used to read images is the same as the code used to read images.

  var pictures = from picture in db.pictureUrl select picture;            pictureUrl myPicture = pictures.First();            MemoryStream mymemorystream = new MemoryStream(myPicture.pictureUrl1.ToArray());            pictureBox1.Image = Image.FromStream(mymemorystream);

Running result:

 

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.