- #region//圖片轉換為二進制流??
- public?void?PictureToBinaryStream()??
- {??
- ????//獲取當前程序運行路徑??
- ????string?path?=?Application.StartupPath;??
- ????//拼接成測試圖片路徑??
- ????string?fullPath?=?path?+?"\\images\\test.png";??
- ????//初始化類??
- ????Bitmap?bmp?=?new?Bitmap(Image.FromFile(fullPath));??
- ????MemoryStream?ms?=?new?MemoryStream();??
- ????bmp.Save(ms,?System.Drawing.Imaging.ImageFormat.Png);??
- ????ms.Flush();??
- ????//將二進制數據存到byte數字中??
- ????byte[]?bmpBytes?=?ms.ToArray();??
- ????//?richTextBox1.Text?=?Convert.ToBase64String(bmpBytes);??
- ????foreach?(var?item?in?bmpBytes)??
- ????{??
- ????????richTextBox1.Text?+=?item;??
- ????}??
- ????pictureBox1.Image?=?Image.FromStream(new?MemoryStream(bmpBytes));??
- }??
- #endregion??
- ?
- #region//二進制流轉換成圖片??
- public?void?BinaryStreamToPicture()??
- {??
- ????string?url?=?@"http://php.weather.sina.com.cn/images/yb3/78_78/duoyun_0.png";??
- ????WebClient?client?=?new?WebClient();??
- ????byte[]?pageData?=?client.DownloadData(url);??
- ????//將二進制流數據轉換為圖片??
- ????pictureBox1.Image?=?Image.FromStream(new?MemoryStream(pageData));??
- ????Bitmap?bmp?=?new?Bitmap(new?MemoryStream(pageData));??
- ????string?path?=?Application.StartupPath;??
- ????string?fullPath?=?path?+?"\\images\\"+?Guid.NewGuid().ToString()+".png";??
- ????richTextBox1.Text?=?fullPath;??
- ????bmp.Save(fullPath,?System.Drawing.Imaging.ImageFormat.Png);??
- }??
- #endregion?
轉載于:https://www.cnblogs.com/jimmyLei/p/9037892.html