[英]Convert an image to base64 without using HTML5 Canvas
First, a little background: 首先,有一点背景:
I apologize ahead of time for the long-winded nature of this preface; 对于此序言的冗长性,我提前表示歉意。 however, it may assist in providing an alternate solution that is not specific to the nature of the question.
但是,它可能有助于提供一种并非针对问题性质的替代解决方案。
I have an ASP.NET MVC application that uses embedded WinForm UserControls. 我有一个使用嵌入式WinForm UserControls的ASP.NET MVC应用程序。 These control provide "ink-over" support to TabletPCs through the
Microsoft.Ink
library. 这些控件通过
Microsoft.Ink
库为TabletPC提供“墨水覆盖”支持。 They are an unfortunate necessity due to an IE8 corporate standard; 由于IE8企业标准,它们是不幸的必需品。 otherwise, HTML5 Canvas would be the solution.
否则,HTML5 Canvas将是解决方案。
Anyway, an image URL is passed to the InkPicture
control through a <PARAM>
. 无论如何,图像URL通过
<PARAM>
传递到InkPicture
控件。
<object VIEWASEXT="true" classid="MyInkControl.dll#MyInkControl.MyInkControl"
id="myImage" name="myImage" runat="server">
<PARAM name="ImageUrl" value="http://some-website/Content/images/myImage.png" />
</object>
The respective property in the UserControl
takes that URL, calls a method that performs an HttpWebRequest
, and the returned image is placed in the InkPicture
. UserControl
的相应属性采用该URL,调用执行HttpWebRequest
的方法,并将返回的图像放置在InkPicture
。
public Image DownloadImage(string url)
{
Image _tmpImage = null;
try
{
// Open a connection
HttpWebRequest _HttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
_HttpWebRequest.AllowWriteStreamBuffering = true;
// use the default credentials
_HttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
// Request response:
System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();
// Open data stream:
System.IO.Stream _WebStream = _WebResponse.GetResponseStream();
// convert webstream to image
_tmpImage = Image.FromStream(_WebStream);
// Cleanup
_WebResponse.Close();
}
catch (Exception ex)
{
// Error
throw ex;
}
return _tmpImage;
}
Problem 问题
This works, but there's a lot of overhead in this process that significantly delays my webpage from loading (15 images taking 15 seconds...not ideal). 这行得通,但是在此过程中有很多开销,这极大地延迟了我的网页的加载(15张图像耗时15秒……不理想)。 Doing
Image img = new Bitmap(url);
正在做
Image img = new Bitmap(url);
in the UserControl
does not work in this situation because of FileIO Permission issues (Full trust or not, I have been unsuccessful in eliminating that issue). 由于FileIO权限问题(无论是否完全信任,我都无法成功消除该问题),因此
UserControl
则无法在这种情况下使用。
Initial Solution 初始解决方案
Even though using canvas
is not a current option, I decided to test a solution using it. 即使使用
canvas
不是当前的选择,我还是决定使用它来测试解决方案。 I would load each image in javascript, then use canvas
and toDataUrl()
to get the base64 data. 我将在javascript中加载每个图像,然后使用
canvas
和toDataUrl()
获取base64数据。 Then, instead of passing the URL to the UserControl
and have it do all the leg work, I pass the base64 data as a <PARAM>
instead. 然后,我没有将URL传递给
UserControl
并让它完成所有工作,而是将base64数据作为<PARAM>
传递。 Then it quickly converts that data back to the image. 然后,它将数据快速转换回图像。
That 15 seconds for 15 images is now less than 3 seconds. 15张图像的15秒现在不到3秒。 Thus began my search for a image->base64 solution that worked in IE7/8.
因此,我开始寻找在IE7 / 8中工作的image-> base64解决方案。
Here are some additional requirements/restrictions: 以下是一些其他要求/限制:
I hope I've provided sufficient information and I appreciate any direction you're able to give. 希望我提供了足够的信息,也感谢您能提供的任何指导。
Thanks. 谢谢。
You can use any of the FlashCanvas, fxCanvas or excanvas libraries, which simulate canvas using Flash or VML in old internet explorer versions. 您可以使用任何FlashCanvas,fxCanvas或excanvas库,这些库在旧的Internet Explorer版本中使用Flash或VML模拟画布。 I believe all of these provide the
toDataURL
method from the Canvas API, allowing you to get an encoded representation of your image. 我相信所有这些都提供了Canvas API中的
toDataURL
方法,使您能够获取图像的编码表示形式。
After extensive digging around (I'm in the same fix) I believe this is the only solution, short of writing a PHP script that you can send the image to. 经过广泛的挖掘(我使用的是相同的修补程序),我相信这是唯一的解决方案,只需编写可将图像发送到的PHP脚本即可。 The problem with that of course is that there isn't a way to send images to the PHP script unless any of these three conditions is true:
当然,问题在于,除非满足以下三个条件之一,否则无法将图像发送到PHP脚本:
Uint8Array
) Uint8Array
) sendAsBinary
sendAsBinary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.