WPF Image解压缩保存为XML中字符串

本文介绍了如何将WPF Image对象压缩成字符串,并存储在XML文件中,以及如何从XML文件中读取字符串解压缩回WPF Image的过程。
摘要由CSDN通过智能技术生成

1. 把WPF image 压缩为字符串并保存在XML中

2.    把XML中的字符串解压为WPF image




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Compression;
using System.IO;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;


namespace WpfAppByteZipTest
{
	/// <summary>
	/// MainWindow.xaml 的交互逻辑
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
			string imageFile = @"C:\Documents and Settings\Administrator\My Documents\My Pictures\11.jpg";
		//	BitmapImage imgSource = new BitmapImage(new Uri(imageFile, UriKind.Absolute));
			
			byte[] b = GetPictureData(imageFile);
			BitmapImage myimg = ByteArrayToBitmapImage(b);
			image1.Source = myimg;
		}

		/// <summary>
		/// 将字节数组进行压缩后返回压缩的字节数组
		/// </summary>
		/// <param name="data">需要压缩的数组</param>
		/// <returns>压缩后的数组</returns>
		public static byte[] Compress(byte[] data)
		{
			MemoryStream stream = new MemoryStream();
			GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress);
			gZipStream.Write(data
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值