windowsphone7下圖片格式的轉換

來源:互聯網
上載者:User

windowsphone7和siverlight中的BitmapImage和Image提供的介面還是很比較大的區別的,總體來說siverlight的封裝性更加健全一些。

同時在壓縮bitmap的時候,siverlight會提供設定和訪問映像dpi(dots per inch)和pixel的函數,但是wp7中只能訪問和操作pixel。這樣的話在wp7下,對於壓縮一張圖片來說,你可以很方便增加或減少像素,但是卻很難訪問它的dpi,那麼圖片的實際寬度和高度就很難定製了。

下面是對圖片和檔案流格式的一個轉換類,可以參考一下:

    public class ImageUtil    {        /// <summary>        /// 將Image轉換成二進位流        /// </summary>        /// <param name="sourceImage"></param>        /// <returns></returns>        public static byte[] ImageToByteArray(Image sourceImage)        {            return ImageSourceToByteArray(sourceImage.Source);        }        /// <summary>        /// 將ImageSource轉換成二進位流        /// </summary>        /// <param name="sourceImage"></param>        /// <returns></returns>        public static byte[] ImageSourceToByteArray(ImageSource source)        {            BitmapSource bs = (BitmapSource)source;            using (MemoryStream ms = new MemoryStream())            {                WriteableBitmap writeableBitmap = new WriteableBitmap(bs);                Extensions.SaveJpeg(writeableBitmap, ms, bs.PixelWidth, bs.PixelHeight, 0, 100);                return ms.GetBuffer();            }        }        /// <summary>        /// 將二進位流轉換為Image格式圖片        /// </summary>        /// <param name="bitData"></param>        /// <returns></returns>        public static Image ByteArrayToImage(byte[] bitData)        {             Image image = new Image();            image.Source = ByteArrayToBitmapImage(bitData);            return image;        }        /// <summary>        /// 將二進位流轉換為BitmapImage格式圖片        /// </summary>        /// <param name="bitData"></param>        /// <returns></returns>        public static BitmapImage ByteArrayToBitmapImage(byte[] bitData)        {            BitmapImage bitmap = new BitmapImage();            using (MemoryStream ms = new MemoryStream(bitData))            {                bitmap.CreateOptions = BitmapCreateOptions.DelayCreation;                bitmap.SetSource(ms);                return bitmap;            }        }        /// <summary>        /// 將流資料寫入jpeg檔案        /// </summary>        /// <param name="stream"></param>        /// <param name="fileDirectory"></param>        /// <param name="filename"></param>        /// <param name="targetWidth"></param>        /// <param name="targetHeight"></param>        public static void WriteStreamObjectToJPEG(Stream stream, string fileDirectory,string filename, int targetWidth, int targetHeight)        {            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())            {                if (!store.DirectoryExists(fileDirectory))                {                    store.CreateDirectory(fileDirectory);                }                WriteableBitmap bmpImage = new WriteableBitmap(targetWidth, targetHeight);                bmpImage.SetSource(stream);                using (IsolatedStorageFileStream isoStream =                    store.OpenFile(fileDirectory + '/' + filename, FileMode.OpenOrCreate))                {                    System.Windows.Media.Imaging.Extensions.SaveJpeg(bmpImage, isoStream, targetWidth, targetHeight, 0, 100);                }            }        }        /// <summary>        /// 將jpeg檔案寫入到資料流中        /// </summary>        /// <param name="filePath"></param>        /// <returns></returns>        public static BitmapImage ReadBitmapImageFromJPEG(string filePath)        {                               BitmapImage image = new BitmapImage();            Stream source = ReadStreamFromJPEG(filePath);            image.SetSource(source);            return image;        }        public static Stream ReadStreamFromJPEG(string filePath)        {            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())            {                using (IsolatedStorageFileStream isoStream =                    store.OpenFile(filePath, FileMode.Open))                {                    Stream fileStream = new MemoryStream();                    isoStream.CopyTo(fileStream);                    isoStream.Close();                    isoStream.Dispose();                    store.Dispose();                    return fileStream;                }            }        }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.