WPF byte [], Image, BitmapImage conversion

        public  byte [] GetPictureData ( String ImagePath) 
        { 
            
            // using the flow path opening image files, and save the byte [] 
            the FileStream FS = new new the FileStream (ImagePath, FileMode.Open);
             byte [] = byData new new  byte [FS .length]; 
            fs.Read (byData, 0 , byData.Length); 
            fs.Close (); 
            return byData; 
        } 
 
        public the BitmapImage ByteArrayToBitmapImage ( byte [] byteArray The) 
        { 
            the BitmapImage BMP = null ; 
 
            the try
            {
                bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = new MemoryStream(byteArray);
                bmp.EndInit();
            }
            catch
            {
                bmp = null;
            }
 
            return bmp;
        }
 
        public byte[] BitmapImageToByteArray(BitmapImage bmp)
        {
            byte[] byteArray = null;
 
            try
            {
                Stream sMarket =bmp.StreamSource; 
 
                IF (! sMarket = null && sMarket.Length> 0 ) 
                { 
                    // the Position Stream often at the end, leading to the following read zero length. 
                    = sMarket.Position 0 ; 
 
                    the using (the BinaryReader br = new new the BinaryReader (sMarket)) 
                    { 
                        byteArray The = br.ReadBytes (( int ) sMarket.Length); 
                    } 
                } 
            } 
            the catch 
            { 
                //
             } 
 
            return byteArray The; 
        }

 

Guess you like

Origin www.cnblogs.com/Bull-Rush/p/12619396.html