Pictures and byte conversion

First, the picture transfer byte

 public byte[]  ImageToByte()
        {
            string imagefile = @"http://192.168.0.212/pass/T-1.jpg";//互联网图片地址
            Image img = UrlToImage(imagefile);
            Bitmap bmp = new Bitmap(img);
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            var a = Convert.ToBase64String(arr);
            string strbaser64 = "";
            WebClient mywebclient = new WebClient();
            byte[] Bytes = mywebclient.DownloadData(imagefile);
            return Bytes

        }
        public  Image UrlToImage(string url)
        {
            WebClient mywebclient = new WebClient();
            byte[] Bytes = mywebclient.DownloadData(url);
            using (MemoryStream ms = new MemoryStream(Bytes))
            {
                Image outputImg = Image.FromStream(ms);
                return outputImg;
            }
        }

Two, byte transfer pictures

Save the byte array to Image: 

Mode 1: System.IO.File.WriteAllBytes ( @ " c: \ test.jpg " , bytes); 

Second way: MS MemoryStream = new new MemoryStream (Byte [] b); put the byte [ ] array pass in, and then 
           the FileStream FS = new new the FileStream (e.g. path: " E: \ Image \ 1.jpg " ); 
    ms.writeto (FS); 
    ms.close (); 
    fs.Close (); 

method three: 

       / / obtained image address 
       var stringFilePath context.Server.MapPath = ( String .Format ( " ~ / image / {{0}}. 1 .jpg " , imageName, ID));
        // declare a picture for temporarily into FileStream stream
       Stream = the FileStream new new the FileStream (stringFilePath, FileMode.Open);
        the using (Stream) 
       { 
           // through GetImageFromStream method pictures into byte array 
           byte [] = imageBytes the this .GetImageFromStream (Stream, context);
            // context determination write file types to the client short 
           context.Response.ContentType = " Image / JPEG " ;
            // context data written in the preceding imageBytes 
           context.Response.BinaryWrite (imageBytes); 
           stream.Close (); 
        }

 

Guess you like

Origin www.cnblogs.com/macT/p/11606457.html