图像相关(三) Bitmap与byte[]、BitmapImage与byte[]互相转换、图像加载与保存
来源:互联网 发布:软件开发学校好吗 编辑:程序博客网 时间:2024/06/06 01:42
using System;using System.Drawing;using System.IO;using System.Windows.Media.Imaging;namespace WpfApplication1.com.utils{ ////// 图像相关工具 /// public static class ImageTools { public static byte[] BitmapToBytes(Bitmap bitmap) { MemoryStream ms = new MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] buffer = ms.ToArray(); ms.Close(); ms.Dispose(); return buffer; } public static Bitmap BytesToBitmap(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); Bitmap bmp = new Bitmap(ms); ms.Close(); ms.Dispose(); return bmp; } public static byte[] BitmapImageToBytes(BitmapImage bmp) { byte[] buffer = null; try { Stream stream = bmp.StreamSource; if (stream != null && stream.Length > 0) { //很重要,因为Position经常位于Stream的末尾,导致下面读取到的长度为0。 stream.Position = 0; using (BinaryReader br = new BinaryReader(stream)) { buffer = br.ReadBytes((int)stream.Length); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return buffer; } public static BitmapImage BytesToBitmapImage(byte[] buffer) { BitmapImage bmpImg = new BitmapImage(); bmpImg.BeginInit(); bmpImg.StreamSource = new MemoryStream(buffer); bmpImg.EndInit(); return bmpImg; } //需要先把Bitmap转换为byte[],再把byte[]转换为BitmapImage public static BitmapImage BitmapToBitmapImage(Bitmap bitmap) { byte[] buffer = BitmapToBytes(bitmap); BitmapImage bmpImg = new BitmapImage(); bmpImg.BeginInit(); bmpImg.StreamSource = new MemoryStream(buffer); bmpImg.EndInit(); return bmpImg; } //需要先把BitmapImage转换为byte[],再把byte[]转换为Bitmap public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage) { byte[] buffer = BitmapImageToBytes(bitmapImage); Bitmap bmp = BytesToBitmap(buffer); return bmp; } }}using System;using System.Drawing;using System.IO;using System.Windows.Media;using System.Windows.Media.Imaging;namespace WpfApplication1.com.core{ ////// 图像管理 /// public static class ImageManager { //加载图像-------------------- public static BitmapImage LoadByBitmapImage(string imgUrl,UriKind uriKind = UriKind.Absolute) { BitmapImage bmpImg = new BitmapImage(); bmpImg.BeginInit(); bmpImg.UriSource = new Uri(imgUrl, uriKind); bmpImg.EndInit(); return bmpImg; } public static BitmapImage LoadByFileStream(string imgUrl) { FileStream fs = new FileStream(imgUrl,FileMode.Open); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); BitmapImage bmpImg = new BitmapImage(); bmpImg.BeginInit(); bmpImg.StreamSource = new MemoryStream(buffer); bmpImg.EndInit(); return bmpImg; } public static Bitmap LoadByBitmap(string imgUrl) { Bitmap bmp = new Bitmap(imgUrl); return bmp; } //保存图像--------------------- public static void SaveByBimtap(string imgUrl,Bitmap bitmap) { //Bitmap可以直接保存图像,与源图像大小一样 bitmap.Save(imgUrl); } public static void SaveByFile(string imgUrl,byte[] buffer) { //这种保存原始大小 File.WriteAllBytes(imgUrl, buffer); } public static void SaveByFileStream(string imgUrl,BitmapImage bmpImg) { //这种保存跟源图大小不一样,可能比源图还要大(跟编码有关) //编码 BitmapEncoder encoder = new JpegBitmapEncoder();// //BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmpImg)); FileStream fs = new FileStream(imgUrl, FileMode.Create); encoder.Save(fs); fs.Close(); } //保存wpf组件为图片 //继承关系 FrameworkElemet-UIElement-Visual public static void SaveVisual(string imgUrl,Visual visual,int width,int height) { RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default); rtb.Render(visual); BitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); FileStream fs = new FileStream(imgUrl,FileMode.Create); encoder.Save(fs); fs.Close(); } }}
阅读全文
0 0
- 图像相关(三) Bitmap与byte[]、BitmapImage与byte[]互相转换、图像加载与保存
- bitmapimage与byte[]相互转换
- bitmapimage与byte[]相互转换 --转
- WPF BitmapImage与byte[]的转换
- BitMapImage与byte[]之间的转换
- IplImage图像与BYTE图像之间的转换与利用
- byte[]与String互相转换
- CMSampleBufferRef与byte*互相转换
- Bitmap与byte[]相互转换
- IpiImage图像与BYTE图像之间的转换
- Byte[]与图片互相转换,保存到本地
- WPF中bitmapimage与byte[]格式相互转换
- android 中bitmap与String(base64) byte[]、和drawable的互相转换
- 图片与byte数组互相转换
- java byte 与int的互相转换
- Java 图片与byte数组互相转换
- byte[]与各种数据类型互相转换示例
- <Java>byte与short互相转换
- 5.2
- 一张细化了的hbase存储结构图
- (一)数据结构与算法--前篇
- STL之list容器详解
- 价值200元的小程序卡卷开发免费送
- 图像相关(三) Bitmap与byte[]、BitmapImage与byte[]互相转换、图像加载与保存
- centos7基于lnmp配置wordpress
- hdu 2642 Stars(二维树状数组)
- HDU 5834 Magic boy Bi Luo with his excited tree——树形dp
- Ebean ORM功能介绍
- Maven项目Run As无Run On Server的解决方法
- 1119: 一维数组排序
- Python OpenCV图像模板匹配
- [leetcode] 459. Repeated Substring Pattern