C# 从字符串加载图像
由于一些数据存储限制(noSQL),我需要将图像存储为字符串。 如何将图像位图序列化为字符串并返回。 我是这样做的:C# 从字符串加载图像,c#,.net,silverlight,silverlight-4.0,C#,.net,Silverlight,Silverlight 4.0,由于一些数据存储限制(noSQL),我需要将图像存储为字符串。 如何将图像位图序列化为字符串并返回。 我是这样做的: Uri testImageUri = new Uri("/DictionaryBasedVM;component/test.jpg", UriKind.Relative); StreamResourceInfo sri = Application.GetResourceStream(testImageUri); var stringData = GetS
Uri testImageUri = new Uri("/DictionaryBasedVM;component/test.jpg", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(testImageUri);
var stringData = GetString(sri.Stream);
ImageSource = stringData;
在哪里
以及以下财产:
private string _ImageSource = "";
public string ImageSource
{
set
{
_ImageSource = value;
byte[] byteArray = Encoding.Unicode.GetBytes(value);
MemoryStream imageStream = new MemoryStream(byteArray);
BitmapImage imageSource = new BitmapImage();
imageSource.SetSource(imageStream);
ImageControl.Source = imageSource;
}
get
{
return _ImageSource;
}
}
我得到错误:“灾难性故障(来自HRESULT的异常:0x8000FFFF(E_意外))”,如图所示:
即使我不将其存储为字符串,我仍然很好奇为什么我不能这样做 Unicode可能不是用于此目的的最佳编码。您最好使用Base64编码
字节[]
并存储它。您是否尝试过使用and方法?我猜unicode GetString/GetBytes不起作用,因为您的字节数组没有与“已知字符”对齐。Base64将为字符串增加30%的大小。我认为这里没有必要。对于这种特殊的二进制需求,您可以安全地将字节“强制转换”为字符,反之亦然,只要您不使用字符串。下面是一些似乎有效的代码:
// usage example
string encoded = FileToString("myimage.png");
Console.WriteLine(s.Length);
FileFromString(encoded, "copy.png");
public static void FileFromString(string input, string filePath)
{
if (input == null)
throw new ArgumentNullException("input");
if (filePath == null)
throw new ArgumentNullException("filePath");
using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
byte[] buffer = FromString(input);
stream.Write(buffer, 0, buffer.Length);
}
}
public static byte[] FromString(string input)
{
if (input == null)
throw new ArgumentNullException("input");
char[] cbuffer = input.ToCharArray();
byte[] buffer = new byte[cbuffer.Length];
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)cbuffer[i];
}
return buffer;
}
public static string FileToString(string filePath)
{
if (filePath == null)
throw new ArgumentNullException("filePath");
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Write))
{
return ToString(stream);
}
}
public static string ToString(Stream input)
{
if (input == null)
throw new ArgumentNullException("input");
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[4096];
char[] cbuffer = new char[4096];
int read;
do
{
read = input.Read(buffer, 0, buffer.Length);
for (int i = 0; i < read; i++)
{
cbuffer[i] = (char)buffer[i];
}
sb.Append(new string(cbuffer, 0, read));
}
while (read > 0);
return sb.ToString();
}
//用法示例
字符串编码=FileToString(“myimage.png”);
控制台写入线(s.长度);
FileFromString(编码为“copy.png”);
公共静态void FileFromString(字符串输入,字符串文件路径)
{
如果(输入==null)
抛出新的ArgumentNullException(“输入”);
if(filePath==null)
抛出新的ArgumentNullException(“文件路径”);
使用(FileStream stream=newfilestream(filePath,FileMode.OpenOrCreate,FileAccess.Write,FileShare.None))
{
字节[]缓冲区=FromString(输入);
stream.Write(buffer,0,buffer.Length);
}
}
公共静态字节[]FromString(字符串输入)
{
如果(输入==null)
抛出新的ArgumentNullException(“输入”);
char[]cbuffer=input.ToCharArray();
字节[]缓冲区=新字节[cbuffer.Length];
for(int i=0;i0);
使某人返回字符串();
}
但是,存储字符串的系统可能不喜欢包含0或其他特殊数字的字符串。在这种情况下,base64仍然是一个选项。解决了这个问题+1(自第二次响应后)。PS:这是ToBase64String()和FromBase64String()的好主意,我非常感谢您的努力+1。不过,幸运的是,我没有空间限制。
private string _ImageSource = "";
public string ImageSource
{
set
{
_ImageSource = value;
byte[] byteArray = Encoding.Unicode.GetBytes(value);
MemoryStream imageStream = new MemoryStream(byteArray);
BitmapImage imageSource = new BitmapImage();
imageSource.SetSource(imageStream);
ImageControl.Source = imageSource;
}
get
{
return _ImageSource;
}
}
// usage example
string encoded = FileToString("myimage.png");
Console.WriteLine(s.Length);
FileFromString(encoded, "copy.png");
public static void FileFromString(string input, string filePath)
{
if (input == null)
throw new ArgumentNullException("input");
if (filePath == null)
throw new ArgumentNullException("filePath");
using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
byte[] buffer = FromString(input);
stream.Write(buffer, 0, buffer.Length);
}
}
public static byte[] FromString(string input)
{
if (input == null)
throw new ArgumentNullException("input");
char[] cbuffer = input.ToCharArray();
byte[] buffer = new byte[cbuffer.Length];
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)cbuffer[i];
}
return buffer;
}
public static string FileToString(string filePath)
{
if (filePath == null)
throw new ArgumentNullException("filePath");
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Write))
{
return ToString(stream);
}
}
public static string ToString(Stream input)
{
if (input == null)
throw new ArgumentNullException("input");
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[4096];
char[] cbuffer = new char[4096];
int read;
do
{
read = input.Read(buffer, 0, buffer.Length);
for (int i = 0; i < read; i++)
{
cbuffer[i] = (char)buffer[i];
}
sb.Append(new string(cbuffer, 0, read));
}
while (read > 0);
return sb.ToString();
}