我想加载图像并将其直接编码为base64字符串。小/低分辨率图片加载速度很快,但加载iPhone 5拍摄的照片(例如2448 x 3264,2.61 MB)需要很长时间。有没有办法在我的应用程序中更快地加载这些图像?
这是我的C#代码:
private void button1_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//Setup OpenFileDialog
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
string sFileName = openFileDialog1.FileName;
textBox1.Text = sFileName;
pictureBox1.ImageLocation = sFileName;
using (Bitmap bm = new Bitmap(sFileName))
{
using (var ms = new MemoryStream())
{
bm.Save(ms, ImageFormat.Jpeg);
base64 = Convert.ToBase64String(ms.ToArray());
textBox3.Text = base64;
}
}
}
}
}
}
答案 0 :(得分:1)
是的,您使用的方法可能是我见过的最受欢迎的方法。我也需要转换图像,所以我用同样的方法写了一些东西。 然而,对我来说效率太低而且速度太慢。我决定寻找解决方法。我发现这个用C John Walker编写的小程序称为base64.exe
。我在输出上做了一些包装并对输出进行了一些修改。
结果是Base64encoder,但它是在很久以前写的。我已在此处上传了源:Base64encoder_v2.1-source.zip(在MIT许可下发布)
我后来创建了一个可以在这里找到的C库:https://github.com/joedf/base64.c(在麻省理工学院许可下发布)
您可以将包装器分叉或将c库分叉到c#库中,或者使用其他库或从头开始编写代码......:/
答案 1 :(得分:0)